首页 > 解决方案 > 如何以编程方式将脚本文件从一个 google 工作表复制到另一个工作表?

问题描述

我为客户编写了一个应用程序,但大约有一百张需要完全相同的代码。手动将代码复制/粘贴到每个工作表的脚本编辑器中是否有捷径?

标签: google-apps-script

解决方案


这是一个示例:

我怀疑这是防弹的,所以它可能需要一些额外的工作,但它似乎对我有用。

它会启动一个对话框,允许您输入源和目标 ID 以及要复制的文件。请注意,目的地不能有同名的文件。

function appendSelectedFilesDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('appendSelected1'), 'Append Selected Files');
}

function appendSelectedFilesIntoAnotherProject(obj) {
  const srcProjectId = obj.srcProjectId;
  const dstProjectId = obj.dstProjectId;
  const srcFileNames = obj.srcFileNames;
  var all = true;
  var fA = [];
  if (srcFileNames.length > 0) {
    all = false;
    fA = String(srcFileNames).split(',').map(n => n.trim());
  }
  if (srcProjectId && dstProjectId) {
    const base1 = "https://script.googleapis.com/v1/projects/"
    const url1 = base1 + srcProjectId + "/content";
    const url2 = base1 + dstProjectId + "/content";
    const options1 = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
    const res1 = UrlFetchApp.fetch(url1, options1);
    const data1 = JSON.parse(res1.getContentText());
    const srcfiles = data1.files;
    const options2 = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
    const res2 = UrlFetchApp.fetch(url2, options2);
    const data2 = JSON.parse(res2.getContentText());
    const dstFiles = data2.files;
    srcfiles.forEach(file => {
      if (file.source && file.name) {
        if (!all) {
          if (fA.indexOf(file.name) > -1) {
            dstFiles.push(file);
          }
        } else {
          dstFiles.push(file);
        }
      }
    });
    const request = { "files": dstFiles };
    const base = "https://script.googleapis.com/v1/projects/"
    const url = base + dstProjectId + "/content"
    const options = { "method": "put", "request": request, "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() }, "contentType": "application/json", "payload": JSON.stringify(request) };
    const resp = UrlFetchApp.fetch(url, options);
    console.log(resp);
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput('<textarea rols="12" cols="60">' + resp + '</textarea>'), "Update Script Project");
  }
  return { "message": "Process Complete" };//minimally to remove the dialog
}

html:

!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>input{margin:2px 5px 2px 0;}</style>
  </head>
  <body>
    <form>
      <input type="text" id="src" name="srcProjectId" size="60" placeholder="Enter Source Project Id" />
      <br /><input type="text" id="dst" name="dstProjectId" size="60" placeholder="Enter Destination Project Id" />
      <br /><input type="text" id="names" name="srcFileNames" size="60" placeholder ="Enter Desired Files separated by commas or nothing if you want all files." />
      <br /><input type="button" value="Submit" onClick="process(this.parentNode);" />
    </form>
    <script>
      function process(obj) {
        google.script.run
        .withSuccessHandler(function(obj){google.script.host.close();})
        .appendSelectedFilesIntoAnotherProject(obj);
        console.log('Form:',JSON.stringify(obj));
      }
    </script>
  </body>
</html>

推荐阅读