简体   繁体   中英

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.

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.

I'm able to see the return using the GAS Logger inside the Google script editor but that's it. 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)); making it to sss.appendRow([createSupersteps(userIds)]); .

Based on the documentation sample code 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); gives an error 在此处输入图片说明

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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