2
votes

I have a master spreadsheet which contains a script that makes a POST request to my server onEdit with the current spreadsheet ID.

function post2Server(){
  ss = SpreadsheetApp.getActiveSpreadsheet();

  payload                   = {};
  payload['spreadsheet_id'] = ss.getId();

  headers = {
    'Content-Type': 'application/json',
    'Accept'      : 'application/json'
  }

  options = {
    'method'     : 'post',
    'contentType': 'application/json',
    'headers' : headers,
    'payload' : JSON.stringify(payload)
  }

  res = UrlFetchApp.fetch(MY_SERVER_URL, options);
  return;
}

This function works as intended on the master sheet. Now, when I use the python google API to create a copy of this master sheet, the script copies over, however, doesn't run. I get an error saying....

Server error occurred. Please try saving the project again.

Why isn't this running? Within the copy of the spreadsheet, I even create a new function which simply logs "hello" and receive the same error. It appears that after the Python SDK copied the master sheet, no functions run. Is this a permissions issue? How can I get the script to execute in any subsequent copy of the master sheet?

2
This will be a new project. Try deleting manifest appsscript.json. You need to re-auth - TheMaster
The purpose is to have this done programatically. We can't ask our users to do this for every copy of the spreadsheet. - yesyoukenn
Well, First, Does it work manually? If so, You may try ScriptApp.invalidateAuth() to re-authorize. - TheMaster
@yesyoukenn Thank you for replying. There might be the issue for the method of copy. So can you provide the detail information including the script for copying spreadsheet you are using? If you can do, please update your question. - Tanaike
You said: Seems that no function, even something as simple as logging, executes with the error: Which means, the code in your question is irrelevant. Consider rewriting your question completely with all the information in comments with more research. Please read minimal reproducible example and How to Ask. - TheMaster

2 Answers

0
votes

Usually my approach to this kind of requirements is to use the scripts.run method of the Google Apps Script REST API. In this way you are in full control of what scripts are executed and of the parameters.

The main conditions you have to met to use this method are the following:

  • Deploy the script project as an API executable
  • Provide a properly scoped OAuth token for the execution
  • Ensure that the script and the calling application share a common Cloud Platform project

If you look for more details there is a dedicated page in the documentation that explains how to run a specific method of your Apps Script and at the bottom of it you have a sample in many different language, including python.

0
votes

Right now copying a sheet that contains a bound Google App Script using the Drive API authenticated through a Google Service Account will always render the script unusable due to a bug in the Drive API (filed here). Until that's fixed, there's no simple solution to this.

In our case, we were able to eliminate our use of the bound script by utilizing functions in Sheets, specifically the IMPORTDATA function as a means of sending web requests.