Array of IMPORTRANGEs in Google Sheets - google-sheets

For this sample table:
A
B
C
D
E
1
Range!A1:C5
URL1
= Formula here
2
URL2
3
URL3
4
...
I have this working formula in C1:
={IMPORTRANGE(B1, A1); IMPORTRANGE(B2, A1); IMPORTRANGE(B3, A1)}
A1 contains the range to pull from the target Sheets.
Links to the target Sheets are found in column B.
(This is a simplified example. My actual Sheet has 21 entries in column B.)
Question:
If I will have to add/remove URLs in column B, I have to adjust the formula in C1 and add/remove IMPORTRANGEs.
I would like to have a formula that automatically adjusts to the entries in column B.
Solutions tried:
I tried this, but it did not work:
={JOIN("", ARRAYFORMULA(IF(LEN(B1:B), "IMPORTRANGE(""" & B1:B & """, """ & A1 & """); ", "")))}
The JOIN function returns a text that should be identical to what the array { ... } parses as a formula. But it doesn't. Wrapping it with INDIRECT also does not work.
Any suggestions on how I could make this work?

if by "working formula" you mean valid formula that returns data then only thing you can do is hardcode it like this:
=QUERY({
IFERROR(IMPORTRANGE(B1, A1), {"","",""});
IFERROR(IMPORTRANGE(B2, A1), {"","",""});
IFERROR(IMPORTRANGE(B3, A1), {"","",""})},
"where Col1 is not null", )
A1:C5 = 3 columns = {"","",""}
you can generate formula dynamically with arrayformula but the result will be always a text string and not active formula. tho if you wanna go this way you will need a script that will convert your text string into active formula. example:
https://stackoverflow.com/a/73119313/5632629
https://stackoverflow.com/a/61819704/5632629

Related

Counting over aggregated columns in Google Sheets

I have the yellow table shown below, and I'm trying to get the blue table, which aggregates columns B:F by value, and then counts the number of 'x' symbols for each row value of column A.
Is there some basic SQL/array magic formula to get this, please? There must be.
Use this new functions formula
=BYROW(B2:4, LAMBDA(v, COUNTIF(v, "=x")))
Used:
BYROW, LAMBDA, COUNTIF
v is the array_or_range
Update
={ A2:A4, BYROW(B2:4, LAMBDA(vv, COUNTIF(vv, "=x")))}
For fun
Update 02
=ArrayFormula(TRANSPOSE(QUERY({
QUERY(TRANSPOSE(IF(A1:4<>"x",A1:4,1)),
" Select * Where Col1 is not null ", 1)},
" Select (Col1),sum(Col2),sum(Col3),sum(Col4) Group by Col1 ", 1)))

Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem

I'm pretty new with ArrayFormula, have been trying but sometime the formula works, sometimes does not. What I'm trying to do is the combination of ArrayFormula, Countif for searching partial text.
As shown in the worksheet below, there are 10 subjects (column A), each subject has at least one of 4 samples (A,B,C,D) summarized as a string (column B). What I'm trying to do is to find which subject has sample A or B or C or D.
I have tried single formula for each sample, eg cell D3
=IF(COUNTIF($B3,"*"&$D$2&"*")>0,$A3,"")
it returns the correct results. However, when I try arrayformula in cell I3,
=arrayformula(IF(COUNTIF($B3:B,"*"&$D$2&"*")>0,$A3:A,""))
The answers are weird. For example: Subjects (Gamma, Zeta, Eta, Theta) who don't have the sample "A" are shown to have sample "A". And this applies to sample B,C,D too
Not sure what went wrong in here. Here is the link to the worksheet
I wouldn't use Countifs or an array formula. Use filter instead. Put this formula in cell i3.
=Filter(if(REGEXMATCH(B3:B,$D$2),A3:A,""),B3:B<>"")
try:
=INDEX(QUERY(IFERROR(TRIM(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B3:B, ","))="",,
SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"))
or use in row 2 if you want to sort it as in your example:
=INDEX(IFNA(VLOOKUP(A2:A, QUERY(IFERROR(TRIM(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B3:B, ","))="",,SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select Col2,max(Col2) where Col2 is not null group by Col2
pivot Col1 label Col2'Subjects'"), {2,3,4,5}, 0)))
You can accomplish all four columns of results with a single formula.
Delete all formulas from I3:L3.
Place the following formula into I3:
=ArrayFormula(IF(REGEXMATCH(B3:B,I2:L2),A3:A,))
In plain speech, this read "If anything in B3:B matches a value found in I2:L2, return A3:A in the matching columns(s) at the matching row(s); if not, return null."

search for a specific Column and then lookup the value of the matching row based off a name

I am trying to get a google sheet to search for a specific cell in a table. The headers change so it might be A6 one week and then A9 the other and so on.
Once it's found that row, I want it to search and pull all of that departments names and data for the column its matched with.
I am 23 sheets in and my heads hit a brick wall and I just can figure it out.
You can try:
=QUERY({A:B,INDEX(A:G,0,MATCH(D25,1:1,0))},"SELECT * WHERE Col2='" & LOWER(F25) & "'")
Note - you should remove unnecessary spaces. In sample data, they were in cells C1 and D25.
Try this:
=QUERY(
FILTER(
IFS(
TRIM(1:20) = "", 0,
ISNUMBER(1:20), 1:20,
True, LOWER(TRIM(1:20))
),
1:1 <> ""
),
"SELECT Col1, Col2, Col" & MATCH(TRIM(D25), ARRAYFORMULA(TRIM(1:1)),) & "
WHERE Col2 = '" & LOWER(F25) & "'",
1
)
You can use a combination of CHAR(MATCH...)) and Query formula to get this
=QUERY('Sheet Name'!A1:G20,"SELECT A, B, "&CHAR(MATCH("Log 4",'Sheet Name'!A1:G1)+64)&" WHERE B='w'")
Above formula only works till column Z, but thanks to Kishkin's comment below, you can use it beyond Z like this:
=QUERY('Sheet Name'!A1:G20,"SELECT A, B, `" & =REGEXEXTRACT(ADDRESS(1, MATCH("Log 4",'Sheet Name'!A1:G1), 4), "\D+") & "` WHERE B='w'")
You use SUBSTITUTE instead of REGEXTRACT too. You can refer to this Question.
the CHAR(MATCH...)) gets the column name of the desired Log
you can then use the column name to include that column in Query select statement
In the MATCH formula, you can also dynamically Match for a cell reference instead of specifying "Log 4" manually
Note: This is basically splitting the Query formula and concatenating it with another formula. So the &, ' and " symbols are really important
Sample Screenshot:

Google Spreadsheet adding sheetname

I have my current query below;
=QUERY({Source_1!B2:I;Source_2!B2:I;Source_3!B2:I},"select * where Col3 is not null",0)
I want to have all my data be combine to one to "All_Sheet" tab it works so far.
Now I need in Col A of my tab "All_Sheet" be included to all rows the Sheet Name like Source_1, Source_2 and so on.
Try this:
Add a calculated column
First build a range that contains the sheet name you want to reference. I did it by concatenating a range that is empty and the sheet name:
{N2:N&"Sheet1",Source_1!B2:I}
The above by itself won't work, because it needs to be told that it is an array. So we wrap it in ARRAYFORMULA():
ARRAYFORMULA({N2:N&"Sheet1",Source_1!B2:I})
Which gives a range that looks like this:
Then build up the rest of your query like this:
Sheet 1 | B2 Value | C2 Value | D2 Value | ...
Build our Query
Take the above and build up your multiple ranges and complete your query. Linebreaks for readability
=QUERY(
ARRAYFORMULA(
{
N2:N&"Sheet1",Source_1!B2:I;
N2:N&"Sheet2",Source_2!B2:I;
N2:N&"Sheet3", Source_3!B2:I
}),
"select * where Col3 is not null",0)

Have Arrayformula({Sheet1,Sheet2}) result in same colums ? (not side by side)

question about GoogleSpreadsheets.
Arrayformula({Sheet1,Sheet2})
this formula works for me cause it MERGES the content of the two sheets into a 3rd sheet (both sheets and destination sheet have the same structure/columnsCount)
BUT my problem is :
- everything taken from Sheet1 is put on columns A to W (the formula is indeed in cell A1)
- everything from Sheet2 is put in columns X to AT !
Is there a way to display first all the content of Sheet1 and THEN right-under it add all the lines from Sheet2
To 'stack' vertically, you need to use a semi-colon
=Arrayformula({Sheet1; Sheet2})
Note that his will also import blank rows. So to filter those out you may need to wrap a query() around it.
=QUERY (Arrayformula({Sheet1; Sheet2}), "where Col1 <> '' ")
or if the first col is numerical
=QUERY (Arrayformula({Sheet1; Sheet2}), "where Col1 is not null ")

Resources