All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Working with Formulas

Table of Contents

In general a formula in Excel can be used directly in the worksheet_write_formula() function:

worksheet_write_formula(worksheet, 0, 0, "=10*B1 + C1", NULL);

However, there are a few potential issues and differences that the user should be aware of. These are explained in the following sections.

Non US Excel functions and syntax

Excel stores formulas in the format of the US English version, regardless of the language or locale of the end-user's version of Excel. Therefore all formula function names written using libxlsxwriter must be in English:

// The following formula syntax is okay.
worksheet_write_formula(worksheet, 0, 0, "=SUM(1, 2, 3)", NULL);
// The following formula syntax is in French. Will cause error on load.
worksheet_write_formula(worksheet, 0, 1, "=SOMME(1, 2, 3)", NULL);

Also, formulas must be written with the US style separator/range operator which is a comma (not semi-colon). Therefore a formula with multiple values should be written as follows:

// The following formula syntax is okay.
worksheet_write_formula(worksheet, 0, 0, "=SUM(1, 2, 3)", NULL);
// The following formula use semi-colons. Will cause error on load.
worksheet_write_formula(worksheet, 0, 1, "=SUM(1; 2; 3)", NULL);

If you have a non-English version of Excel you can use the following multi-lingual Formula Translator to help you convert the formula. It can also replace semi-colons with commas.

Formulas added in Excel 2010 and later

Excel 2010 and later versions added functions which weren't defined in the original file specification. These functions are referred to by Microsoft as "Future Functions". Examples of these functions are ACOT, CHISQ.DIST.RT , CONFIDENCE.NORM, STDEV.P, STDEV.S and WORKDAY.INTL.

When written using worksheet_write_formula() these functions need to be fully qualified with a _xlfn. (or other) prefix as they are shown the list below. For example:

worksheet_write_formula(worksheet, 0, 0, "=_xlfn.STDEV.S(B1:B10)", NULL);

They will appear without the prefix in Excel:


The following list is taken from MS XLSX extensions documentation on future functions.

Future Functions

Dealing with formula errors

If there is an error in the syntax of a formula it is usually displayed in Excel as #NAME?. Alternatively you may get a warning from Excel when the file is loaded. If you encounter an error like this you can debug it as follows:

  1. Ensure the formula is valid in Excel by copying and pasting it into a cell. Note, this should be done in Excel and not other applications such as OpenOffice or LibreOffice since they may have slightly different syntax.
  2. Ensure the formula is using comma separators instead of semi-colons, see Non US Excel functions and syntax above.
  3. Ensure the formula is in English, see Non US Excel functions and syntax above.
  4. Ensure that the formula doesn't contain an Excel 2010+ future function as listed above (Formulas added in Excel 2010 and later). If it does then ensure that the correct prefix is used.

Finally if you have completed all the previous steps and still get a #NAME? error you can examine a valid Excel file to see what the correct syntax should be. To do this you should create a valid formula in Excel and save the file. You can then examine the XML in the unzipped file.

The following shows how to do that using Linux unzip and `libxml's xmllint to format the XML for clarity:

$ unzip myfile.xlsx -d myfile
$ xmllint --format myfile/xl/worksheets/sheet1.xml | grep '<f>'

        <f>SUM(1, 2, 3)</f>

Formula Results

Libxlsxwriter doesn't calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.

This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don't have a facility to calculate formulas will only display the 0 results. Examples of such applications are Excel Viewer, PDF Converters, and some mobile device applications.

If required, it is also possible to specify the calculated result of the formula using the result parameter for :func:worksheet_write_formula_num():

worksheet_write_formula_num(worksheet, 0, 0, "=2+2", NULL, 4);

Next: Working with Dates and Times