簡體   English   中英

Google 工作表:將工作表復制到其他電子表格時出錯

[英]Google sheet: Error on copy sheet to other spreadsheet

我正在嘗試使用此代碼將工作表復制到其他電子表格中。 但是,出現錯誤:[例外:指定的工作表必須是電子表格的一部分]

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});
}

問題和解決方法:

不幸的是,似乎當源工作表的電子表格與目標電子表格不同時, 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);
}

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM