Google 应用程序脚本 "ExecutionResponse" 对象不包含 "result"

Google apps script "ExecutionResponse" object does not contain "result"

我正在使用 Google Scripts REST API 调用脚本。文档说 "If the script function returns successfully, [the response] field will contain an ExecutionResponse object with the function's return value as the object's result field."

但是,当响应返回时,它似乎不包含结果字段。我刚刚得到这个:

{
  "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 脚本文件非常简单。

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
}

可能您没有保存新版本的代码,或者没有发布使用最新版本的脚本。

https://developers.google.com/apps-script/guides/web#deploying_a_script_as_a_web_app