Home > Software engineering >  Google Apps Script getFileByName returning nothing
Google Apps Script getFileByName returning nothing

Time:01-08

I have tried running several versions of the code below in a shared drive, but the function does not seem to return anything, even though I am expecting the filenames of the files matching q. What am I missing here?

function onOpen(e) {
  SpreadsheetApp.getUi()
  .createMenu('testMenu')
  //.addItem('map clientfolders', 'readClientFolders')
  //.addItem('map clientnames', 'clientChecking')
  //.addItem('map all folders & files', 'getMyFilesFromDrive')
  .addItem('getRegExFiles', 'matchingFileArray')
  .addToUi();
}

function matchingFileArray() {
  var q = 'title contains "2022"';
  var files = DriveApp.getFilesByName(q);

  var activeSheet = SpreadsheetApp.getActive().insertSheet('matchFiles');
  while (files.hasNext()) {
    var file = files.next();
    activeSheet.clear;
    activeSheet.appendRow([file]);
  }
}

I have cleaned up the code as much as possible, looked up answers and documentation, including different possible parameters for the getFilesByName function. I expected one file ID per Sheets row, but the script finishes instantly (no changes to the sheet were made).

CodePudding user response:

See comment from Cooper, DriveApp.searchFiles() is the perfect function here.

Try DriveApp.searchFiles() https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)

  • Related