将谷歌脚本项目从电子表格复制到另一个 [英] Copy a google script project from a Spreadsheet to another

查看:24
本文介绍了将谷歌脚本项目从电子表格复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于如何使用脚本将工作表复制到 Google 电子表格中的另一个电子表格的答案,例如 这个 copyTo 方法.

I've seen many answers about how to use scripts to copy sheets to another Spreadsheet in Google Spreadsheets, such as this copyTo method.

但现在我有一个包含许多选项卡的大电子表格,我创建了一个新版本,并在绑定脚本中进行了更新.这个新表中的数据是测试数据.所以这就是我所拥有的:

But now I have a big spreadsheet with many tabs, and I created a new version of it with updates in the bound scripts. The data in this new sheet is test data. So this is what I have:

  • 旧电子表格文件(一致数据)/旧脚本项目(过时)
  • 新电子表格(测试数据)/新脚本项目(当前)
  • Old spreadsheet file (consistent data) / Old script project (obsolete)
  • New spreadsheet (test data) / New script project (current)

然后我需要将新脚本项目复制到旧电子表格(具有一致数据).

I need then to copy the new script project to the old spreadsheet (which has the consistent data).

我知道我可以努力复制每张纸,但这真的不是这里的问题(除了还会给命名范围带来很多麻烦).问题是如何将脚本项目复制到另一个电子表格.

I know I could make an effort to copy each sheet instead, but that is really not the question here (besides also creating lots of trouble with named ranges). The question is how to copy the script project to another spreasheet.

推荐答案

您还在寻找这个问题的解决方案吗?如果你仍然这样做,这个答案怎么样?最近,添加了 Google Apps Script API.这样,用户就可以轻松管理 GAS 项目文件.此示例脚本使用此 API.

Are you looking for the solution of this question yet? If you still do, how about this answer? Recently, Google Apps Script API was added. By this, users got to be able to easily manage GAS project files. This sample script uses this API.

要使用此示例,请执行以下安装流程.

In order to use this sample, please do the following installation flow.

  • 在 API 控制台启用 Google Apps Script API.
    • 如果您已经打开了脚本编辑器,您可以通过 这个链接.
    • 在脚本编辑器上,文件 -> 项目属性 -> 范围
    • 复制范围.
    • 在脚本编辑器上,查看 -> 显示清单文件
    • 添加 "oauthScopes": ["https://www.googleapis.com/auth/script.projects", "https://www.googleapis.com/auth/script.external_request", "### 其他范围 ###"] 到 appsscript.json,并保存.
      • 如果您的脚本需要其他范围,请在此处添加.
      • On script editor, View -> Show manifest file
      • Add "oauthScopes": ["https://www.googleapis.com/auth/script.projects", "https://www.googleapis.com/auth/script.external_request", "### other scopes ###"] to appsscript.json, and save.
        • If your script needs other scopes, please add them here.
        1. 检索源项目的文件名.
        2. 检索源项目.
        3. 在 Google 文档中创建新项目(绑定脚本).
        4. 通过检索到的源项目更新创建的新项目.

        通过这些流程,源电子表格的项目(绑定脚本)被复制到目标电子表格.

        By these flow, the project (bound script) of source spreadsheet is copied to the destination spreadsheet.

        在启动此示例之前,请输入源电子表格的项目 ID 和目标电子表格 ID.

        Before launch this sample, please input the project ID of source spreadsheet and the destination spreadsheet ID.

        function main() {
          var srcProjectId = "### project ID ###"; // Source project ID
          var dstGoogleDocsId = "### file ID of Google Docs ###"; // Destination spreadsheet ID
        
          var baseUrl = "https://script.googleapis.com/v1/projects";
          var accessToken = ScriptApp.getOAuthToken();
        
          // Retrieve filename of bound-script project.
          var srcName = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + srcProjectId, {
            method: "get",
            headers: {"Authorization": "Bearer " + accessToken}
          }).getContentText()).title;
        
          // Retrieve bound-script project.
          var obj = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
            method: "get",
            headers: {"Authorization": "Bearer " + accessToken}
          }).getContentText();
        
          // Create new bound script and retrieve project ID.
          var dstId = JSON.parse(UrlFetchApp.fetch(baseUrl, {
            method: "post",
            contentType: 'application/json',
            headers: {"Authorization": "Bearer " + accessToken},
            payload: JSON.stringify({"title": srcName, "parentId": dstGoogleDocsId})
          }).getContentText()).scriptId;
        
          // Upload a project to bound-script project.
          var res = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + dstId + "/content", {
            method: "put",
            contentType: 'application/json',
            headers: {"Authorization": "Bearer " + accessToken},
            payload: obj
          }).getContentText());
        }
        

        注意:

        • 此脚本将源项目复制为新项目.所以如果谷歌文档中有一些绑定的脚本项目,这个脚本不会影响现有的脚本.
          • 但是,如果您使用此示例 sceipt,请首先使用新项目和新 Google 文档进行测试.借此,请理解此脚本的工作.
          • 如果我误解了你的问题,我很抱歉.

            If I misunderstand your question, I'm sorry.

            在 2019 年 4 月 8 日,Google Apps 脚本项目的规范发生了变化.请在 Google 云平台项目中查看详细信息.当您将 Google Apps Script API 与 2019 年 4 月 8 日之后创建的 GAS 项目一起使用时,请创建一个新的云平台项目并将 GAS 项目链接到创建的云平台项目.(在脚本编辑器,资源 --> 云平台项目)这样就可以使用上面的流程了.

            At April 8, 2019, the specification of Google Apps Script Project was changed. Please see the detail at Google Cloud Platform Projects. When you use Google Apps Script API with the GAS project created after At April 8, 2019, please create a new Cloud Platform Project and link the GAS project to the created Cloud Platform Project. (At script editor, Resources --> Cloud Platform project) By this, the above flow can be used.

            这篇关于将谷歌脚本项目从电子表格复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆