1
votes

I have a little proble with Google Sheets API

I have a Google Sheet Document on my GDrive. And I can work with it using Google Sheets API and Google Drive API. So I can move it, update it or make copies.

My task is: 1. make a copy of this document (I can do this) 2. publish this copy as web app. So each copy has a doGet() functon inside so it can be published as a Web app. Manually it can be done like: Publish -> Deploy as web app.. But I can't find any API to do this. enter image description here

  1. Get an URL of published service

UPDATE

I read documentation projects.deployments.create And I maanged to create a new deployment (before that I should create a version) But my new deployemnt has no web access, no url etc. IF I check via projects.deployments.list it shows:

{
  "deploymentId": "AKfycbxVfuoeIQmumgy_Efhw12NCcqE7vqosYoxbDiKj5CT4mL_GbtybXsh1ppMIX22wQX20",
  "deploymentConfig": {
    "scriptId": "1zfjbALVe0jGbZCtqjFR0RP2-O___hR7MtAlx3biuJGXKsrKh3y1W0hMT",
    "versionNumber": 1,
    "manifestFileName": "appsscript",
    "description": "v1"
  },
  "updateTime": "2019-05-13T22:33:23.760Z"
}

And if I will do this manually via web interface it will looks like

{
  "deploymentId": "AKfycbyn3smPKxJcZwsm9SzSTtzNCAcWJzf1OVs4WTslvHo",
  "deploymentConfig": {
    "scriptId": "1zfjbALVe0jGbZCtqjFR0RP2-O___hR7MtAlx3biuJGXKsrKh3y1W0hMT",
    "manifestFileName": "appsscript"
  },
  "updateTime": "1970-01-01T00:00:00Z",
  "entryPoints": [
    {
      "entryPointType": "WEB_APP",
      "webApp": {
        "url": "https://script.google.com/macros/s/AKfycbyn3smPKxJcZwsm9SzSTtzNCAcWJzf1OVs4WTslvHo/exec",
        "entryPointConfig": {
          "access": "ANYONE_ANONYMOUS",
          "executeAs": "USER_DEPLOYING"
        }
      }
    }
  ]
}
1

1 Answers

1
votes

The Apps Script API needs to be used. You can use the REST API and make a UrlFetchApp.fetch(url) request. This is a two step process. First you must create a new Apps Script file, then you must update that new Apps Script file with new contents in the manifest file. The manifest file must have a property for webapp which is what designates the type of deployment.

The code will probably look something like the following:

function deployA_project() {
  var id, OAuthToken,options,payload,rtrn,url;

  id = ScriptApp.getScriptId();//Get the project ID of this script project
  Logger.log('id: ' + id)

  url = 'https://script.googleapis.com/v1/projects/' + id + '/deployments';//For REST

  OAuthToken = ScriptApp.getOAuthToken();

  payload = {
    "versionNumber": number,
    "manifestFileName": string,
    "description": string
  }

  options = {
    "method" : "POST",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  OAuthToken
    },
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };

  rtrn = UrlFetchApp.fetch(url,options);

  Logger.log('rtrn: ' + rtrn)


}

See the documentation: projects.deployments.create

How the apps script project is deployed is designated in the manifest file:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "webapp": {
    "access": "MYSELF",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER"
}

The API doesn't provide a configuration setting for creating the type of deployment. So, turning the deployment into a Web App is done by updating the manifest file. This must be a two step process. First, you create the new project file, then you update it with a JSON object of file content. You can designate the file content of the manifest file named "appsscript.json"

The content must be JSON in the following format:

{
  "scriptId": "scriptId",
  "files": [{
    "name": "appsscript",
    "type": "JSON",
    "source": "{\"timeZone\":\"America/New_York\", \"webapp\": {\"access\": \"MYSELF\",\"executeAs\": \"USER_DEPLOYING\"},\"exceptionLogging\":\"STACKDRIVER\"}",
    "lastModifyUser": {
      "name": "MyName",
      "email": "[email protected]",
    }
  }]
}

You need to make another request to a different url, and the request must be a PUT request in order to make an update:

  url = 'https://script.googleapis.com/v1/projects/' + id + '/deployments/' + {deploymentId}

  var newContent = {
    "scriptId": "scriptId",
    "files": [{
      "name": "appsscript",
      "type": "JSON",
      "source": "{\"timeZone\":\"America/New_York\", \"webapp\": {\"access\": \"MYSELF\",\"executeAs\": \"USER_DEPLOYING\"},\"exceptionLogging\":\"STACKDRIVER\"}",
      "lastModifyUser": {
        "name": "MyName",
        "email": "[email protected]",
      }
    }]
  }

  var requestBody = {};
  requestBody.files = newContent;

  requestBody.deploymentConfig = {
      "scriptId": string,
      "versionNumber": number,
      "manifestFileName": string,
      "description": string
    }

  options = {
    "method" : "PUT",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  OAuthToken
    },
    "contentType": "application/json",
    "payload": JSON.stringify(requestBody)
  };

  rtrn = UrlFetchApp.fetch(url,options);

  Logger.log('rtrn: ' + rtrn)