The Apps Script API can update an existing script.
Whether using the API is a good idea or not, depends on how much work it would be to learn and write the code,
versus other options.
Have a Sheets template. Copy the template which will also copy the bound project.
Use a library. You still need to add the library to the bound project, and then add code to run the library.
Publish an add-on. You'll need to provide graphics and meet requirements even to publish a private add-on.
Some of the things that the Apps Script API can do:
- Create a new Apps Script file with a base manifest file.
https://developers.google.com/apps-script/api/reference/rest/v1/projects/create - Put content into an existing Apps Script file. Including updating the appsscript.json file:
https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContentAn Apps Script project can be used as the basis for different things:
- A Web App
- An Add-on - Editor Add-on or G Suite
- A library
- API Executable
- Bound to a document (Sheet, Form, Doc, Slides)
How the Apps Script file behaves is controlled through the manifest file.
The Apps Script API can be used from an Apps Script project by using the REST API and `UrlFetchApp.fetch(url,options)`
You don't need to use an OAuth library to do this from Apps Script. An
access token can be obtained with:
var accessTkn = ScriptApp.getOAuthToken()
The structure of the content to update the Apps Script file is described at:
https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent#request-bodyAn example of the code to overwrite an existing Apps Script file is:
function overWriteAppsScriptFile_(scriptId,content,theAccessTkn) {
try{
var options,payload,response,url;
if (!content) {
throw new Error('failed to pass in content');
return;
}
if (!theAccessTkn) {
theAccessTkn = ScriptApp.getOAuthToken();
}
url = "
https://script.googleapis.com/v1/projects/" + scriptId + "/content";
options = {
"method" : "PUT",
"muteHttpExceptions": true,
"headers": {
'Authorization': 'Bearer ' + theAccessTkn
},
"contentType": "application/json",//If the content type is set then you can stringify the payload
"payload": JSON.stringify(content)
};
response = UrlFetchApp.fetch(url,options);
//llz('typeof response 38',typeof response)
//llz('response.getResponseCode() 39',response.getResponseCode())
//llz('response 39',JSON.stringify(response).slice(0,45))
return response;
} catch(e) {
myErrorHandling_(e,'Error updating Apps Script file');
//llz('error',e.message)
//llz('stack',e.stack)
}
};