Google Sheet 在不同工作表之间复制和粘贴值以及格式化

Google Sheet copying and paste values and formatting between different sheets

我一直在尝试制作一个脚本来在两个不同的 sheet 之间复制和粘贴数据,并且我已经阅读了该论坛上的所有 post。许多解决方案都太旧了,不再有效,而其他解决方案只会出现不同类型的错误。我已阅读 Google 文档并尝试了一些东西,但我无法使其工作。我会 post 在这里找到一个我发现并修改过的脚本,该脚本更接近我的需要,但它在某些方面失败了。我只需要值(如果可能的话,还需要格式化),我不想通过源 sheet.

中的任何公式
function copiarValores2(){
  var source = SpreadsheetApp.openById('1hWTdUNhTEohOqg-UGNGV4XwY7nDOX5Ys9piz256QeIc');
  var sourceS = source.getSheets()[0];
  var range = sourceS.getRange('A1:S230');
  var destination = SpreadsheetApp.openById('15zvklK7NAEBKQcusTwYPEmlH3BNw0t-6MlgQ1fqBuPs');
  var destSheet = destination.getSheets()[0];
  range.copyValuesToRange(destSheet,1,20,1,230)
}

运行这个脚本我有以下错误:

Exception: Target sheet and source range must be on the same spreadsheet.

我不知道如何解决这个问题。 感谢关注和帮助。

由于无法直接将范围复制到不同的点差sheet,您可以使用此解决方法:

示例代码:

function copiarValores2() {
  var source = SpreadsheetApp.openById('1PTRabYcpVY6hLNvUByj9ZJAouogc-uMCMhiexxxxx');
  var sourceS = source.getSheetByName("Sheet1");
  var destination = SpreadsheetApp.openById('1RPQ7WOTOZZSirf60kP6nULhViCdsuGHcOxxxxxx');
  var destSheet = destination.getSheetByName("Sheet1");

  //Copy source sheet to destination spreadsheet
  newSource = sourceS.copyTo(destination);
  //Get the range from the newly copied sheet
  var range = newSource.getRange('A1:S230');

  //Copy to the destination sheet, starting from cell A1
  range.copyTo(destSheet.getRange("A1"));

  //Delete copied source sheet
  destination.deleteSheet(newSource);
}

它有什么作用?

  1. 打开一个源传播sheet和select一个sheet被复制使用Spreadsheet.getSheetByName(name)
  2. 打开一个目标传播sheet和select一个sheet,其中值将使用Spreadsheet.getSheetByName(name)
  3. 粘贴
  4. 使用 Sheet.copyTo(spreadsheet) 将源 sheet 复制到目标传播sheet。此复制的 sheet 将成为您的新来源 sheet,它位于您的目标价差 sheet.

The copied sheet is named "Copy of [original name]" in your destination spreadsheet

  1. Select 您要从新来源复制的范围 sheet 然后使用 Range.copyTo(destination)
  2. 将该范围复制到目标 sheet

I used this method since you mentioned in your post that you also want to copy the formatting. You can use other copy methods available in Range Class based on your preference

  1. 最后,使用Spreadsheet.deleteSheet(sheet)
  2. 删除目标传播sheet中的新来源sheet

输出:

来源Sheet:

目的地Sheet: