Home > Software design >  Google Sheets Script DriveApp.getFiles() A server error occurred
Google Sheets Script DriveApp.getFiles() A server error occurred

Time:12-18

As part of a longer script, I am using the DriveApp.getFiles() function to list the files on my google drive as follows:

function logFiles(){
// Log the name of every file in the user's Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
  Logger.log(file.getName());
}
}

Using the debug function the execution stops on the while statement as files is undefined. The logfile shows:

Error Exception: We're sorry, a server error occurred. Please wait a bit and try again. at logFiles(Duplicate:190:14)

Needless to say the error hasn't magically sorted itself out, but the function WAS working just yesterday.

Steps taken so far is to create a project and deploy the code using the project ID. I have designated myself and one other as project "Owners" and we are both listed as Test accounts to run the script.

I am only using a free personal google account as this code is attached to a single Google Sheet. Other functions in the same script are working, so it would seem the permissions are okay; otherwise I would expect a permissions error.

The sample code above is taken straight from the google reference docs and it still doesn't work which seems to eliminate any finger trouble in my code.

Would appreciate any ideas on what could be causing this.

CodePudding user response:

try it this way:

It will take a lot less time this way and you get your result in column 1 of the active sheet

function logFiles() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  var files = DriveApp.getFiles();
  let fA = [];
  while (files.hasNext()) {
    fA.push([files.next()]);
  }
  sh.getRange(1,1,fA.length,1).setValues(fA);
}

CodePudding user response:

After much poking around the Project interface, I have managed to get this working by enabling the Google Drive API and Google Sheets API from the Google Cloud Platform API Dashboard for my project.

I have no idea why this works, but it does. The script was bound directly to a google sheet located in the same drive that was being searched so by all accounts it should have worked without this step. But for what it is worth - that was the fix for me.

  • Related