如何使用pyxll函数在特定单元格坐标上放置多个列表



嗨,伙计们,我陷入了这种糟糕的境地,我必须在excel中的特定单元格坐标下放置多个列表,并且在的任何位置都找不到任何解决方案

当前实施:

@xl_func("string key: object", macro=True)
def calc(key) -> object:
result = [[1, 2], [3, 4]]
from pyxll import xl_app, XLCell
caller = xlfCaller()
address = caller.address
xl = xl_app()
xl_range = xl.Range(address)
one_below = xl_range.GetOffset(RowOffset=1, ColumnOffset=0)
XLCell.from_range(one_below)
cell = XLCell.from_range(one_below)
cell.options(type="var", auto_resize=True).value = result
return "123"

此代码适用于单个数据集。但现在我想在一个特定的细胞坐标上添加多个这样的数据集。如果可能的话,如下所示:

@xl_func("string key: object", macro=True)
def calc(key) -> object:
result = [[1, 2], [3, 4]]
from pyxll import xl_app, XLCell
caller = xlfCaller()
address = caller.address
xl = xl_app()
xl_range = xl.Range(address)
one_below = xl_range.GetOffset(RowOffset=1, ColumnOffset=0)
XLCell.from_range(one_below)
cell = XLCell.from_range(one_below)
#This need to go between A1:A2 to B1:B2
cell.options(type="var", auto_resize=True).value = result
#This need to go between D1:D2 to E1:E2
cell.options(type="var", auto_resize=True).value = result2
#This need to go between F1:F2 to G1:G2
cell.options(type="var", auto_resize=True).value = result3
return "123"
Env: 
Python 3.8.6 32 bit 
Pyxll 5.0.5 32 bit

您可以将XLCell.from_range作为COM range对象或地址作为字符串传递。

c = XLCell.from_range("A1")
c.options(auto_resize=True).value = x

此外,您可以使用Application.Range从地址获取COM范围,例如:

xl = xl_app()
r = xl.Range("A1")
r.Value = x
# or using XLCell
c = XLCell.from_range(r)
c.options(auto_resize=True).value = x

如果你需要,你也可以在地址中包括工作簿和表格,例如";[书1]第1张!A1";。

请参阅https://www.pyxll.com/docs/api/classes.html#pyxll.XLCell.from_range详细信息。

这篇常见问题解答文章也会有所帮助https://support.pyxll.com/hc/en-gb/articles/360044507453-Writing-arrays-back-to-Excel

最新更新