应用“行带"主题范围 [英] Apply a "row banding" theme to a range

查看:94
本文介绍了应用“行带"主题范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a beginner to Google Apps Script but use it to automate some simple repeating tasks. I have several spreadsheets I am copying content on a weekly basis and export them as an .xls file that I send to my client.

我正在尝试对从另一张纸复印的范围应用交替的颜色,但是我完全卡住了.如何使用applyRowBanding方法正确设置bandingTheme?在代码的最后一行中应该使用什么正确的语法?

I am trying to apply alternating colors to a range I copy from another sheet but I completely got stuck. How to correctly set bandingTheme with the applyRowBanding method? What is the right syntax I should use in the last line of my code?

我的代码:

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

推荐答案

如果您的方法参数bandingTheme是列出的枚举之一

If your method argument bandingTheme is one of the enums listed here, you can simply apply it, using the apply___Banding(BandingTheme theme) method signature:

target.applyRowBanding(bandingTheme);

根据文档,以上内容等同于此行:

The above is equivalent to this line, per documentation:

target.applyRowBanding(bandingTheme, true, false);

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

(In other words, the default behavior is to color the header but not the footer, in addition to alternating row colors.)

您可以确保以前没有现有的主题(在任何给定时间都只能显示一种交替的颜色-无论是列还是行中的颜色-否则都会引发错误).

You can ensure no existing themes were previously present (only a single kind of alternating colors - be it from columns OR rows - can be present at any given time, else an error is thrown).

target.getBandings().forEach(function (banding) {
  banding.remove();
});
/**
 * set the new banding theme
 * ....
 */

如果要设置自定义带状主题,可以从主题设计之一开始进行设置.请注意,apply___Banding方法返回 Banding 他们应用的对象.如果绑定此返回值(或链接方法),则可以使用其返回值类来修改它.

If you wanted to set a custom banding theme, you can do so by starting from one of the theme designs. Note that the apply___Banding methods return the Banding object that they applied. If you bind this return value (or chain the methods), then you can modify it using its class methods.

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

请注意,无法为行带状主题中的非标题列设置颜色.同样,用于在列带状主题中设置非标题行的颜色.

Note that setting colors for non-header columns in a row-banding theme doesn't work. Likewise for setting non-header row colors in a column-banding theme.

如果您的bandingTheme参数不是主题枚举之一,那么您将不得不提供有关其主题的更多详细信息,以便获得有助于将其转换为可用的Spreadsheet Service方法的答案.

If your bandingTheme argument isn't one of the theme enums, then you will have to provide more details about what it is in order to get answers that help you convert it into the available Spreadsheet Service methods.

这篇关于应用“行带"主题范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆