在我创建的第一张工作表中,我已有条件条件格式规则,该规则已经在使用中。但是,我希望这样,以便在创建新工作表时,这些规则也复制到新工作表中。
到目前为止,我已经执行了以下操作:
代码
/**
* Automatically applies conditional formatting to all the Sheets, based on that of the first one
**/
function autoConditionalFormat() {
// get all the sheets
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
sheets = spreadsheet.getSheets();
// get the first spreadsheet
const firstSheet = sheets[0],
conditionalFormatRules = firstSheet.getConditionalFormatRules();
sheets.forEach(function(sheet, idx) {
if (idx) {
// for some reason, this is throwing an Error: Conditional format rule cannot reference a different sheet. at [unknown function](Code:16) at autoConditionalFormat(Code:14)
sheet.setConditionalFormatRules(conditionalFormatRules);
}
})
}
结果
在创建触发器以进行更改时,运行此代码时,会发生以下错误:
Conditional format rule cannot reference a different sheet. (line 17, file "Code")
有没有一种方法,而无需对条件格式规则进行硬编码或手动创建一个完整的模板工作表,供侦听器功能简单地粘贴到新工作表上?