如何使用Google API将Google Sheet部署为网络应用

时间:2019-05-13 20:33:28

标签: google-apps-script web-applications google-drive-api google-sheets-api google-apps-script-api

我对Google Sheets API有一些疑问

我的GDrive上有一个Google表格文档。我可以使用Google Sheets API和Google Drive API来使用它。这样我就可以移动,更新或制作副本。

我的任务是:  1.复印一份本文件(我可以这样做)  2.将此副本发布为Web应用程序。因此,每个副本内部都有一个doGet()函数,因此可以将其发布为Web应用程序。可以手动完成,例如:发布->部署为Web应用。,但是我找不到任何API可以做到这一点。 enter image description here

  1. 获取已发布服务的网址

更新

我阅读了文档projects.deployments.create,并想创建一个新的部署(在我应该创建一个版本之前),但是我的新deployemnt没有Web访问权限,没有url等。如果我通过projects.deployments.list检查它显示:

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

如果我将通过Web界面手动进行操作,它将类似于

{
  "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 个答案:

答案 0 :(得分:1)

需要使用Apps Script API。您可以使用REST API并发出UrlFetchApp.fetch(url)请求。这是一个两步过程。首先,您必须创建一个新的Apps脚本文件,然后必须使用清单文件中的新内容更新该新的Apps脚本文件。清单文件必须具有webapp的属性,该属性指定部署的类型。

代码可能类似于以下内容:

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)


}

请参阅文档: projects.deployments.create

清单文件中指定了apps脚本项目的部署方式:

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

API不提供用于创建部署类型的配置设置。因此,通过更新清单文件将部署转变为Web App。这必须是一个两步过程。首先,创建新的项目文件,然后使用文件内容的JSON对象对其进行更新。您可以指定清单文件“ appsscript.json”的文件内容

内容必须为以下格式的JSON:

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

您需要向另一个网址发出另一个请求,并且该请求必须是PUT请求才能进行更新:

  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": "example@gmail.com",
      }
    }]
  }

  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)