GoogleAppsScript函数在我手动运行时执行,但在作为触发器运行时失败,引用错误代码INTERNAL

我有一个链接到 Google 表格的 Google 表单。在 Google 表格中,我有一个 Google Apps Script 函数,它将输入与 Google 表单分开并逐行存储每个项目。我有一个 VLookup 设置,可以根据 Google Apps 脚本的解析从另一个电子表格中提取信息。提取信息后,我会将信息通过电子邮件发送到通过 Google 表单提供的电子邮件。我有一个触发器设置,可以在提交表单时自动运行该功能。下图是表格中的表格结果。

表单提交信息

当我在 Google 表单上选择一些选项时,触发器会起作用。一旦我选择了更多选项,大约 20+,它会返回以下错误代码:

很抱歉,JavaScript 引擎报告了意外错误。错误代码内部。

但是,当我手动运行该功能时,它可以完美执行。我不太确定是什么问题。我发现的与此问题有关的唯一其他问题是Web App 调用失败,并显示“... Error code INTERNAL” with Custom API,但我已经在使用 openByID。下面函数 onFormSubmit 中的 e 是我尝试使用表单对象的尝试,但我还没有让它起作用。我不相信这会导致问题。

//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)
}

回答

看起来这是 v8 引擎中的一个问题,现在正在调查:https :
//issuetracker.google.com/issues/184759540

要同时解决此问题,将代码从运行时 V8 切换到 DEPRECATED_ES5 应该可以解决问题。

除了错误之外,您通过 onFormSubmit 参数传递事件对象但不使用它。

例子:

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!
}

获取命名值返回一个对象,其中键是 Google 表单中的问题,值是表单提交中的答案。


以上是GoogleAppsScript函数在我手动运行时执行,但在作为触发器运行时失败,引用错误代码INTERNAL的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>