如何解决Google Apps Script 函数在我手动运行时执行,但在作为触发器运行时失败,引用错误代码 INTERNAL
我有一个链接到 Google 表格的 Google 表单。在 Google 表格中,我有一个 Google Apps Script 函数,它将输入与 Google 表单分开并逐行存储每个项目。我有一个 VLookup 设置,可以根据 Google Apps 脚本的解析从另一个电子表格中提取信息。提取信息后,我会将信息通过电子邮件发送到通过 Google 表单提供的电子邮件。我有一个触发器设置,可以在提交表单时自动运行该功能。下图是表格中的表格结果。
当我在 Google 表单上选择一些选项时,触发器会起作用。一旦我选择了更多选项(大约 20 多个),它会返回以下错误代码:
很抱歉,JavaScript 引擎报告了意外错误。错误代码内部。
当我手动运行该函数时,它完美地执行。我不太确定是什么问题。我发现的与此问题有关的唯一其他问题是 Web App call fails with "... 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 表单中的问题,值是表单提交中的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。