【问题标题】:Google sheet: Error on copy sheet to other spreadsheetGoogle 工作表:将工作表复制到其他电子表格时出错
【发布时间】:2021-10-23 04:34:25
【问题描述】:

我正在尝试使用此代码将工作表复制到其他电子表格中。但是,有错误: [例外:指定的工作表必须是电子表格的一部分]

function copy_to_A() {
  
 var source = SpreadsheetApp.getActiveSpreadsheet();
 var destination = SpreadsheetApp.openById(Bestwise_id);
 var tempsheet = source.getSheetByName('Bestwise-tocopy');

 destination.insertSheet('New Sheet Name', 0, {template: tempsheet});
}

【问题讨论】:

  • 异常发生在哪一行?这里缺少很多上下文。

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


【解决方案1】:

问题和解决方法:

不幸的是,当源工作表的电子表格与目标电子表格不同时,insertSheet 似乎不能用于其他电子表格。这似乎是当前的规范。那么当你想使用destination.insertSheet('New Sheet Name', 0, {template: tempsheet});时,下面的变通方法怎么样?此变通方法的流程如下。

  1. tempsheet 工作表从源电子表格复制到目标电子表格。
  2. 使用insertSheet('New Sheet Name', 0, {template: tempsheet}),使用复制的工作表。
  3. 删除复制的工作表。

当这个流程反映到你的脚本中时,它变成如下。

修改脚本:

function copy_to_A() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var destination = SpreadsheetApp.openById(Bestwise_id);
  var tempsheet = source.getSheetByName('Bestwise-tocopy');
  
  // 1. Copy `tempsheet` sheet from source Spreadsheet to destination Spreadsheet.
  var copiedSheet = tempsheet.copyTo(destination);
  
  // 2. Using `insertSheet('New Sheet Name', 0, {template: tempsheet})`, the copied sheet is used.
  destination.insertSheet('New Sheet Name', 0, { template: copiedSheet });
  
  // 3. Delete the copied sheet.
  destination.deleteSheet(copiedSheet);
}

参考资料:

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2023-03-15
    • 2020-03-09
    • 2016-10-08
    相关资源
    最近更新 更多