You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何GSpread的RGB配色方案不准确?设置黄色却显示深蓝色

GSpread设置单元格背景色RGB显示异常的解决方法
  • 问题本质:gspread_formatting的color()函数接受的是0到1区间的浮点数,而非常规的0-255整数。你传入的241、194、50这类数值会被超出范围的部分截断为1,最终计算出的颜色值完全偏离预期,才会显示成深蓝色。

  • 修复步骤:把每个RGB分量除以255,转换成0-1的浮点数即可。

修改后的代码示例:

import gspread
from gspread_formatting import *
import keyboard

gc = gspread.service_account(filename=r'C:\Users\Name\PycharmProjects\GoogleSheetsGame\SuperSecretKey.json')
sh = gc.open('Video Game')

mainworksheet = sh.worksheet("Game")
# 转换RGB值为gspread要求的浮点数格式
fmt = cellFormat(backgroundColor=color(241/255, 194/255, 50/255))

print('Ready!')
while True:
    if keyboard.is_pressed('right arrow'):
        format_cell_range(mainworksheet, 'BI39:BI41', fmt)

如果需要频繁转换,可以封装一个工具函数简化操作:

def rgb_to_gspread(r, g, b):
    return color(r/255, g/255, b/255)

# 调用示例
fmt = cellFormat(backgroundColor=rgb_to_gspread(241, 194, 50))

内容的提问来源于stack exchange,提问作者San__

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 08:57:14