GSpread CellFormat 前景颜色
GSpread CellFormat foregroundColor
我正在尝试在 Google 表格中为我的 header 上色,但是 GSpread textFormat 似乎无法恢复正确的颜色。
我已经查看了 GSpread 文档,几乎没有关于颜色代码应该是什么格式的内容,所以我只是假设 RGB 就是它应该的样子。
还查看了 Google 工作表 API 并使用了 RGB。
fmt = CellFormat(textFormat=textFormat(bold=True, foregroundColor=color(112, 48, 160), fontSize=24),)
format_cell_range(worksheet, 'B1:B1', fmt)
header 应该与以下 RGB 代码相同的颜色:(112, 48, 160)
而不是这个:(144, 208, 96)
- 您想使用 gspread 设置 Google 电子表格单元格的前景色。
- 您想将单元格 "B1" 设置为
112, 48, 160
的颜色,即 RGB。
- 您想使用
gspread-formatting
实现此目的。
- 您已经能够使用 gspread 将值放入和获取电子表格。
如果我的理解是正确的,这个答案怎么样?
准备:
在使用此修改后的脚本之前,请按如下方式安装 gspread-formatting
。如果您已经安装,请跳过此部分。
$ pip install gspread-formatting
修改后的脚本:
请设置spreadsheetId
和sheetName
。
import gspread_formatting as gsf # <--- Also please add this to your script.
spreadsheetId = "###"
sheetName = "Sheet1"
client = gspread.authorize(credentials)
ss = client.open_by_key(spreadsheetId)
worksheet = ss.worksheet(sheetName)
fmt = gsf.cellFormat(
textFormat=gsf.textFormat(
bold=True, foregroundColor=gsf.color(112, 48, 160), fontSize=24)
)
gsf.format_cell_range(worksheet, 'B1:B1', fmt)
参考文献:
如果这对您的情况没有用,我深表歉意。
Sheets API 希望 RGB(和 alpha)值在范围 (0,1) 内;将每个典型值除以 255.0,您将看到预期的结果。
就作者而言,获得所需 rgb 值的解决方案是:
fmt = CellFormat(textFormat=textFormat(bold=True, foregroundColor=color(112/250, 48/250, 160/250), fontSize=24),)
format_cell_range(worksheet, 'B1:B1', fmt)
有一些说明性的例子
https://developers.google.com/sheets/api/samples/formatting
我正在尝试在 Google 表格中为我的 header 上色,但是 GSpread textFormat 似乎无法恢复正确的颜色。
我已经查看了 GSpread 文档,几乎没有关于颜色代码应该是什么格式的内容,所以我只是假设 RGB 就是它应该的样子。
还查看了 Google 工作表 API 并使用了 RGB。
fmt = CellFormat(textFormat=textFormat(bold=True, foregroundColor=color(112, 48, 160), fontSize=24),)
format_cell_range(worksheet, 'B1:B1', fmt)
header 应该与以下 RGB 代码相同的颜色:(112, 48, 160)
而不是这个:(144, 208, 96)
- 您想使用 gspread 设置 Google 电子表格单元格的前景色。
- 您想将单元格 "B1" 设置为
112, 48, 160
的颜色,即 RGB。
- 您想将单元格 "B1" 设置为
- 您想使用
gspread-formatting
实现此目的。 - 您已经能够使用 gspread 将值放入和获取电子表格。
如果我的理解是正确的,这个答案怎么样?
准备:
在使用此修改后的脚本之前,请按如下方式安装 gspread-formatting
。如果您已经安装,请跳过此部分。
$ pip install gspread-formatting
修改后的脚本:
请设置spreadsheetId
和sheetName
。
import gspread_formatting as gsf # <--- Also please add this to your script.
spreadsheetId = "###"
sheetName = "Sheet1"
client = gspread.authorize(credentials)
ss = client.open_by_key(spreadsheetId)
worksheet = ss.worksheet(sheetName)
fmt = gsf.cellFormat(
textFormat=gsf.textFormat(
bold=True, foregroundColor=gsf.color(112, 48, 160), fontSize=24)
)
gsf.format_cell_range(worksheet, 'B1:B1', fmt)
参考文献:
如果这对您的情况没有用,我深表歉意。
Sheets API 希望 RGB(和 alpha)值在范围 (0,1) 内;将每个典型值除以 255.0,您将看到预期的结果。
就作者而言,获得所需 rgb 值的解决方案是:
fmt = CellFormat(textFormat=textFormat(bold=True, foregroundColor=color(112/250, 48/250, 160/250), fontSize=24),)
format_cell_range(worksheet, 'B1:B1', fmt)
有一些说明性的例子 https://developers.google.com/sheets/api/samples/formatting