How to reference a Google Sheet by filename?

I would like to use IMPORTRANGE function, but it can only reference a file by URL not by filename.

Example filename:

  • 2018-11.xlsx
  • 2018-12.xlsx
  • 1
    @serenesat on this case the original tags were fine. – Rubén Dec 3 at 16:19
  • @Rubén: Ok, thank you! – serenesat Dec 4 at 4:46

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

// Log the name of every file in the user's Drive that modified after February 28,
// 2013 whose name contains "untitled".
var files = DriveApp.searchFiles(
    'modifiedDate > "2013-02-28" and title contains "untitled"');
while (files.hasNext()) {
  var file = files.next();
  Logger.log(file.getName());
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.