bijus
bijus

Reputation: 161

Google sheet javascript API - change background color to white

I have changed the google sheet row background color to red via javascript API. I need to again change the background color to white.

My code is below RGB code I have used for white 255,255,255 it's not working.

const request = {
  spreadsheetId, // fill with your own
  resource: {
    "requests": [
      {
        "repeatCell": {
          "range": {
            "sheetId": 0,
            "startRowIndex": 1,
            "endRowIndex": 2
          },
          // "fields": "userEnteredFormat"
          "cell": {
            "userEnteredFormat": {
              "backgroundColor": {
                'red': 255,
                'green': 255,
                'blue': 255
              },
            }
          },
          "fields": "userEnteredFormat(backgroundColor)"
        }
      },
      {
        "updateSheetProperties": {
          "properties": {
            "sheetId": 0,
            "gridProperties": {
              "frozenRowCount": 1
            }
          },
          "fields": "gridProperties.frozenRowCount"
        }
      }
    ]
  },
};

const resultData = await sheets.spreadsheets.batchUpdate(request);

Upvotes: 0

Views: 188

Answers (1)

Tanaike
Tanaike

Reputation: 201378

In your script, how about the following modification? In this modification, the property of repeatCell is modified as follows.

The property of backgroundColor is deprecated. Please be careful about this.

From:

{
  "repeatCell": {
    "range": {
      "sheetId": 0,
      "startRowIndex": 1,
      "endRowIndex": 2
    },
    "cell": {
      "userEnteredFormat": {
        "backgroundColor": {
          "red": 255,
          "green": 255,
          "blue": 255
        },
      }
    },
    "fields": "userEnteredFormat(backgroundColor)"
  }
}

To:

{
  "repeatCell": {
    "range": {
      "sheetId": 0,
      "startRowIndex": 1,
      "endRowIndex": 2
    },
    "fields": "userEnteredFormat(backgroundColorStyle)"
  }
}

or, if you want to set the white, you can also the following modification. Please set the values of red, green and blue like 0 to 1.

{
  "repeatCell": {
    "range": {
      "sheetId": 0,
      "startRowIndex": 1,
      "endRowIndex": 2
    },
    "cell": {
      "userEnteredFormat": {
        "backgroundColorStyle": {
          "rgbColor": {
            "red": 1,
            "green": 1,
            "blue": 1
          }
        }
      }
    },
    "fields": "userEnteredFormat(backgroundColorStyle)"
  }
}
  • By this, userEnteredFormat(backgroundColor) is reset and the background color is set as white.

Reference:

Upvotes: 2

Related Questions