为何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__
相关产品推荐
相关产品推荐

