Google Apps Script: getFileById Error "Exception: We're sorry, a server error occurred. Please wait a bit and try again"

Viewed 210

My script that grabs a url from a Google Sheet and attaches to an email has worked without issues on a test copy of a spreadsheet, but is throwing an error when run on the original.

Exception: We're sorry, a server error occurred. Please wait a bit and try again

Below is a trimmed down version of the script

function wireInfoEmail() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Current");
  var lr = sheet.getLastRow();
  var lc = sheet.getLastColumn();
  var range = sheet.getRange(3,1,lr-2,lc);
  var values = range.getValues();
  var headers = sheet.getRange(2,1,1,lc).getValues();
  var atchUrlColName = "Instructions_Link";
  var atchUrlCol = headers[0].indexOf(atchUrlColName);
  var propAdColName = "Address";
  var propAdCol = headers[0].indexOf(propAdColName);
  var borrowRepColName = "Borrower_Rep";
  var borrowRepCol = headers[0].indexOf(borrowRepColName);
  var wiColName = "Sent_WI's";
  var wiCol = headers[0].indexOf(wiColName);

  var date = Utilities.formatDate(new Date(), "MST", "M/d/yy");

  for (var i = 0; i < lr-3; i++) {
    if (values[i][wiCol] == "" && values[i][propAdCol] !== "") {
      //Get attachment, throw an error if there is no URL
      var fileUrl = values[i][atchUrlCol];
      var fileId = fileUrl.match(/[-\w]{25,}/);
      try {
        var file = DriveApp.getFileById(fileId);
        var fileAttachment = file.getAs(MimeType.PDF);
      }
      catch(err) {
        Logger.log("Message : " + err.message + " - Stacktrace: " + err.stack);
        throw "No attachment linked on row " + (i + 3);
        break;
      }
    }
  }
}

When I run the Debugger, file shows undefined. I verified that I have permission to open the URL in a browser. I also tried replacing

var file = DriveApp.getFileById(fileId);

with the actual file id as a string

var file = DriveApp.getFileById("AAAAAAAA");

and I still get the same error.

Edit: I added a Logger to the catch and got the following.

6:32:58 PM Info Message : We're sorry, a server error occurred. Please wait a bit and try again. - Stacktrace: Exception: We're sorry, a server error occurred. Please wait a bit and try again.
at wireInfoEmail (Wire Instructions Email:54:29)
at GS_INTERNAL_top_function_call.gs:1:8

0 Answers
Related