Search code examples
google-apps-scriptwhile-loopxlsx

Stop the function when the folder doesn't have xlsx files in (Google App scripts)


I have the next function on Google App Script:

function remove(){
  var folder = DriveApp.getFolderById('folderID');
  var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
  while (files.hasNext()){
    var file = files.next();
    folder.removeFile(file);
  }
}

I want stop/break/exit the function when the folder does not have/contains xlsx files. How I do that? Greetings


Solution

  • Your function should work fine. Including stopping if there are no more excel files.

    But you do need to change this:

       var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
    

    (this searches the entire Drive)

    to

       var files = folder.getFilesByType(MimeType.MICROSOFT_EXCEL);