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:
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: