简体   繁体   中英

Apps Script API not pasting data into Google Sheets

I'm trying to execute the function by calling it from my Chrome Extension. I can see in my "My Executions" that the chrome extension called the function, but it's not pasting data into Google Sheets.

  • When I try to run that function from the Script Editor directly it's working.

Function Call from Chrome Extension

post({ 'url': 'https://script.googleapis.com/v1/scripts/' + SCRIPT_ID + ':run',
  'callback': executionAPIResponse,
  'token': token,
  'request': {
    'function': 'setData',
    'parameters': { 'data': JSON.parse(exec_data.value) },
  }
});

Script Editor Code

function setData(parameters) {  
  try {
    var doc = SpreadsheetApp.openById(DOC_ID);
    var sheet = doc.getSheets()[0];
    var data = parameters.data;
    sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
    return {"status":"ok", 'doc':doc.getUrl()};
  } catch(e){
    // if error return this
    Logger.log(e);
    return {"status": JSON.stringify(e)};
  }
}

I deployed the Apps Script project as "API Executable." I am wondering, is there something wrong with the way I deployed the app in Script Editor?

getActiveSpreadsheet() only works on scripts projects bounded to a spreadsheet and only to access the the spreadshet that contains the script project.

Instead of using the above method use openByUrl(url) or openById(id)

I found the mistake i was making. Actually the code was correct but while deploying as API Executable you have to always change the version to new one when you made changes to your code

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