从单元格内检索多种字体数据

2024-02-22

谷歌工作表中的单元格可以沿着存储在其中的字符串具有多种字体颜色(或其他富文本属性)。

也可以通过属性使用 API 来完成TextFormatRun as 在这里解释 https://stackoverflow.com/questions/49287090/apply-multiple-font-colors-to-the-text-in-a-single-google-sheets-cell例如。

但是,仅讨论了编写部分,并且我在 API 文档或在线外部资源中没有发现任何提及有关reading并检索该富文本数据。

这可以实现吗?

例如,我想检索像这样的单元格的完整字体颜色数据:

PS:如果相关的话,我正在使用 python。


我相信你的目标如下。

  • 您想要使用 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
  • 在此示例curl 中,使用了访问令牌。

样本值:

当从上面的单元格中检索带有以下字段的富文本数据时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
    • 您还可以在“尝试此 API”中进行测试。
  • 获取 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从单元格内检索多种字体数据 的相关文章

随机推荐