简体   繁体   English

无法从Google表格中的自定义功能输出或保存API响应

[英]Unable to output or save API response from custom function in Google sheet

I have the following Google App Script that ultimately returns a matrix but I am able to alter rows and use the appendRow method because I am trying to call it from a custom function. 我有以下Google App脚本,最终会返回一个矩阵,但是我可以更改行并使用appendRow方法,因为我试图从自定义函数调用它。

Consulting other related questions on stackoverflow and the GAS documentation whatever a custom function returns should be displayed in the cell it was called from. 有关自定义函数返回的内容,请咨询有关堆栈溢出和GAS文档的其他相关问题,并应在调用它的单元格中显示。

I'm able to see the return using the GAS Logger inside the Google script editor but that's it. 我可以在Google脚本编辑器中使用GAS Logger看到返回的信息,仅此而已。 How can I output to the sheet instead or otherwise save and export this response? 如何将其输出到工作表,或者保存并导出此响应?

The sample response looks like: 样本响应如下所示:

[[diy_move, forward_mail, update_account, renters_insurance, recommended_provider, utility, home_service, offer, share],
 [forward_mail, update_account, utility, home_service, offer, share]]

 // Get current row function getActiveRow() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); var activeRow = sheet.getActiveCell().getRow(); return activeRow; } // call updater api function getSteps() { var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues(); var userIds = []; var sheet = SpreadsheetApp.getActiveSheet(); var ss = SpreadsheetApp.getActiveSpreadsheet(); var sss = ss.getSheets()[0]; var range = sheet.getRange(getActiveRow(), 1, 1, 1); var id = range.getValues(); function first(list) { return list[0]; } function getProperty(property) { return function (item) { return item[property]; } } var getCode = getProperty('code'); var userIds = values.map(first); var headers = { // auth "Content-Type": "application/json", "app": "company", "uid": "phong@updater.com", "client": "*****", "access-token": "*****" }; var options = { "method": "GET", "headers": headers //"muteHttpExceptions": true }; function callApi(id) { var url = "http://api.updater.com/v2/item_definitions?user_id=" + id; var response = UrlFetchApp.fetch(url, options) var json = response.getContentText(); var data = JSON.parse(json); return data.map(getCode); } function createSupersteps(ids) { return ids.map(callApi); } Logger.log(createSupersteps(userIds)); sss.appendRow(createSupersteps(userIds)); //createSupersteps(userIds) } 

Try adding [] to sss.appendRow(createSupersteps(userIds)); 尝试将[]添加到sss.appendRow(createSupersteps(userIds)); making it to sss.appendRow([createSupersteps(userIds)]); 使它成为sss.appendRow([createSupersteps(userIds)]); .

Based on the documentation sample code appendRow(rowContents) 基于文档样本代码appendRow(rowContents)

Appends a row to the spreadsheet. 在电子表格上追加一行。 This operation is atomic; 此操作是原子操作; it prevents issues where a user asks for the last row, and then writes to that row, and an intervening mutation occurs between getting the last row and writing to it. 它可以防止出现以下问题:用户要求最后一行,然后写入该行,并且在获取最后一行并写入之前发生了中间的突变。

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // Appends a new row with 3 columns to the bottom of the
 // spreadsheet containing the values in the array
 sheet.appendRow(["a man", "a plan", "panama"]);

Here is my sample code: 这是我的示例代码:

function insert(){
 var ss = SpreadsheetApp.openById('FILE_ID');
 var sheet = ss.getSheets()[0];
 var sample = "sample";
 // Appends a new row with 3 columns to the bottom of the
 // spreadsheet containing the values in the array
 sheet.appendRow([sample]);
}

Trying to add using sheet.appendRow(sample); 尝试使用sheet.appendRow(sample);添加; gives an error 给出一个错误 在此处输入图片说明

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM