0

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?

Tanveer Jan
  • 61
  • 2
  • 2
  • 11
  • In your script of Chrome side, ``setData`` is called as a function. But in the GAS side, the function name is ``setSheet``. How about this situation? And when you ran the script of Chrome side, if the error occurs, can you show it? – Tanaike Mar 31 '19 at 22:00
  • @Tanaike Sorry i edited the file. The function is also set to setData in script editor. There is no error on both side and true response is returned This is the response that is recieved on chrome side {"done":true,"response":{"@type":"type.googleapis.com/google.apps.script.v1.ExecutionResponse"}} – Tanveer Jan Apr 01 '19 at 13:31
  • Check your Stackdriver logs. – tehhowch Apr 01 '19 at 13:43
  • @tehhowch checked both Logger and Stackdriver, there is no error is both – Tanveer Jan Apr 01 '19 at 13:52

2 Answers2

0

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)

Rubén
  • 34,714
  • 9
  • 70
  • 166
0

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

Tanveer Jan
  • 61
  • 2
  • 2
  • 11