0

嗨,伙计们,我陷入了这种糟糕的境地,我必须在特定单元格坐标的 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
4

1 回答 1

1

您可以将 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

如果需要,您还可以在地址中包含工作簿和工作表,例如“[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有所帮助

于 2020-12-17T19:59:59.397 回答