我相信你的目标如下。
- 您想要使用 Sheets API 从电子表格中检索富文本数据。
在这种情况下,我认为使用“spreadsheets.get”的方法可以实现你的目标。但是当您使用它时,请设置字段。这样就可以获取到富文本数据了。
端点:
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?ranges=Sheet1!A1&fields=sheets
- 该端点使用
sheets
as fields
. Also, sheets(data(rowData(values(textFormatRuns))))
可以使用。并且该值是从“Sheet1”中的“A1”单元格中检索的。
- 在这种情况下,不会进行 URL 编码。因此,当您使用它时,请对查询参数进行URL编码。
示例卷曲命令:
curl \
'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?ranges=Sheet1!A1&fields=sheets' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
样本值:
当从上面的单元格中检索带有以下字段的富文本数据时sheets(data(rowData(values(textFormatRuns))))
,得到以下值。
{
"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脚本时,示例脚本如下。
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()
用来。当你想从多个单元格中检索富文本的数据时,你还可以使用getRichTextValues()
.
Result:
当使用上述单元格时,将返回以下值。
[
{
"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:
当使用python脚本时,就变成如下所示。响应值与curl命令相同。这种情况下,你也可以在官方文档中查看示例脚本。Ref https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get
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:
- 例如,当您想使用 Sheets API 确认单元格中的文本数据时,
userEnteredValue
and formattedValue
可以使用。
参考:
-
Method: spreadsheets.get https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get
- 获取 RichTextValue() https://developers.google.com/apps-script/reference/spreadsheet/range#getrichtextvalue
- 获取 RichTextValue() https://developers.google.com/apps-script/reference/spreadsheet/range#getrichtextvalues
- 类 RichTextValue https://developers.google.com/apps-script/reference/spreadsheet/rich-text-value