简体   繁体   中英

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

I'm very new to coding and haven't tried anything really. I did see this is possible through conditional formatting outside of the macros but it seems it only applies to the current spreadsheet and I didn't notice a way to quickly do this for all applicable cells in every sheet.

I have a spreadsheet I use for budgeting. Column B is labeled Sub-total, this is the amount that Column D(Actually Spent) is supposed to meet but not exceed. I would like the cells in column D to be the default color until it reaches the amount from the adjacent cell in column B. If the amount goes over the number in the adjacent column B cell then i would like it to be red.

This should apply to every sheet in the workbook and i would like it to do this automatically without prompting anything other than opening the workbook.

Also, when I'm done budgeting for that pay period if I've gone over budget I change the tab's color to red, if I'm within budget the tab is black.

Coding is something I've wanted to learn and i think this is a good place to start.

The below link is an example of a budget. With "cell phone" being too high", "Gas/Fuel" being correct, and "Grocery" being under.

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

Edit: I figured out the conditional formatting.

**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

My next question would be how do I make it so this is applied to all of the sheets instead of just the current sheet?

I went the route of recording a macro while applying the conditional formatting that I wanted. It came out long and clunky but I've reduced it down to the following script that does what I want it to do quickly and efficiently. It would be cool to have this applied to all spreadsheets in the workbook but the keyboard shortcut is easy enough to use. I will just want to do this before I use each spreadsheet on my phone.

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

I reworked your code a bit and came up with this script that loops through all sheets in your spreadsheet and adds the conditional formatting to all of them at once. Basically, with getSheets() you can get an array of all the sheets in the spreadsheet, and with a for loop you can create the rules for all sheets:

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

Let me know if that works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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