仅从单独的 Google 表格复制格式



我尝试仅从与当前工作表位于不同工作簿中的源工作表中复制格式。 从本质上讲,源工作表格式会发生变化,我想将对格式的更新提取到我的目标工作表(这是我将使用的当前工作表(中。 我正在使用以下代码,但在最后一行出现"无法将电子表格转换为(类("错误。 有什么见解吗?

var ss = SpreadsheetApp.openById("Source ID")
var source = ss
var destination = SpreadsheetApp.openById("Destination ID");
var range = source.getRange("N3:AE50");
range.copyFormatToRange(destination, 10, 27, 4, 50);

您要将格式从电子表格 A 复制到电子表格 B。如果我的理解是正确的,那么这个解决方法怎么样?我认为您的情况有几个答案,所以请将其视为其中之一。

正如tehhowch指出的那样,copyFormatToRange()可用于电子表格中的相同工作表。对于这种情况,我知道您想在 2 个电子表格之间使用。在我的解决方法中,我使用了以程。

将包含"源 ID"的电子表格的
  1. 源工作表复制到具有"目标 ID"的电子表格。
  2. 从复制的工作表中检索范围作为源范围。
  3. 使用copyFormatToRange()将源范围复制到目标工作表。
  4. 删除复制的工作表。

示例脚本:

var sourceA1Notation = "N3:AE50";
var destinationA1Notation = "D10"; // row=4, col=10 This is the cell of upper left for putting the source range.
var source = SpreadsheetApp.openById("Source ID");
var destination = SpreadsheetApp.openById("Destination ID");
var sourceSheet = source.getSheets()[0]; // You can also use source.getSheetByName(name)
var destinationSheet = destination.getSheets()[0]; // You can also use source.getSheetByName(name)
var copiedsheet = sourceSheet.copyTo(destination);
var sourceRange = copiedsheet.getRange(sourceA1Notation);
var destinationRange = destinationSheet.getRange(destinationA1Notation);
sourceRange.copyFormatToRange(
destinationSheet,
destinationRange.getColumn(),
destinationRange.getColumn() + sourceRange.getNumColumns() - 1,
destinationRange.getRow(),
destinationRange.getRow() + sourceRange.getNumRows() - 1
);
destination.deleteSheet(copiedsheet);

注意:

  • 当您使用它时,请输入并确认sourceA1NotationdestinationA1Notation"Source ID""Destination ID"
    • destinationA1Notation是左上角的单元格,用于放置源范围。
  • 在此示例脚本中,使用"源 ID"和"目标 ID"的每个第一页。如果要使用其他工作表,请修改此脚本。

引用:

  • copyFormatToRange((
  • getSheets((
  • getSheetByName((

如果我误解了你想要什么,我很抱歉。

相关内容

最新更新