【问题标题】:How to use one conditional formatting in Google Sheets to all sheets at once?如何一次在 Google 表格中对所有表格使用一种条件格式?
【发布时间】:2018-07-26 07:15:05
【问题描述】:
我在 Google 表格中为一张表格制作了一些“条件格式”,但我需要应用到其他表格。大约有 45 张桌子,我真的不想复制粘贴它。谁能帮我解决这个问题?
【问题讨论】:
标签:
google-sheets
gs-conditional-formatting
【解决方案1】:
一个快速的解决方案是执行以下操作:
- 记录如何在第一张工作表上应用条件格式的宏
- 编辑宏,使其在每个工作表中循环(参见下面的代码示例)
- 请务必先在原始工作表的副本上执行此操作,以防出现任何问题
代码:
function autoConditionalFormat() {
// Counts how many sheets there are
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
numSheets = sheets.length;
// Loop to get name of each tab (sheet)
var tabNames = new Array()
for (var i=0; i<numSheets; i++) tabNames.push( [ sheets[i].getName() ] )
// Loops through each sheet
for (var i = 0; i < numSheets; i++) {
// Applies some conditional formatting to each sheet
SpreadsheetApp.setActiveSheet(ss.getSheetByName(tabNames[i]));
// Insert what your macro recorded here:
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A2').activate();
var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.push(SpreadsheetApp
.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('A2')])
.whenCellNotEmpty()
.setBackground('#B7E1CD')
.build());
spreadsheet.getActiveSheet()
.setConditionalFormatRules(conditionalFormatRules);
}
}
}