1

A cell in a google sheet can have multiple font colors (or other rich text properties) in it along the string stored within.

It can also be done with the API through the property TextFormatRun as explained here for instance.

However, there's only discussion about the writing part, and I find no mention anywhere on the API documentation or on external resources online regarding reading and retrieving this rich text data.

Is this achievable ?

For instance, I would like to retrieve the full font color data for a cell like this:

enter image description here

PS: I'm working with python, if this is relevant.

1
  • 1
    Now, I noticed that your question had been updated. I deeply apologize for this. From your updated question, I added a sample script of python.
    – Tanaike
    Commented Oct 29, 2020 at 23:17

1 Answer 1

3

I believe your goal as follows.

  • You want to retrieve the data of richtext from Spreadsheet using Sheets API.

In this case, I think that your goal can be achieved using the method of "spreadsheets.get". But when you use this, please set the fields. By this, the richtext data can be retrieved.

Endpoint:

https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?ranges=Sheet1!A1&fields=sheets
  • This endpoint uses sheets as fields. Also, sheets(data(rowData(values(textFormatRuns)))) can be used. And the value is retrieved from a cell of "A1" in "Sheet1".
  • In this case, no URL encode is done. So when you use this, please do the URL encode for the query parameter.

Sample curl command:

curl \
  'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?ranges=Sheet1!A1&fields=sheets' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed
  • In this sample curl, the access token is used.

Sample value:

enter image description here

When the data of richtext is retrieved from the above cell with the fields of sheets(data(rowData(values(textFormatRuns)))), the following value is obtained.

{
  "sheets": [
    {
      "data": [
        {
          "rowData": [
            {
              "values": [
                {
                  "textFormatRuns": [
                    {
                      "format": {
                        "foregroundColor": {
                          "red": 1
                        },
                        "bold": true,
                        "foregroundColorStyle": {
                          "rgbColor": {
                            "red": 1
                          }
                        }
                      }
                    },
                    {
                      "startIndex": 1,
                      "format": {
                        "fontSize": 18
                      }
                    },
                    {
                      "startIndex": 5,
                      "format": {
                        "foregroundColor": {
                          "red": 1
                        },
                        "italic": true,
                        "foregroundColorStyle": {
                          "rgbColor": {
                            "red": 1
                          }
                        }
                      }
                    },
                    {
                      "startIndex": 6,
                      "format": {}
                    },
                    {
                      "startIndex": 7,
                      "format": {
                        "foregroundColor": {
                          "blue": 1
                        },
                        "bold": true,
                        "italic": true,
                        "foregroundColorStyle": {
                          "rgbColor": {
                            "blue": 1
                          }
                        }
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Google Apps Script:

When Google Apps Script is used, the sample script is as follows.

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const richTextValue = sheet.getRange("A1").getRichTextValue();
const res = richTextValue.getRuns().map(r => ({
  text: r.getText(),
  foregroundColor: r.getTextStyle().getForegroundColor(),
  fontSize: r.getTextStyle().getFontSize(),
  bold: r.getTextStyle().isBold(),
  italic: r.getTextStyle().isItalic()
}));
console.log(res)
  • getRichTextValue() is used. When you want to retrieve the data of richtext from multiple cells, you can also use getRichTextValues().
Result:

When above cell is used, the following value is returned.

[
  {
    "text": "s",
    "foregroundColor": "#ff0000",
    "fontSize": 36,
    "bold": true,
    "italic": false
  },
  {
    "text": "ampl",
    "foregroundColor": "#000000",
    "fontSize": 18,
    "bold": false,
    "italic": false
  },
  {
    "text": "e",
    "foregroundColor": "#ff0000",
    "fontSize": 36,
    "bold": false,
    "italic": true
  },
  {
    "text": " ",
    "foregroundColor": "#000000",
    "fontSize": 36,
    "bold": false,
    "italic": false
  },
  {
    "text": "text",
    "foregroundColor": "#0000ff",
    "fontSize": 36,
    "bold": true,
    "italic": true
  }
]

Python:

When python script is used, it becomes as follows. The response value is the same with the curl command. In this case, you can also see the sample script at the official document. Ref

spreadsheet_id = '###' # Please set Spreadsheeet ID.
ranges = 'Sheet1!A1' # Please set range as a1Notation.
service = build('sheets', 'v4', credentials=creds)
fields = 'sheets(data(rowData(values(textFormatRuns))))'
res = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=ranges, fields=fields).execute()
print(res)

Note:

  • When you want to confirm the text data in the cell using Sheets API, for example, userEnteredValue and formattedValue can be used.

References:

1
  • 1
    Your answer is amazing ! Thanks so much for this extensive and deeply comprehensive explanation. I hadn't answered yet because I wanted to take the necessary time to give you a worthy response after you took all this time to help me, and be sure to implement all this by myself on my own sheet to see if I can make it work correctly. But, as I saw the comment you just made, I had to make a quick response to tell you that I acknowledged your message and this clearly seems to be the way to go. I'll be sure to update you when I will have the time for this tomorrow. Thanks again :)
    – Atralb
    Commented Oct 29, 2020 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.