从一个单元格中检索多种字体数据 [英] Retrieve multiple fonts data from within a cell

查看:18
本文介绍了从一个单元格中检索多种字体数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google工作表中的单元格可以具有多种字体颜色(或其他富文本属性),也可以包含存储在中的字符串。

也可以通过TextFormatRun属性explained here等接口来实现。

但是,只有关于编写部分的讨论,我在API文档或联机外部资源中找不到任何有关读取和检索此富文本数据的内容。

这是否可以实现?

例如,我希望检索单元格的完整字体颜色数据,如下所示:

PS:如果与此相关,我正在使用Python。

推荐答案

我相信您的目标如下。

  • 您要使用Sheets API从电子表格中检索富文本数据。

在这种情况下,我认为可以使用&spadsheets.get";方法来实现您的目标。但当您使用此功能时,请设置字段。这样,就可以检索到富文本数据。

终结点:

https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}?ranges=Sheet1!A1&fields=sheets
  • 此终结点使用sheetsASfields。也可以使用sheets(data(rowData(values(textFormatRuns))))。并且该值是从";Sheet1";中的";A1";的单元格检索的。
  • 在这种情况下,不进行URL编码。因此,当您使用它时,请对查询参数进行URL编码。

cURL命令示例:

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

使用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()
结果:

使用上述单元格时,返回下列值。

[
  {
    "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

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)

注意:

  • 例如,当您想使用Sheets API确认单元格中的文本数据时,可以使用userEnteredValueformattedValue

引用:

这篇关于从一个单元格中检索多种字体数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆