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

how to place multiple list on a specific cell co-ordinates using pyxll functions

大家好,我陷入了这种糟糕的境地,我必须在 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 范围对象或地址作为字符串传递。

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

如果需要,您还可以在地址中包含工作簿和 sheet,例如“[Book1]Sheet1!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