條件式格式設定

Google Sheets API 可讓您在試算表中建立及更新條件式格式設定規則。只有特定格式類型 (粗體、斜體、刪除線、前景色和背景顏色) 可透過條件式格式設定控制。本頁面的範例說明如何使用 Google Sheets API 執行常見的條件式格式設定作業。

這些範例以 HTTP 要求的形式呈現,因此不限語言。如要瞭解如何使用 Google API 用戶端程式庫,以不同語言實作批次更新,請參閱「更新試算表」一文。

在這些範例中,預留位置 SPREADSHEET_IDSHEET_ID 表示您提供這些 ID 的位置。您可以在試算表網址中找到試算表 ID。您可以使用 spreadsheets.get 方法取得工作表 ID。範圍是使用 A1 標記法指定。例如 Sheet1!A1:D5。

在整列中新增條件式顏色漸層

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何使用 AddConditionalFormatRuleRequest 為試算表的第 10 和 11 列建立新的漸層條件式格式規則。第一條規則指出,該列的儲存格會根據值設定背景顏色。資料列中的最低值會以深紅色標示,最高值則以亮綠色標示。其他值的顏色會經過內插處理。第二條規則的作用相同,但會使用特定數值決定漸層端點 (以及不同顏色)。要求會使用 sheets.InterpolationPointType 做為 type

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startRowIndex": 9,
              "endRowIndex": 10,
            }
          ],
          "gradientRule": {
            "minpoint": {
              "color": {
                "green": 0.2,
                "red": 0.8
              },
              "type": "MIN"
            },
            "maxpoint": {
              "color": {
                "green": 0.9
              },
              "type": "MAX"
            },
          }
        },
        "index": 0
      }
    },
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startRowIndex": 10,
              "endRowIndex": 11,
            }
          ],
          "gradientRule": {
            "minpoint": {
              "color": {
                "green": 0.8,
                "red": 0.8
              },
              "type": "NUMBER",
              "value": "0"
            },
            "maxpoint": {
              "color": {
                "blue": 0.9,
                "green": 0.5,
                "red": 0.5
              },
              "type": "NUMBER",
              "value": "256"
            },
          }
        },
        "index": 1
      }
    },
  ]
}

要求完成後,套用的格式規則會更新試算表。由於第 11 列的漸層已將上限點設為 256,因此高於該值的任何值都會採用上限點顏色:

新增漸層格式食譜結果

為一組範圍新增條件式格式設定規則

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何使用 AddConditionalFormatRuleRequest 為試算表的 A 欄和 C 欄建立新的條件式格式規則。這項規則規定,如果儲存格的值為 10 以下,背景顏色就會變更為深紅色。這項規則會插入索引 0,因此優先順序高於其他格式規則。要求會使用 ConditionType 做為 BooleanRuletype

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startColumnIndex": 0,
              "endColumnIndex": 1,
            },
            {
              "sheetId": SHEET_ID,
              "startColumnIndex": 2,
              "endColumnIndex": 3,
            },
          ],
          "booleanRule": {
            "condition": {
              "type": "NUMBER_LESS_THAN_EQ",
              "values": [
                {
                  "userEnteredValue": "10"
                }
              ]
            },
            "format": {
              "backgroundColor": {
                "green": 0.2,
                "red": 0.8,
              }
            }
          }
        },
        "index": 0
      }
    }
  ]
}

要求完成後,套用的格式規則會更新試算表:

新增條件式格式設定食譜結果

為範圍新增日期和文字條件式格式設定規則

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何使用 AddConditionalFormatRuleRequest,根據工作表中 A1:D5 範圍的日期和文字值,建立新的條件式格式規則。如果文字包含「Cost」字串 (不區分大小寫),第一個規則會將儲存格文字設為粗體。如果儲存格中的日期早於上週,第二條規則會將儲存格文字設為斜體,並將顏色設為藍色。要求會使用 ConditionType 做為 BooleanRuletype

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startRowIndex": 0,
              "endRowIndex": 5,
              "startColumnIndex": 0,
              "endColumnIndex": 4,
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "TEXT_CONTAINS",
              "values": [
                {
                  "userEnteredValue": "Cost"
                }
              ]
            },
            "format": {
              "textFormat": {
                "bold": true
              }
            }
          }
        },
        "index": 0
      }
    },
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startRowIndex": 0,
              "endRowIndex": 5,
              "startColumnIndex": 0,
              "endColumnIndex": 4,
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "DATE_BEFORE",
              "values": [
                {
                  "relativeDate": "PAST_WEEK"
                }
              ]
            },
            "format": {
              "textFormat": {
                "italic": true,
                "foregroundColor": {
                  "blue": 1
                }
              }
            }
          }
        },
        "index": 1
      }
    }
  ]
}

要求完成後,套用的格式規則會更新試算表。在本例中,目前日期為 2016 年 9 月 26 日:

文字和日期條件式格式設定食譜結果

在範圍中新增自訂公式規則

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何使用 AddConditionalFormatRuleRequest ,根據自訂公式為試算表中的 B5:B8 範圍建立新的條件式格式規則。這項規則會計算 A 欄和 B 欄中儲存格的乘積。如果產品大於 120,儲存格文字會設為粗體和斜體。要求會使用 ConditionType 做為 BooleanRuletype

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "addConditionalFormatRule": {
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startColumnIndex": 2,
              "endColumnIndex": 3,
              "startRowIndex": 4,
              "endRowIndex": 8
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "CUSTOM_FORMULA",
              "values": [
                {
                  "userEnteredValue": "=GT(A5*B5,120)"
                }
              ]
            },
            "format": {
              "textFormat": {
                "bold": true,
                "italic": true
              }
            }
          }
        },
        "index": 0
      }
    }
  ]
}

要求完成後,套用的格式規則會更新試算表:

自訂條件式格式設定規則結果

刪除條件式格式設定規則

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何使用 DeleteConditionalFormatRuleRequest 刪除 SHEET_ID 指定工作表中的條件式格式規則,索引為 0

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "deleteConditionalFormatRule": {
        "sheetId": SHEET_ID,
        "index": 0
      }
    }
  ]
}

讀取條件式格式設定規則清單

下列spreadsheets.get方法程式碼範例說明如何取得試算表的標題、SHEET_ID,以及每個工作表的條件式格式規則清單。fields 查詢參數會決定要傳回的資料。

要求通訊協定如下所示。

GET https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID?fields=sheets(properties(title,sheetId),conditionalFormats)

回應包含 Spreadsheet 資源,其中含有 Sheet 物件陣列,每個物件都具有 SheetProperties 元素和 ConditionalFormatRule 元素陣列。如果指定的回應欄位設為預設值,系統會從回應中省略該欄位。要求會使用 ConditionType 做為 BooleanRuletype

{
  "sheets": [
    {
      "properties": {
        "sheetId": 0,
        "title": "Sheet1"
      },
      "conditionalFormats": [
        {
          "ranges": [
            {
              "startRowIndex": 4,
              "endRowIndex": 8,
              "startColumnIndex": 2,
              "endColumnIndex": 3
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "CUSTOM_FORMULA",
              "values": [
                {
                  "userEnteredValue": "=GT(A5*B5,120)"
                }
              ]
            },
            "format": {
              "textFormat": {
                "bold": true,
                "italic": true
              }
            }
          }
        },
        {
          "ranges": [
            {
              "startRowIndex": 0,
              "endRowIndex": 5,
              "startColumnIndex": 0,
              "endColumnIndex": 4
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "DATE_BEFORE",
              "values": [
                {
                  "relativeDate": "PAST_WEEK"
                }
              ]
            },
            "format": {
              "textFormat": {
                "foregroundColor": {
                  "blue": 1
                },
                "italic": true
              }
            }
          }
        },
        ...
      ]
    }
  ]
}

更新條件式格式設定規則或優先順序

下列 spreadsheets.batchUpdate 方法程式碼範例說明如何搭配多項要求使用 UpdateConditionalFormatRuleRequest。第一個要求會將現有的條件式格式規則移至較高的索引 (從 02,降低優先順序)。第二個要求會以新規則取代索引 0 的條件式格式規則,新規則會格式化 A1:D5 範圍中含有指定文字 (「總費用」) 的儲存格。第一個要求的移動作業完成後,第二個要求才會開始,因此第二個要求會取代原本位於索引 1 的規則。要求會使用 ConditionType 做為 BooleanRuletype

要求通訊協定如下所示。

POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{
  "requests": [
    {
      "updateConditionalFormatRule": {
        "sheetId": SHEET_ID,
        "index": 0,
        "newIndex": 2
      },
      "updateConditionalFormatRule": {
        "sheetId": SHEET_ID,
        "index": 0,
        "rule": {
          "ranges": [
            {
              "sheetId": SHEET_ID,
              "startRowIndex": 0,
              "endRowIndex": 5,
              "startColumnIndex": 0,
              "endColumnIndex": 4,
            }
          ],
          "booleanRule": {
            "condition": {
              "type": "TEXT_EQ",
              "values": [
                {
                  "userEnteredValue": "Total Cost"
                }
              ]
            },
            "format": {
              "textFormat": {
                "bold": true
              }
            }
          }
        }
      }
    }
  ]
}