簡體   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