Is it possible to programmatically change Cloud Platform Project of a container-bound script?

Viewed 61

I am essentially asking the same question from this post.

It is from a year+ ago, and it seems like it was never answered. I tried commenting to ask the original poster whether they were able to solve the problem, but my reputation to comment there is less than 50.

I am hoping that this helps bring the issue to the attention of more people too.

1 Answers

A Cloud Platform Project can not programatically be associated with an Apps Script project. There is an Apps Script API, which is capable of doing various things with Apps Script files, but the Apps Script API doesn't provide a way to associate an Apps Script project with a different Cloud project. That can only be done manually.

Some of the things that the Apps Script API can do:

An 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 manifest file can be configured to be a web app: https://developers.google.com/apps-script/manifest#webapp

So, the manifest file can be configured to make the Apps Script project a Web App and the Apps Script API can update the manifest file. Therefore, you can create an Apps Script file as a Web App programmatically. But creating the Apps Script file and configuring it through the manifest file to be a Web App doesn't publish it.

Once the manifest file is configured to define the Apps Script file as a Web App, when the Apps Script project is deployed, it automatically is deployed as a Web App.

An Apps Script file can be programmatically deployed: https://developers.google.com/apps-script/api/reference/rest/v1/projects.deployments/create

You can see the changes in "Deployments" dialog box by opening up the Deployments dialog before the project has been published as a Web App. The Deployments dialog is opened up from the code editor. If you compare the Deployments dialog content before and after the project has been published as a Web App, you'll see the difference.

If you manually publish an Apps Script file as a Web App, and then you look at the "Publish Menu" -> "Deploy From Manifest" dialog box, you'll see that a new version has been created, and you'll see an icon of the world. I believe that the world icon is the indicator of the type of deployment being a Web App.

So if you want to do this completely programmatically, there are multiple steps.

  • Create the Apps Script file
  • Update the Apps Script file with content including the appsscript.json file
  • Create a new deployment

And 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-body

An 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)
}
};

There is also gcloud commands that can be used for managing Google Cloud projects. Gcloud commands can be run from the Google Cloud Shell.

https://cloud.google.com/sdk/gcloud/reference/projects

I don't know if you can start with the Google Cloud project, and then associate a different Apps Script project to it.

Related