我正在尝试使用以下代码将表单发布到Google Spreadsheet:
function doGet(e){
return handleResponse(e);
}
// Enter sheet name where data is to be written below
var SHEET_NAME = "orderSheet";
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
function handleResponse(e) {
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.
try {
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName(SHEET_NAME);
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]]);
}
}
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
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());
}
因此,我发现问题是由于电子表格名称不匹配而发生的,但一切似乎都还不错。 SHEET_NAME设置为“ orderSheet”,因此名称为spreadSheet。 start()函数成功运行并部署了应用。
我得到的完整答复:
{
"result": "error",
"error": {
"message": "Cannot call method \"getRange\" of null.",
"name": "TypeError",
"fileName": "macros",
"lineNumber": 28,
"stack": "\tat macros:28 (handleResponse)\n\tat macros:5 (doGet)\n"
}
}
答案 0 :(得分:1)
在我看来,“ SHEET_NAME”令人困惑:这不是屏幕快照中显示的电子表格的名称!它必须是底部工作表的名称(如某些“标签”)。