Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set xlsx to recalculate formulae on open

Tags:

xlsx

openxml

I am generating xlsx files and would like to not have to compute the values of all formulae during this process. That is, I would like to set <v> to 0 (or omit it) for cells with an <f>, and have Excel fill in the values when it is opened.

One suggestion was to have a macro run Calculate on startup, but have been unable to find a complete guide on how to do this with signed macros to avoid prompting the user. A flag you can set somewhere within the xlsx would be far better.

Edit: I'm not looking for answers that involve using Office programs to make changes. I am looking for file format details.

like image 646
OrangeDog Avatar asked Aug 21 '13 11:08

OrangeDog


People also ask

How do you make Excel recalculate automatically?

In the Excel for the web spreadsheet, click the Formulas tab. Next to Calculation Options, select one of the following options in the dropdown: To recalculate all dependent formulas every time you make a change to a value, formula, or name, click Automatic. This is the default setting.

How often does Excel automatically recalculate formula in a worksheet?

Automatic (default) - tells Excel to automatically recalculate all dependent formulas every time any value, formula, or name referenced in those formulas is changed. Automatic Except for Data Tables - automatically recalculate all dependent formulas except data tables.


2 Answers

The Python module XlsxWriter sets the formula <v> value to 0 (unless the actual value is known) and the <calcPr> fullCalcOnLoad attribute to true in the xl/workbook.xml file:

<calcPr fullCalcOnLoad="1"/>

This works for all Excel and OpenOffice, LibreOffice, Google Docs and Gnumeric versions that I have tested.

The place it won't work is for non-spreadsheet applications that cannot re-calculate the formula value such as file viewers.

like image 52
jmcnamara Avatar answered Oct 21 '22 21:10

jmcnamara


If calculation mode is set to automatic, Excel always (re)calculates workbooks on open.

So, just generate your files with calculation mode set to "Automatic".

In xl/workbook.xml, add following node to workbook node:

<calcPr calcMode="auto"/>

Also check Description of how Excel determines the current mode of calculation.

You can use macros as suggested, however you will create a less secure and less compatible workbook without avoiding user interaction to force calculation.

If you opt by using VBA, you may Application.Calculate in Workbook_Open event.

like image 27
LS_ᴅᴇᴠ Avatar answered Oct 21 '22 19:10

LS_ᴅᴇᴠ