关于更新单元格属性的ipysheet问题

ipysheet question about update cell attribute

我的代码在这里

import ipysheet
mysheet = ipysheet.sheet(rows=2, columns=2)
mysheet
c = cell(0,0,1, background_color = 'red')

我想改变background_color,我希望做这样的事情

c.background_color = 'yellow'

或以后

mysheet[0, 0].background_color = 'yellow'

而不是构造一个新对象

c = cell(c.row_start, c.column_start, c.value, background_color = 'yellow')

对不起,如果我提出 read-doc-for-me 问题。但是我没有找到我期望的方法。如果没有,谁能解释一下?谢谢

在查看 cell 方法的 source code 之后,您可以找到 cell 方法 returns ipysheet.sheet.Cell class 的实例。背景颜色设置为此 class 的 style 参数的 属性。如果此 属性:

,您可以通过为值分配指定的颜色来动态更改此 属性
>>> import ipysheet
>>> from ipysheet.easy import cell
>>> mysheet = ipysheet.sheet(rows=2, columns=2)
>>> c = cell(0,0,1, background_color = 'red')
>>> type(c)
ipysheet.sheet.Cell
>>> print(c.style['backgroundColor'])
red
>>> c.style['backgroundColor'] = 'blue'
>>> print(c.style['backgroundColor'])
blue

更新:

看来,将 属性 设置为另一个值不会更改 sheet 的背景颜色,因为 sheet 的小部件无法动态获取状态。您可以使用单元格实例的 send_state() 方法更新状态:

c.style['backgroundColor'] = 'blue'
c.send_state()

# or by position in mysheet
mysheet[0, 0].style['backgroundColor'] = 'blue'
mysheet[0, 0].send_state()

sheet 的单元格更改背景颜色后。