谷歌工作表:有条件地将一列单元格格式化为相邻的单元格(列)



我对编码很陌生,还没有真正尝试过任何东西。我确实通过宏之外的条件格式看到了这一点,但它似乎只适用于当前的电子表格,而且我没有注意到对每张工作表中所有适用的单元格快速执行这一操作的方法。

我有一个用于预算的电子表格。B列标记为小计,这是D列(实际支出(应达到但不超过的金额。我希望D列中的单元格是默认颜色,直到它达到B列中相邻单元格的数量。如果数量超过了B列相邻单元格中的数字,那么我希望它是红色。

这应该适用于工作簿中的每一张工作表,我希望它能自动执行此操作,而不提示打开工作簿以外的任何其他操作。

此外,当我完成该支付期的预算时,如果我超出了预算,我会将选项卡的颜色更改为红色,如果我在预算范围内,则选项卡为黑色。

编码是我一直想学习的东西,我认为这是一个很好的起点。

下面的链接是一个预算示例。用";"蜂窝电话";"太高""气体/燃料";是正确的,并且";杂货店;处于压力之下。

https://docs.google.com/spreadsheets/d/17lUuwdjfPz17_gkckofgW8pnqGMvBlqeyN8d8xq2HxY/edit?usp=sharing

编辑:我搞清楚了条件格式

**Red:** Custom formula is =D3>1*B3 for range D3:D72
**Empty:** Cell is empty for range D3:D72
**Green:** Custom formula is =D3=B3 for range D3:D72

我的下一个问题是如何将其应用于所有工作表,而不仅仅是当前工作表

我在应用我想要的条件格式时录制了一个宏。它长而笨重,但我已经把它简化为下面的脚本,它可以快速高效地完成我想要的任务。将其应用于工作簿中的所有电子表格会很酷,但键盘快捷键使用起来很简单。我只想在使用手机上的每个电子表格之前做这件事。

function ZeroBalance1() {
var spreadsheet = SpreadsheetApp.getActive();
var conditionalFormatRules = 
spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, 
SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('D3:D72')])
.whenFormulaSatisfied('=D3>1*B3')
.setBackground('#FF0000')
.build());
conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('D3:D72')])
.whenCellEmpty()
.build());
conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('D3:D72')])
.whenFormulaSatisfied('=D3=B3')
.setBackground('#00FF00')
.build());
spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
};

我重新编写了您的代码,并提出了这个脚本,该脚本可以在电子表格中的所有工作表中循环,并一次为所有工作表添加条件格式。基本上,使用getSheets((可以获得电子表格中所有工作表的数组,使用for循环可以为所有工作表创建规则:

function ZeroBalance() {
var spreadsheet = SpreadsheetApp.getActive();
var sheets = spreadsheet.getSheets();
var column = 4;
// Loop through each sheet in the file
for(var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var range = sheet.getRange('D3:D72');
// Create conditional rules
var turnRed = SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.whenFormulaSatisfied('=D3>1*B3')
.setBackground('#FF0000')
.build();
var turnDefault = SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.whenCellEmpty()
.build();
var turnGreen = SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.whenFormulaSatisfied('=D3=B3')
.setBackground('#00FF00') // Green
.build();
var conditionalFormatRules = [turnRed, turnDefault, turnGreen];
// Add conditional rules to the sheet
sheet.setConditionalFormatRules(conditionalFormatRules);
}
}

如果这对你有用,请告诉我。

最新更新