Working with Formulas

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.

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 worksheet_worksheet_write_formula_num():

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

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.

Dynamic Array support

Excel introduced the concept of "Dynamic Arrays" and new functions that use them in Office 365. The new functions are:

  • SORT

The following special case functions were also added with Dynamic Arrays:

These functions are all "future functions" and need to written in libxlsxwriter as follows:

  • _xlfn.LAMBDA
  • _xlfn.RANDARRAY
  • _xlfn.SEQUENCE
  • _xlfn.SINGLE
  • _xlfn.SORTBY
  • _xlfn.UNIQUE
  • _xlfn.XLOOKUP
  • _xlfn.XMATCH
  • _xlfn._xlws.FILTER
  • _xlfn._xlws.SORT

Future functions are explained in the section below on Formulas added in Excel 2010 and later.

Dynamic Arrays - An introduction

Dynamic arrays in Excel are ranges of return values that can change in size based on the results. For example, a function such as FILTER() returns an array of values that can vary in size depending on the the filter results:


This formula gives the results shown in the image below. The dynamic range here is "F2:I5" but it can vary based on the filter criteria.

It is also possible to get dynamic array behavior with older Excel functions. For example, the Excel function "=LEN(A1)" applies to a single cell and returns a single value but it can also apply to a range of cells and return a range of values using an array formula like "{=LEN(A1:A3)}". This type of "static" array behavior is referred to as a CSE (Ctrl+Shift+Enter) formula and has existed in Excel since early versions. In Office 365 Excel updated and extended this behavior to create the concept of dynamic arrays. In Excel 365 you can now write the previous LEN function as "=LEN(A1:A3)" and get a dynamic range of return values. In libxlsxwriter you can use the worksheet_write_array_formula() function to get a static/CSE range and worksheet_write_dynamic_array_formula() to get a dynamic range. For example:


Which gives the following result:

The difference between the two types of array functions is explained in the Microsoft documentation on Dynamic array formulas vs. legacy CSE array formulas. Note the use of the word "legacy" here. This, and the documentation itself, is a clear indication of the future importance of dynamic arrays in Excel.

For a wider and more general introduction to dynamic arrays see the following: Dynamic array formulas in Excel.

The worksheet_write_dynamic_array_formula() function takes a (first_row, first_col, last_row, last_col) cell range to define the area that the formula applies to. However, since the range is dynamic this generally won't be known in advance in which case you can specify the range with the same start and end cell. The following range is "F2:F2":


As a syntactic shortcut you can use the worksheet_write_dynamic_formula() function which only requires the start cell:


Dynamic Arrays - The Implicit Intersection Operator "@"

The Implicit Intersection Operator, "@", is used by Excel 365 to indicate a position in a formula that is implicitly returning a single value when a range or an array could be returned.

We can see how this operator works in practice by considering the formula we used in the last section: =LEN(A1:A3). In Excel versions without support for dynamic arrays, i.e. prior to Excel 365, this formula would operate on a single value from the input range and return a single value, like the following in Excel 2011:

There is an implicit conversion here of the range of input values, "A1:A3", to a single value "A1". Since this was the default behavior of older versions of Excel this conversion isn't highlighted in any way. But if you open the same file in Excel 365 it will appear as follows:

The result of the formula is the same (this is important to note) and it still operates on, and returns, a single value. However the formula now contains a "@" operator to show that it is implicitly using a single value from the given range.

Finally, if you entered this formula in Excel 365, or with worksheet_write_dynamic_array_formula() in libxlsxwriter, it would operate on the entire range and return an array of values:

If you are encountering the Implicit Intersection Operator "@" for the first time then it is probably from a point of view of "why is Excel/libxlsxwriter putting @s in my formulas". In practical terms if you encounter this operator, and you don't intend it to be there, then you should probably write the formula as a CSE or dynamic array function using worksheet_write_array_formula() or worksheet_write_dynamic_array_formula().

A full explanation of this operator is shown in the Microsoft documentation on the Implicit intersection operator: @.

One important thing to note is that the "@" operator isn't stored with the formula. It is just displayed by Excel 365 when reading "legacy" formulas. However, it is possible to write it to a formula, if necessary, using _xlfn.SINGLE(). The unusual cases where this may be necessary are shown in the linked document in the previous paragraph.

Dynamic Arrays - The Spilled Range Operator "#"

In the section above on Dynamic Arrays - An introduction we saw that dynamic array formulas can return variable sized ranges of results. The Excel documentation refers to this as a "Spilled" range/array from the idea that the results spill into the required number of cells. This is explained in the Microsoft documentation on Dynamic array formulas and spilled array behavior.

Since a spilled range is variable in size a new operator is required to refer to the range. This operator is the Spilled range operator and it is represented by "#". For example, the range F2# in the image below is used to refer to a dynamic array returned by UNIQUE() in the cell F2:

Unfortunately, Excel doesn't store the formula like this and in libxlsxwriter you need to use the explicit function _xlfn.ANCHORARRAY() to refer to a spilled range. The example in the image above was generated using the following:

// Same as '=COUNTA(F2#)' in Excel.

The Excel 365 LAMBDA() function

Recent versions of Excel 365 have introduced a powerful new function/feature called LAMBDA(). This is similar to lambda expressions in C++ (and other languages).

Consider the following Excel example which converts the variable temp from Fahrenheit to Celsius:

LAMBDA(temp, (5/9) * (temp-32))

This could be called in Excel with an argument:

=LAMBDA(temp, (5/9) * (temp-32))(212)

Or assigned to a defined name and called as a user defined function:


An libxlsxwriter example that replicates the described Excel functionality is

shown below:

// Write the lambda as a function.
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))(32)",
// Create the lambda function as a defined name and write it as a dynamic formula.
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))");
worksheet_write_dynamic_formula(worksheet, CELL("A3"), "=ToCelsius(212)", NULL);

Note, that the formula name must have a "_xlfn." prefix and the parameters in the LAMBDA() function must have a "_xlpm." prefix for compatibility with how the formulas are stored in Excel. These prefixes won't show up in the formula, as shown in the image.

The LET() function is often used in conjunction with LAMBDA() to assign names to calculation results.

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

The dynamic array functions shown in the Dynamic Array support section above are also future functions:

Dynamic Array 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.
  5. If the function loads in Excel but appears with one or more @ symbols added then it is probably an array function and should be written using worksheet_write_array_formula() or worksheet_write_dynamic_array_formula() (see the sections above on Dynamic Array support and Dynamic Arrays - The Implicit Intersection Operator "@").

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>

Next: Working with Dates and Times

lxw_error worksheet_write_formula(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format)
Write a formula to a worksheet cell.
lxw_error worksheet_write_dynamic_formula(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format)
Write an Excel 365 dynamic array formula to a worksheet cell.
lxw_error workbook_define_name(lxw_workbook *workbook, const char *name, const char *formula)
Create a defined name in the workbook to use as a variable.
#define RANGE(range)
Convert an Excel A1:B2 range into a (first_row, first_col, last_row, last_col) sequence.
Definition: utility.h:82
lxw_error worksheet_write_dynamic_array_formula(lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, const char *formula, lxw_format *format)
Write an Excel 365 dynamic array formula to a worksheet range.
lxw_error worksheet_write_formula_num(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format, double result)
Write a formula to a worksheet cell with a user defined numeric result.
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:45