Python 从单元格中检索多个字体数据
google工作表中的单元格可以沿存储在其中的字符串具有多种字体颜色(或其他富文本属性) 例如,也可以使用API通过属性Python 从单元格中检索多个字体数据,python,google-apps-script,google-sheets,google-sheets-api,Python,Google Apps Script,Google Sheets,Google Sheets Api,google工作表中的单元格可以沿存储在其中的字符串具有多种字体颜色(或其他富文本属性) 例如,也可以使用API通过属性TextFormatRun完成 但是,这里只讨论了编写部分,我在API文档或外部在线资源中没有提到有关读取和检索此富文本数据的内容 这是可以实现的吗 例如,我想检索如下单元格的完整字体颜色数据: PS:如果相关的话,我正在使用python。我相信您的目标如下 const sheet = SpreadsheetApp.getActiveSpreadsheet().getShee
TextFormatRun
完成
但是,这里只讨论了编写部分,我在API文档或外部在线资源中没有提到有关读取和检索此富文本数据的内容
这是可以实现的吗
例如,我想检索如下单元格的完整字体颜色数据:
PS:如果相关的话,我正在使用python。我相信您的目标如下
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)
- 您希望使用Sheets API从电子表格检索richtext的数据
- 此端点将
用作工作表
。此外,还可以使用字段
。该值从“Sheet1”中的“A1”单元格中检索工作表(数据(行数据(值(textFormatRuns)))
- 在这种情况下,不进行URL编码。因此,当您使用此选项时,请对查询参数进行URL编码
- 在这个示例中,使用了访问令牌
表格(数据(rowData(值(textFormatRuns)))
字段从上述单元格检索richtext的数据时,将获得以下值
{
"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
}
}
}
}
]
}
]
}
]
}
]
}
]
}
谷歌应用程序脚本:
使用GoogleApps脚本时,示例脚本如下所示
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)
- 使用了
。当您想从多个单元格中检索richtext的数据时,还可以使用getRichTextValue()
getRichTextValues()
[
{
"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脚本时,它将变成如下所示。响应值与curl命令相同。在本例中,您还可以在官方文档中看到示例脚本
注:
- 当您想使用Sheets API确认单元格中的文本数据时,例如,可以使用
和userEnteredValue
formattedValue
-
- 您还可以在“尝试此API”中测试此功能
[
{
"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
}
]
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)