Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script function executes when I run it manually, but fails when run as trigger, citing error code INTERNAL [closed]

I have a Google Forms linked to a Google Sheets. Within the Google Sheets, I have an Google Apps Script function that separates the input from Google Forms and stores each item row by row. I have a VLookup setup to pull information from another spreadsheet, based on the parsing from the Google Apps Script. Once the information is pulled, I then email the information to the email provided through Google Forms. I have a trigger setup to automatically run the function when the form is submitted. The picture below is the form results in the sheet.

Form submission information

The trigger works when I select some options on Google Forms. Once I select more options, around 20+, it returns the following error code:

We're sorry, the JavaScript engine reported an unexpected error. Error code INTERNAL.

When I run the function manually though, it executes perfectly. I'm not quite sure what the issue is. The only other question with this issue that I've found is Web App call fails with "... Error code INTERNAL" with Custom API, but I'm already using openByID. The e in the function onFormSubmit below is my attempt at using the form object, but I haven't gotten that to work. I don't believe this causes the problem though.

//Object to store city & state in one object
function city (city, state) {
  this.city = city;
  this.state = state;
}

function onFormSubmit (e) {
  // Set active sheet and store form information in variables
  //This function runs based off a formSubmit trigger
  var sheet = SpreadsheetApp.openById('sampleID'); 
  SpreadsheetApp.setActiveSheet(sheet.getSheets()[0]);
  var lastRow = String(sheet.getLastRow());
  var email = String(sheet.getRange('D' +lastRow).getValue());
  var name = String(sheet.getRange('B' + lastRow).getValue());
  var company = String(sheet.getRange('C' + lastRow).getValue());
  var state = String(sheet.getRange('E' + lastRow).getValue()).split(",");
  var list_cities = [];

  //Separates City, State into object with City & State properties
  n = 0
  for (x of String(sheet.getRange('F' + lastRow).getValue()).split(",")) {
    if (n == 0) {
      var city_name = x.trim();
      n = 1;
    }
    else {
      n = 0;
      var temp = new city(city_name, x.trim());
      list_cities.push(temp);
    }
  }

  //Moves to next sheet
  SpreadsheetApp.setActiveSheet(sheet.getSheets()[1]);

  //Clear Form tab
  lastRow = String(SpreadsheetApp.getActive().getLastRow());
  SpreadsheetApp.getActiveSheet().getRangeList(['A2:B2', 'A3:F' + lastRow]).clear();
  
  // Initializes first row
  var row = 2;
 
  //Fills in State column
  for (x of state) {
    SpreadsheetApp.getActiveSheet().getRange(row, 1).setValue(x.trim());
    row = row + 1;
  }
  
  //Fills in City column
  for (x of list_cities) {
    SpreadsheetApp.getActiveSheet().getRange(row,1).setValue(x.state);
    SpreadsheetApp.getActiveSheet().getRange(row, 2).setValue(x.city);
    row = row + 1
  }

  //List of cell column headers
  var letters = ["C", "D", "E", "F"];
  
  //Copies down formula for each column
  for (x of letters) {
    var sourceRange = SpreadsheetApp.getActiveSheet().getRange(x + String(2));
    var destination = SpreadsheetApp.getActiveSheet().getRange(x + String(2) + ":" + x + String(row-1));
    sourceRange.autoFill(destination, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  }

  SpreadsheetApp.flush();

  //Extracts only completed sheet
  var url = "https://docs.google.com/";
  var params = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true
  };
  var blob = UrlFetchApp.fetch(url, params).getBlob();
  blob.setName(SpreadsheetApp.getActive().getName() + ".xlsx");

  //Set email information and send email
  var message = {
    to: email,
    name: 'Name',
    subject: company + " Subject",
    body: "Body text",
    attachments: blob
  }
  MailApp.sendEmail(message)
}
like image 995
googlinghowtogoogle Avatar asked Apr 07 '21 23:04

googlinghowtogoogle


1 Answers

Looks like this is a problem in the v8 engine and is being looked into now: https://issuetracker.google.com/issues/184759540

To fix this issue in the meantime switching your code from runtime V8 to DEPRECATED_ES5 should fix the problem.

Bug aside, you're passing the event object through the onFormSubmit parameter but not using it.

Example:

function onFormSubmit(e) {
  let response = e.namedValues;

  var email = response['Email:'];
  var name = response['Name:'];
  var company = response['Company:'];
  var state = response['State:'];

  //Remaining code below!
}

Getting the named values returns a object where the keys are the questions in the Google Form and the values are the answers in the form submission.

like image 191
vaught-dawson Avatar answered Oct 11 '22 18:10

vaught-dawson