I’m trying to update conditional formatting rules using the Google Sheets API but I’m having trouble with the indexing across different worksheets.
My Understanding
From what I’ve learned, here’s what I need to do:
- Retrieve all the existing conditional formatting rules
- Identify the correct index for the rule I want to modify
- Send an update request that includes this index
The Issue
The conditional formatting rules I get back are organized by separate worksheets. Each worksheet has its own set of rules with unique indexes (0, 1, 2, etc.). However, when I attempt to update a rule, it appears that the API requires a global index that spans the entire spreadsheet.
My Current Approach
I retrieve the rules using the following request:
GET https://sheets.googleapis.com/v4/spreadsheets/{SHEET_ID}?fields=sheets(properties(title,sheetId),conditionalFormats)
The response I receive is similar to this:
{
"worksheets": [
{
"properties": {
"worksheetId": 123,
"name": "FirstSheet"
},
"conditionalFormats": [
{
"ranges": [{
"startRow": 1,
"endRow": 100,
"startCol": 1,
"endCol": 5,
"worksheetId": 123
}],
"booleanRule": {
"condition": {
"type": "TEXT_CONTAINS",
"values": [{
"userEnteredValue": "PRIORITY"
}]
},
"format": {
"backgroundColor": {
"red": 0.8,
"green": 0.2,
"blue": 0.2
}
}
}
}
]
},
{
"properties": {
"worksheetId": 456,
"name": "SecondSheet"
},
"conditionalFormats": [
{
"ranges": [{
"startRow": 2,
"endRow": 200,
"startCol": 3,
"endCol": 8,
"worksheetId": 456
}],
"booleanRule": {
"condition": {
"type": "TEXT_EQ",
"values": [{
"userEnteredValue": "COMPLETED"
}]
},
"format": {
"backgroundColor": {
"red": 0.2,
"green": 0.8,
"blue": 0.2
}
}
}
}
]
}
]
}
When I try to make an update for the rule on SecondSheet (which is index 0 within that specific sheet) with the following request:
{
"requests": [{
"updateConditionalFormatRule": {
"index": 0,
"worksheetId": 456,
"rule": {
"ranges": [{
"startRow": 2,
"endRow": 200,
"startCol": 3,
"endCol": 8,
"worksheetId": 456
}],
"booleanRule": {
"condition": {
"type": "TEXT_EQ",
"values": [{
"userEnteredValue": "FINISHED"
}]
},
"format": {
"backgroundColor": {
"red": 0.3,
"green": 0.7,
"blue": 0.9
}
}
}
}
}
}]
}
It updates a different rule instead. This suggests that the index is perhaps treated as a global index across all worksheets, not a local index specific to each one.
My Question
Is there a method to obtain a complete list of all conditional formatting rules showing their correct global indexes? Or am I overlooking how indexing functions when updating rules in various worksheets? My spreadsheet contains about 100 conditional formatting rules distributed across multiple worksheets, making accurate indexing essential.