Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从单元格中检索多个字体数据_Python_Google Apps Script_Google Sheets_Google Sheets Api - Fatal编程技术网

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

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

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

例如,也可以使用API通过属性
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的数据
在这种情况下,我认为使用“电子表格.获取”的方法可以实现您的目标。但当您使用此选项时,请设置字段。这样,可以检索richtext数据

终点:
  • 此端点将
    工作表
    用作
    字段
    。此外,还可以使用
    工作表(数据(行数据(值(textFormatRuns)))
    。该值从“Sheet1”中的“A1”单元格中检索
  • 在这种情况下,不进行URL编码。因此,当您使用此选项时,请对查询参数进行URL编码
示例curl命令:
  • 在这个示例中,使用了访问令牌
样本值:

当使用
表格(数据(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)
  • 使用了
    getRichTextValue()
    。当您想从多个单元格中检索richtext的数据时,还可以使用
    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”中测试此功能

现在,我注意到您的问题已经更新。对此我深表歉意。根据您更新的问题,我添加了一个python示例脚本。您的答案太棒了!非常感谢您的广泛和深入全面的解释。我还没有回答,因为在你花了这么多时间帮助我之后,我想花必要的时间给你一个有价值的回答,并确保自己在我自己的工作表上实现这一切,看看我是否能使它正确工作。但是,当我看到你刚才的评论时,我不得不迅速做出回应,告诉你我承认了你的信息,这显然是一条出路。当我明天有时间做这件事时,我一定会告诉你最新情况。再次感谢:)
[
  {
    "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)