从一个单元格中检索多种字体数据

本教程将介绍从一个单元格中检索多种字体数据的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

从一个单元格中检索多种字体数据 教程 第1张

问题描述

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

引用:

    Method: spreadsheets.get

      您也可以在"试用此API进行测试。

    getRichTextValue()

    getRichTextValues()

    Class RichTextValue

好了关于从一个单元格中检索多种字体数据的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。