将“行带”主题应用于范围

Eug*_*nov 3 syntax enums google-sheets google-apps-script

我是 Google Apps 脚本的初学者,但使用它来自动执行一些简单的重复任务。我有几个电子表格,每周都会复制内容并将它们导出为.xls文件发送给客户。

我试图将交替颜色应用于从另一张纸复制的范围,但我完全陷入困境。如何正确设置bandingTheme方法applyRowBanding?我应该在代码的最后一行使用什么正确的语法?

我的代码:

function copyRange (SourceSSID, SourceRange, TargetSheetName, bandingTheme) {
  var sheetSource = SpreadsheetApp.openById(SourceSSID);
  var sheetTarget = SpreadsheetApp.openById("bla-bla");
  var source =  sheetSource.getRange(SourceRange);
  var target_ss = sheetTarget.getSheetByName(TargetSheetName);
  var values = source.getValues();
  var target = target_ss.getRange(1, 1, values.length, values[0].length);
  target.clear();

  target.setValues(values);

  target.applyRowBanding ();
}
Run Code Online (Sandbox Code Playgroud)

teh*_*wch 5

如果您的方法参数是此处bandingTheme列出的枚举之一,您可以使用方法签名简单地应用它:apply___Banding(BandingTheme theme)

target.applyRowBanding(bandingTheme);
Run Code Online (Sandbox Code Playgroud)

根据文档,上面的内容相当于这一行:

target.applyRowBanding(bandingTheme, true, false);
Run Code Online (Sandbox Code Playgroud)

(换句话说,除了交替行颜色之外,默认行为是对页眉而不是页脚进行着色。)

您可以确保以前不存在任何现有主题(在任何给定时间只能存在一种交替颜色(无论是来自列还是行),否则会引发错误)。

target.getBandings().forEach(function (banding) {
  banding.remove();
});
/**
 * set the new banding theme
 * ....
 */
Run Code Online (Sandbox Code Playgroud)

如果您想设置自定义条带主题,可以从其中一种主题设计开始。请注意,这些apply___Banding方法返回Banding它们应用的对象。如果绑定此返回值(或链接方法),则可以使用其类方法对其进行修改。

const newBanding = target.applyRowBanding(SpreadsheetApp.BandingTheme.BLUE);
// newBanding is now a Banding that was instantiated with the "Blue" template.
// Color the header column:
newBanding.setHeaderColumnColor('teal');

// Equivalent:
target.applyRowBanding(SpreadsheetApp.BandingTheme.BLUE).setHeaderColumnColor('teal');
Run Code Online (Sandbox Code Playgroud)

请注意,在行带主题中设置非标题列的颜色不起作用。同样,在列带主题中设置非标题行颜色。


如果您的bandingTheme参数不是主题枚举之一,那么您将必须提供有关它是什么的更多详细信息,以便获得帮助您将其转换为可用的电子表格服务方法的答案。