简体   繁体   English

如何使用 openpyxl 在 Excel 工作表中插入数组公式?

[英]How to insert array formula in an Excel sheet with openpyxl?

I'm using OpenPyxl to create and modify an Excel sheet.我正在使用 OpenPyxl 创建和修改 Excel 工作表。 I have the following formula in Excel:我在 Excel 中有以下公式:

=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))

This formula which is an "array formula" is working but in order to write it by hand, I have to finish with CTRL+SHIFT+ENTER (because it's an array formula).这个公式是一个“数组公式”,但为了手写它,我必须用 CTRL+SHIFT+ENTER 结束(因为它是一个数组公式)。 This transform then the formula as follow:这个变换则公式如下:

{=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))}

I want to be able to write this formula via OpenPyxl with the following code:我希望能够使用以下代码通过 OpenPyxl 编写此公式:

    sheet.cell(row=j, column=i).value = '{=(SUM(IF(LEFT(Balances!$B$2:$B$100,LEN($B4))=$B4,Balances!$D$2:$D$100)))}'

However, it doesn't work.但是,它不起作用。 OpenPyxl can't manage it. OpenPyxl 无法管理它。 It give me the formula written but not working.它给了我写的公式但没有用。

I could do it with XLSX Writer https://xlsxwriter.readthedocs.io/example_array_formula.html我可以用 XLSX Writer https://xlsxwriter.readthedocs.io/example_array_formula.html 来做

However XLSX writer doesn't work with already created files.但是,XLSX 编写器不适用于已创建的文件。

I don't see which path to follow.我不知道该走哪条路。

Use the worksheet.formula_attributes to set the array formula. 使用worksheet.formula_attributes设置数组公式。 Place the formula in the desired cell, A1 for this example. 将公式放在所需的单元格中,在此示例中为A1。 Then set the formula_attributes to the cell range you want to apply the formula to. 然后将formula_attributes设置为您要对其应用公式的单元格区域。

ws["A1"] = "=B4:B8"
ws.formula_attributes['A1'] = {'t': 'array', 'ref': "A1:A5"}

In case solution provided above does not work, check whether you are using english name of functions in your formulae.如果上面提供的解决方案不起作用,请检查您是否在公式中使用了函数的英文名称。

In my case I have been using czech function name and although formulae works if inserted manually, it did not work when inserted via openpyxl.在我的情况下,我一直在使用捷克 function 名称,虽然公式在手动插入时有效,但在通过 openpyxl 插入时不起作用。

Switching to english name of the function solved the issue!切换到 function 的英文名称解决了这个问题!

In my case the formula was using arrays for intermediate results before summarizing with a MAX.在我的例子中,公式在使用 MAX 进行汇总之前使用 arrays 作为中间结果。 The formula worked OK when typed in but not when inserted via openpyxl.该公式在输入时工作正常,但在通过 openpyxl 插入时无效。 Office 365 version of Excel was inserting the new implicit intersection operator, @, incorrectly. Excel 的 Office 365 版本错误地插入了新的隐式交集运算符 @。

formula: ="Y" & MAX(tbl_mcare_opt[Year]*(tbl_mcare_opt[Who]=[@Who])*(tbl_mcare_opt[Year]<=intyear(this_col_name())))

It turns out that the properties needed to be set, as above.原来需要设置属性,如上。 This allowed Excel to correctly interpret the formula.这允许 Excel 正确解释公式。 In my case the ref turned out to be just the single cell address.在我的例子中, ref原来只是单个单元格地址。

I was able to determine that the formula was using dynamic arrays with a regex.我能够确定该公式正在使用带有正则表达式的动态 arrays。 If it was then I added the formula properties.如果是,那么我添加了公式属性。

        # provision for dynamic arrays to be included in formulas - notify excel
        if is_formula(values[cn]):
          regex_column=r'[A-Za-z_]+(\[\[?[ A-Za-z0-9]+\]?\])'
          pattern=re.compile(regex_column)
          matches=pattern.findall(values[cn]) 
          if len(matches): # looks like a dynamic formula
            address=get_column_letter(cix)+str(rix)
            ws.formula_attributes[address]={'t':'array','ref': address}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM