title: Google Apps Script function executes when I run it manually, but fails when run as trigger, citing error code INTERNAL
body: <p>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.</p>
  <p><a href="https://i.stack.imgur.com/NZAfz.png" rel="noreferrer">Form submission information</a></p>
  <p>The trigger works when I select some options on Google Forms. Once I select more options, around 20+, it returns the following error code:</p>
  <blockquote>
  <p>We're sorry, the JavaScript engine reported an unexpected error. Error code INTERNAL.</p>
  </blockquote>
  <p>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 <a href="https://stackoverflow.com/questions/61967246/web-app-call-fails-with-error-code-internal-with-custom-api">Web App call fails with &quot;... Error code INTERNAL&quot; with Custom API</a>, 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.</p>
  <pre><code>//Object to store city &amp; 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(&quot;,&quot;);
    var list_cities = [];
  
    //Separates City, State into object with City &amp; State properties
    n = 0
    for (x of String(sheet.getRange('F' + lastRow).getValue()).split(&quot;,&quot;)) {
      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 = [&quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;];
    
    //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) + &quot;:&quot; + x + String(row-1));
      sourceRange.autoFill(destination, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
    }
  
    SpreadsheetApp.flush();
  
    //Extracts only completed sheet
    var url = &quot;https://docs.google.com/&quot;;
    var params = {
      method      : &quot;get&quot;,
      headers     : {&quot;Authorization&quot;: &quot;Bearer &quot; + ScriptApp.getOAuthToken()},
      muteHttpExceptions: true
    };
    var blob = UrlFetchApp.fetch(url, params).getBlob();
    blob.setName(SpreadsheetApp.getActive().getName() + &quot;.xlsx&quot;);
  
    //Set email information and send email
    var message = {
      to: email,
      name: 'Name',
      subject: company + &quot; Subject&quot;,
      body: &quot;Body text&quot;,
      attachments: blob
    }
    MailApp.sendEmail(message)
  }
  </code></pre>