AGGREGATE formula not automatically calculating wh

2019-08-18 17:54发布

问题:

I have a python 3.7 script that has been developed using the OPENPYXL (v2.5.10) library to take data from a number of excel workbooks, to process that data and then to write to a separate excel workbook. The results workbook contains around 100 named ranges and numerous formula which all work as expected including automatically calculating when the workbook is opened in excel.

I am having trouble with one particular formula group which includes the AGGREGATE function. In this case the formula writes to the results workbook, to the correct cell and in the correct form. While other formulas show the resulting value on opening the workbook, this sequence of formulas only show a result if you select the cell, place the cursor in the formula bar (as if you are editing the formula) and then you push the enter/return key. No change or edit is made to the formula. Once you have done this the formula works as expected.

I have tested this on both the latest macOS and windows versions of excel and I get the same behaviour. I should add that trying the 'calculate now', CtrlShiftAltF9, and 'calculate sheet' options do not have any impact. The only time the formula calculates is if you use the enter/return key.

The code that writes the formula is:

activeSheet.cell(row, col).value = f"=IFERROR(INDEX({rngData}, AGGREGATE(15,3,({rng}={cellRef})/({rng}={cellRef})*ROW({rng}),{nth}),{colIndex}),\"\")"

which gives, for example, a correct result in the excel workbook cell as:

=IFERROR(INDEX(_monthAgedDebt_Data, AGGREGATE(15,3,(_monthAgedDebt_ProjectNumbers=$L$4)/(_monthAgedDebt_ProjectNumbers=$L$4)*ROW(_monthAgedDebt_ProjectNumbers),1),6),"")

So in summary:

  • the code works as it writes the correct formula to the correct cell and in the correct form
  • in excel the formula does not automatically calculate but only works if you edit the formula in the cell, make no changes, and push enter/return to exit the edit

Is it an issue with AGGREGATE producing an array result? I chose this form of formula principally because you do not need a CTL-SHIFT-ENTER to make it work. If you enter it directly into a cell in excel you can enter it as a normal formula.

I haven't been able to find help on stack overflow other than this one. However, the solution proposed here doesn't work either.

This question poses a similar issue but has no relevant responses.

This question may hold a clue but I don't seem to be able to make that work as well.

Any thoughts on how to fix this issue appreciated. I am not sure if it is an openpyxl issue or an excel issue and am not sure what else to test.

回答1:

All - the final answer for completeness.

It turns out that the key to solving this issue lay with OPENPYXL and that the guidance provided in the answer by Charlie Clark to this

question

was correct. I had initially applied the solution incorrectly.

I changed the formula to:

activeSheet.cell(row, col).value = \ f"=IFERROR(INDEX({rngData}, _xlfn.AGGREGATE(15,3,({rng}={cellRef})/({rng}={cellRef})*ROW({rng}),{nth}),{colIndex}),\"\")"

by adding in the '_xlfn.' to the front of the AGGREGATE function statement.

The excel spreadsheet now works as expected without the need to edit the cell containing the formula.