I'm using Googlesheet API with Python to get the cell data. I'm using the following code to get the value of a cell in my google sheet:
client = gspread.service_account_from_dict(creds)
workbook = client.open('HR Data')
sheet = workbook.get_worksheet(0)
Cell1 = sheet.acell('B3').value
How can I get the font color of that cell? The cells are formatted using conditional formatting.
I was advised to use the following codes:
client = gspread.service_account_from_dict(creds)
spreadsheet_id = '1qlfDoQctCjfsO-bJkNbedi4t9P_8Uq242WxiOabZSA4'
rng = "WH Data!H2" # Please set the cell coordinate as A1Notation.
service = build("sheets", "v4", credentials=client.auth)
res = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=[rng], fields="sheets(data(rowData(values(userEnteredFormat,formattedValue,textFormatRuns))),conditionalFormats)").execute()
conditionalFormats = res["sheets"][0].get("conditionalFormats")
print(conditionalFormats)
The result returns a lot of information instead of a single value:
[{'ranges': [{'sheetId': 1781915778, 'startRowIndex': 0, 'endRowIndex': 1004, 'startColumnIndex': 7, 'endColumnIndex': 8}, {'sheetId': 1781915778, 'startRowIndex': 0, 'endRowIndex': 1004, 'startColumnIndex': 12, 'endColumnIndex': 13}, {'sheetId': 1781915778, 'startRowIndex': 1, 'endRowIndex': 1004, 'startColumnIndex': 3, 'endColumnIndex': 4}], 'booleanRule': {'condition': {'type': 'NUMBER_LESS', 'values': [{'userEnteredValue': '0'}]}, 'format': {'textFormat': {'foregroundColor': {'red': 1}, 'foregroundColorStyle': {'rgbColor': {'red': 1}}}}}}, {'ranges': [{'sheetId': 1781915778, 'startRowIndex': 0, 'endRowIndex': 1004, 'startColumnIndex': 7, 'endColumnIndex': 8}, {'sheetId': 1781915778, 'startRowIndex': 0, 'endRowIndex': 1004, 'startColumnIndex': 12, 'endColumnIndex': 13}, {'sheetId': 1781915778, 'startRowIndex': 1, 'endRowIndex': 1004, 'startColumnIndex': 3, 'endColumnIndex': 4}], 'booleanRule': {'condition': {'type': 'NUMBER_GREATER', 'values': [{'userEnteredValue': '0'}]}, 'format': {'textFormat': {'foregroundColor': {'red': 0.043137256, 'green': 0.5019608, 'blue': 0.2627451}, 'foregroundColorStyle': {'rgbColor': {'red': 0.043137256, 'green': 0.5019608, 'blue': 0.2627451}}}}}}]
And the value doesn't change no matter which cell I entered. Looks like it only returns the rule of the conditional format, but not the format result.