6

I am attempting to use a query to display data off multiple Google Sheets. I make a new sheet every week that has a specific sheet name, e.g. Week of 01/13, Week of 01/06 and so forth.

The following is where my idea spawned from for reference:

I have a summary sheet that is using COUNTA(INDIRECT("'" & A5 & "'!E4:E",true)

A5 being a cell that concatenates a date and words to replicate the sheet names.

The row on the summary sheet does not populate until B5<=today()

So I am able to set it an forget it and the sheet will continue to give me my weekly data as the days progress and keeps the sheets clean until the week is upon us.

Long story short, I have a query that I use that gives me all the data I need with a specific parameter but I have to manually update the data syntax array with the new sheet names each week.

=QUERY({'Week of 01/13'!A:P;'Week of 01/06'!A:P;'Week of 12/30'!A:P;'Week of 12/23'!A:P;'WEEK OF 12/16'!A:P;'WEEK OF 12/09'!A:P;'WEEK OF 12/02'!A:P;'WEEK OF 11/25'!A:P;'WEEK OF 11/18'!A:P;'WEEK OF 11/11'!A:P;'WEEK OF 11/04'!A:P;'WEEK OF 10/28'!A:P;'WEEK OF 10/21'!A:P;'WEEK OF 10/14'!A:P;'WEEK OF 10/07'!A:P;'WEEK OF 09/30'!A:P;'WEEK OF 09/23'!A:P;'WEEK OF 09/16'!A:P;'WEEK OF 09/09'!A:P;'WEEK OF 09/02'!A:P},
 "Select * where Col11 = 'RD' order by Col2 desc",0)

I would like to build a reference to an array that will auto-populate a concatenation based on the day.

Sample structure of reference

Using the following code I can have the concatenate give me the array I need,

=if(H4<=today(),CONCATENATE("'",H$1,text(H4,"mm/dd"),"'!A:P;",),"")

but when I try to input it into the query function it just returns the concatenated text:

=QUERY(I1,"Select *")

 

'Week of 01/06'!A:P;'Week of 01/13'!A:P

I have tried with and without the curly brackets with no success.

I would like the sheet to be able to refresh and see that it is the correct day, the new sheet name is populated and the query gets updated.

I need help with making I1 work.

Link to Test Query Sheet

player0
  • 124,011
  • 12
  • 67
  • 124
s0m3thlng
  • 61
  • 1
  • 3

2 Answers2

10

dudes who copy-pasted INDIRECT function into Google Sheets completely failed to understand the potential of it and therefore they made zero effort to improve upon it and cover the obvious logic which is crucial in this age of arrays.

in other words, INDIRECT can't intake more than one array:

=INDIRECT("Sheet1!A:B"; "Sheet2!A:B")

nor convert an arrayed string into active reference, which means that any attempt of concatenation is also futile:

=INDIRECT(MasterSheet!A1:A10)
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{Sheet1!A:B; Sheet2!A:B}")
————————————————————————————————————————————————————————————————————————————————————
={INDIRECT("Sheet1!A:B"; "Sheet2!A:B")}
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}")

the only possible way is to use INDIRECT for each end every range like:

={INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}

which means that the best you can do is to pre-program your array like this if only part of the sheets/tabs is existant (let's have a scenario where only 2 sheets are created from a total of 4):

=QUERY(
 {IFERROR(INDIRECT("Sheet1!A1:B5"), {"",""}); 
  IFERROR(INDIRECT("Sheet2!A1:B5"), {"",""}); 
  IFERROR(INDIRECT("Sheet3!A1:B5"), {"",""}); 
  IFERROR(INDIRECT("Sheet4!A1:B5"), {"",""})}, 
 "where Col1 is not null", 0)

so, even if sheet names are predictable (which not always are) to pre-program 100+ sheets like this would be painful (even if there are various sneaky ways how to write such formula under 30 seconds)


an alternative would be to use a script to convert string and inject it as the formula

A1 would be formula that treates a string that looks like real formula:

=ARRAYFORMULA("=QUERY({"&TEXTJOIN("; ", 1, 
 IF(A3:A<>"", "'Week of "&LEFT(A3:A, 5)&"'!A1:D5", ))&
 "}, ""where Col1 is not null"", 1)")

further populating of A6:A will expand the string automatically

then this script will take the string from A1 cell and it will paste it as valid formula into C5 cell:

function onEdit() { 
var sheet = SpreadsheetApp.getActive().getSheetByName('Master Sheet');  
var src = sheet.getRange("A1");
var str = src.getValue(); 
var cell = sheet.getRange("C5"); 
cell.setFormula(str);
}

0

of course, the script can be changed to onOpen trigger or with custom name triggered from the custom menu or via button (however it's not possible to use the custom function as formula directly)

player0
  • 124,011
  • 12
  • 67
  • 124
1

If you're trying to update the data your query is looking at and you're feeding it a string, you need to put that string within the indirect() function. That will interpret your string as a data reference and point your query() in the right direction.

So for this you'd probably have

=QUERY(INDIRECT(I1),"Select *")
Luke Allpress
  • 170
  • 1
  • 2
  • 8
  • I attempted that but it looks as if the quotations makes it invalid, it will add a single quote to the end or add a double quote to the beginning. – s0m3thlng Jan 21 '19 at 21:14
  • Your indirect() is referencing F2, where you currently have a typo in your cell reference. {'Week of 01/06'!A:C; 'Week of 01/13'!A:C'Week of 01/20'!A:C} -- you're missing the semicolon after your second range. – Luke Allpress Jan 23 '19 at 13:46