gpt4 book ai didi

javascript - 将表单数据提交到 Google 表格时 Google App 脚本出错

转载 作者:行者123 更新时间:2023-11-30 19:33:24 25 4
gpt4 key购买 nike

目标是与 Google 表格中的表单数据一起发送,以便记录在适当的字段中。我用了这个article ,但我的脚本给出了一个错误:

{
"result": "error",
"Error in: ": {
"message": "Invalid argument: id",
"name": "Exception",
"fileName": "Code",
"lineNumber": 20,
""smack": "\that Code:20 (handleResponse)\n\tat Code:2 (doGet)\n"
}
}

请帮助我找出我在 Google Apps 脚本中犯了什么错误,或者告诉我您如何自己诊断错误。

Google Apps 脚本:

function doGet(e){
return handleResponse(e);
}

// Enter sheet name where data is to be written below
var SHEET_NAME = "Table";

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

function handleResponse(e) {
// shortly after my original solution Google announced the LockService[1]
// this prevents concurrent access overwritting data
// [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
// we want a public lock, one that locks for all invocations
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.

try {
// next set where we write the data - you could write to multiple/alternate destinations
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName(SHEET_NAME);

// we'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
row.push(e.parameter[headers[i]]);
}
}
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally { //release lock
lock.releaseLock();
}
}

function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
}

最佳答案

您收到此错误是因为您忘记执行 Google Apps 脚本的添加。

为此 - 转到“运行”菜单并选择“设置”。

关于javascript - 将表单数据提交到 Google 表格时 Google App 脚本出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56207714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com