Search code examples
javascripthtmlgoogle-apps-scriptgoogle-sheetsgoogle-drive-api

Scan files in a Google Drive folder via an HTML form


I would like some help checking how many files are inside a Google Drive folder before allowing a form submission.

I have this form here: enter image description here

I would like that when the Submit button is clicked, a function checks how many files exist in a child folder of the folder where this GAS file is.

The HTML form GAS is linked to this worksheet here: enter image description here

After checking how many files there are in the _ folder I would like to apply the following if/else statements:

If the _ folder has more than 3 files, display a confirmation pop-up, if the answer is Yes, create any file in the _ folder, if the answer is No, just don't allow the form submission.

If the _ folder contains 4 or more files, display an error message and disallow form submission.

The code of this HTML form can be seen here, accessing the spreadsheet's GAS:

HTML Form - Demonstration


Solution

  • Partial Solution:

    I made some changes on the getLimitFolder() function to return the count of the files inside the _ folder.

    function getLimitFolder(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var pastapai = DriveApp.getFileById(ss.getId()).getParents();
      var files = [];
      Logger.log(pastapai);
      //var limitfolder = pastapai.next().getFoldersByName("_").next().getId();
      var limitfolder = pastapai.next().getFoldersByName("_").next().getFiles();
      while(limitfolder.hasNext()){
        var file = limitfolder.next();
        files.push([file.getId()]);
      }
      console.log(files.length);
      return files.length;
    }
    

    Inside the folder _: enter image description here

    From here, since the function can now return the count of files within the _ folder, you may need to create a new modal window on your HTML file to display the message first and create a separate function to determine the conditions for your if else statement.