我使用Google Scripts REST API调用脚本。文档说明"如果脚本函数成功返回,[response]字段将包含一个ExecutionResponse对象,其函数的返回值为对象的结果字段。"
但是,当响应返回时,它似乎不包含结果字段。我得到这个:
{
"name": "CleanCSV",
"done": true,
"response": {
"@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse"
}
}
以下是调用API调用的代码:
function callScript(SCRIPT_ID, SHEET_ID, token) {
var xhr = new XMLHttpRequest();
xhr.open("POST", 'https://script.googleapis.com/v1/scripts/' + SCRIPT_ID + ':run', true);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
console.log(xhr.response); //this is what I pasted above
};
xhr.onerror = function() {
console.log("D'Oh! That didn't work. Please try again");
};
var body = {};
body.function = 'CleanCSV';
body.parameters = [SHEET_ID];
xhr.send(JSON.stringify(body));
};
Google Script文件非常简单。
function CleanCSV(sid) {
//identify the sheet
var ss = SpreadsheetApp.openById(sid);
var sheet = ss.getSheets()[0];
//...
//do some stuff to the sheet
//...
return "hi there";
//in reality, this will return some data
}
答案 0 :(得分:1)
您可能尚未保存新版本的代码或未使用该最新版本发布该脚本。
请参阅https://developers.google.com/apps-script/guides/web#deploying_a_script_as_a_web_app