繁体   English   中英

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

[英]Google Sheets: Conditional formatting a column of cells to an adjacent cell(column)

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

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

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

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

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

以下链接是预算示例。 “手机”太高了”,“Gas/Fuel”是正确的,“Grocery”是在下面。

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

让我知道这是否适合你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM