【问题标题】:Using google drive api to duplicate a worksheet使用 google drive api 复制工作表
【发布时间】:2013-05-21 10:17:24
【问题描述】:

我正在尝试使用谷歌应用脚​​本复制/复制工作表。有可能这样做吗?我无法在 API 文档中找到此类调用的任何参考。 https://developers.google.com/google-apps/spreadsheets/

【问题讨论】:

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


    【解决方案1】:

    对不起,据我所知,没有功能可以做到这一点。 Google 应用程序脚本会执行此操作。

    我是手动完成的。我创建了一个新工作表并复制了公式/数据。但是如果你在你的公式中使用 $ 会有错误,读取工作正常,但写入很痛苦,因为某些公式,例如带有 A$1 的引用不会写入工作表(由于错误)。所以你需要将它们切换到A1。你也不能复制字体/颜色等。

    我的代码是用 Java 编写的,但不容易拆分出你需要的部分。

    【讨论】:

      【解决方案2】:

      您可以使用部署为网络应用程序的谷歌应用程序脚本。 使用 HTTP GET 调用 Web 应用程序以传输电子表格 ID、要复制的工作表的名称和新工作表的名称。这是我使用的脚本:

      function doGet(e) {
      
        // get parameter from request (GET HTTP method)
        var spreadsheetID = e.parameter.spreadsheetID;
        var targetName = e.parameter.targetName;
        var sourceSheetName = e.parameter.sheetName;
      
        var response = copyWorksheet(spreadsheetID, sourceSheetName, targetName);
      
        return ContentService.createTextOutput(response);
      }
      
      
      function copyWorksheet(spreadsheetID, templateName, sheetName){
      
          // if there is a spreadsheet ID, find the spreadsheet with this ID
          if(typeof spreadsheetID == "string"){
            spreadsheet = SpreadsheetApp.openById(spreadsheetID);
          }else{
            return "failure; the spreadsheet " + spreadsheetID + " was not found";
          }
      
          // if there is a template name, find the worksheet with this name and set it as the active sheet;  (if there is no name provided, the active worksheet is duplicated)
          if(typeof templateName == "string"){
            template = spreadsheet.getSheetByName(templateName);
            if(template != null)
              spreadsheet.setActiveSheet(template);
          }
      
          // duplicate active sheet and set the new sheet as the active sheet
          newSheet = spreadsheet.duplicateActiveSheet();
      
        // rename the sheet
          if(typeof sheetName == "string"){
            sheetName.setName(sheetName);
          }
      
        return "success;" + sheetName + ";";
      }
      

      将此脚本附加到任何电子表格并进行部署。调用它使用:

      <scriptURL>?spreadsheetID=<spreadsheetID>&targetName=<targetName>&templateName=<sourceSheetName>
      

      【讨论】:

      • 所以我将此脚本附加到工作表,部署它,并使用获得的 URL 通过我通过 SignedJWTAssertionCredentials 授权的 Python 中的 httplib2.Http() 对象在我拥有的电子表格上发出请求使用正确的 Google Drive ID、范围、密钥等。但我得到一个 404,即使在检查/实验名称和 ID 之后也是如此。你知道(比我更好:P)为什么会发生这种情况吗?
      猜你喜欢
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多