I've been struggling with the same problem. Instead of writing a custom function, I add a different query string to spreadsheet_url
in IMPORTRANGE
in the hope that each time the page is refreshed, Google thinks it needs to fetch data from a new spreadsheet. I simply append a new timestamp to make the url unique each time. It's a blatant hack, but it has been working for me across a lot of sheets.
My formula previously looked something like:
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/123123123123/edit#gid=1816927174","'Sheet1'!A1:B25")
And now it looks like:
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/123123123123/edit#gid=1816927174"&"?"&now(),"'Sheet1'!A1:B25")
Update:
This method no longer works since Google no longer permits now()
inside importrange()
. See comment from Hugh below.
While the formula added by Brian Bennet works, it takes a significant amount of time to update and feels somewhat clunky in an arrayformula.
Instead, I used INDIRECT
to solve my problem :
A | B | C
------------------------------------------------------------
1| =SUM(INDIRECT("'Raw Data'!A:A")) | |
2| =SUM(INDIRECT(B2&"A:A")) | ="'Raw Data'!" |
3| | |
On the column A
of this example, A1 is what I used for quick formulas, and A2 allowed for more modularity
However, as I used this formula inside ARRAYFORMULA
, the refresh time of the array became erratic, especially when the sheet doesn't exist at first.
When the sheet doesn't exist, all cells in the arrayformula are marked #N/A
. When I add the sheet Raw Data
, even if I refresh my page with F5 several times, I still got the #N/A
. I am unsure how to force the refresh. A few minutes afterwards however, the data is updated, and I can edit my sheet & the arrayformula is calculated instantly (calculation time is only very slightly impacted).
This might be another issue not necessarily related to my initial question, but I thought I should mention this behavior anyway.
As a side note : this fix doesn't work for pivot tables. I am still looking for a solution for that case.
Best Answer
Google Sheets hasn't a built-in function to make reference a Google Sheet by filename and a custom function can't be used because they ran anonymously and the Google Apps Script Google Drive and Google Drive Advanced services require authorization to use them.
The only way is by using scripts triggered by other means like a macro, a custom menu, dialog or installable triggers or script editor.
You could DriveApp.searchFiles(params) to get a collection of files with the specified name, then use file iterator to loop through that collection.
Example from the above link