Added docs on working with formulas.

Closed #150
This commit is contained in:
John McNamara 2018-03-18 00:58:58 +00:00
parent 88bf28d388
commit 8b708b2885
9 changed files with 275 additions and 9 deletions

View File

@ -768,6 +768,7 @@ INPUT = src/mainpage.dox \
../include/xlsxwriter/common.h \
src/working_with_formats.dox \
src/working_with_colors.dox \
src/working_with_formulas.dox \
src/working_with_dates.dox \
src/working_with_charts.dox \
src/working_with_data_validation.dox \

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -7,8 +7,7 @@ Libxlsxwriter was written by John McNamara.
- [Twitter \@jmcnamara13](https://twitter.com/jmcnamara13)
Donations
---------
@section donations Donations
If you would like to donate to the libxlsxwriter project to keep it active
you can do so via

View File

@ -9,7 +9,7 @@ how to submit bug reports.
@section bugs_unreadable Content is Unreadable. Open and Repair
Very, very occasionally you may encounter a bug which generates an Excel
Very occasionally you may encounter a bug which generates an Excel
warning when opening an `libxlsxwriter` file like:
> Excel could not open file.xlsx because some content is unreadable. Do
@ -19,8 +19,7 @@ This ominous sounding message is Excel's standard warning for any validation
error in the XML used for the components of the XLSX file.
If you encounter an issue like this you should open an issue on GitHub with a
program to replicate the issue (see below) or send one of the failing output
files to the @ref author.
program to replicate the issue (see below).
@section bugs_formula_name Formulas displayed as 'NAME?' until edited
@ -35,8 +34,11 @@ When written using `write_formula()` these functions need to be fully
qualified with the `_xlfn.` prefix as they are shown in the MS XLSX
documentation link above. For example:
@code
worksheet_write_formula(worksheet, 0, 0,"=_xlfn.STDEV.S(B1:B10)", NULL);
@endcode
See also @ref working_with_formulas.
@section bugs_formula_zero Formula results displaying as zero in non-Excel applications
@ -54,6 +56,7 @@ applications, will only display the 0 results.
If required, it is also possible to specify the calculated result of the
formula using the `worksheet_write_formula_num()` function.
See also @ref working_with_formulas.
@section bugs_images Images not displayed correctly in Excel 2001 for Mac and non-Excel applications

View File

@ -49,6 +49,7 @@ following sections for more information:
- @ref working_with_formats
- @ref working_with_colors
- @ref working_with_formulas
- @ref working_with_dates
- @ref working_with_charts
- @ref working_with_data_validation

View File

@ -41,6 +41,6 @@ The color names and corresponding RRGGBB value are shown below:
as 0x1000000 to avoid confusion with an undefined or Zero color value. It is
converted to the correct Html code internally.
Next: @ref working_with_dates
Next: @ref working_with_formulas
*/

View File

@ -0,0 +1,260 @@
/**
@page working_with_formulas Working with Formulas
@tableofcontents
In general a formula in Excel can be used directly in the
worksheet_write_formula() function:
@code
worksheet_write_formula(worksheet, 0, 0, "=10*B1 + C1", NULL);
@endcode
@image html working_with_formulas1.png
However, there are a few potential issues and differences that the user should
be aware of. These are explained in the following sections.
@section ww_formulas_non_us 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:
@code
// 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);
@endcode
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:
@code
// 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);
@endcode
If you have a non-English version of Excel you can use the following
multi-lingual [Formula Translator](http://en.excel-translator.de/language/)
to help you convert the formula. It can also replace semi-colons with commas.
@section ww_formulas_future 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:
@code
worksheet_write_formula(worksheet, 0, 0, "=_xlfn.STDEV.S(B1:B10)", NULL);
@endcode
They will appear without the prefix in Excel:
@image html working_with_formulas2.png
The following list is taken from
[MS XLSX extensions documentation on future functions](http://msdn.microsoft.com/en-us/library/dd907480%28v=office.12%29.aspx).
| Future Functions |
| -------------------------------- |
| `_xlfn.ACOT` |
| `_xlfn.ACOTH` |
| `_xlfn.AGGREGATE` |
| `_xlfn.ARABIC` |
| `_xlfn.BASE` |
| `_xlfn.BETA.DIST` |
| `_xlfn.BETA.INV` |
| `_xlfn.BINOM.DIST` |
| `_xlfn.BINOM.DIST.RANGE` |
| `_xlfn.BINOM.INV` |
| `_xlfn.BITAND` |
| `_xlfn.BITLSHIFT` |
| `_xlfn.BITOR` |
| `_xlfn.BITRSHIFT` |
| `_xlfn.BITXOR` |
| `_xlfn.CEILING.MATH` |
| `_xlfn.CEILING.PRECISE` |
| `_xlfn.CHISQ.DIST` |
| `_xlfn.CHISQ.DIST.RT` |
| `_xlfn.CHISQ.INV` |
| `_xlfn.CHISQ.INV.RT` |
| `_xlfn.CHISQ.TEST` |
| `_xlfn.COMBINA` |
| `_xlfn.CONCAT` |
| `_xlfn.CONFIDENCE.NORM` |
| `_xlfn.CONFIDENCE.T` |
| `_xlfn.COT` |
| `_xlfn.COTH` |
| `_xlfn.COVARIANCE.P` |
| `_xlfn.COVARIANCE.S` |
| `_xlfn.CSC` |
| `_xlfn.CSCH` |
| `_xlfn.DAYS` |
| `_xlfn.DECIMAL` |
| `ECMA.CEILING` |
| `_xlfn.ERF.PRECISE` |
| `_xlfn.ERFC.PRECISE` |
| `_xlfn.EXPON.DIST` |
| `_xlfn.F.DIST` |
| `_xlfn.F.DIST.RT` |
| `_xlfn.F.INV` |
| `_xlfn.F.INV.RT` |
| `_xlfn.F.TEST` |
| `_xlfn.FILTERXML` |
| `_xlfn.FLOOR.MATH` |
| `_xlfn.FLOOR.PRECISE` |
| `_xlfn.FORECAST.ETS` |
| `_xlfn.FORECAST.ETS.CONFINT` |
| `_xlfn.FORECAST.ETS.SEASONALITY` |
| `_xlfn.FORECAST.ETS.STAT` |
| `_xlfn.FORECAST.LINEAR` |
| `_xlfn.FORMULATEXT` |
| `_xlfn.GAMMA` |
| `_xlfn.GAMMA.DIST` |
| `_xlfn.GAMMA.INV` |
| `_xlfn.GAMMALN.PRECISE` |
| `_xlfn.GAUSS` |
| `_xlfn.HYPGEOM.DIST` |
| `_xlfn.IFNA` |
| `_xlfn.IFS` |
| `_xlfn.IMCOSH` |
| `_xlfn.IMCOT` |
| `_xlfn.IMCSC` |
| `_xlfn.IMCSCH` |
| `_xlfn.IMSEC` |
| `_xlfn.IMSECH` |
| `_xlfn.IMSINH` |
| `_xlfn.IMTAN` |
| `_xlfn.ISFORMULA` |
| `ISO.CEILING` |
| `_xlfn.ISOWEEKNUM` |
| `_xlfn.LOGNORM.DIST` |
| `_xlfn.LOGNORM.INV` |
| `_xlfn.MAXIFS` |
| `_xlfn.MINIFS` |
| `_xlfn.MODE.MULT` |
| `_xlfn.MODE.SNGL` |
| `_xlfn.MUNIT` |
| `_xlfn.NEGBINOM.DIST` |
| `NETWORKDAYS.INTL` |
| `_xlfn.NORM.DIST` |
| `_xlfn.NORM.INV` |
| `_xlfn.NORM.S.DIST` |
| `_xlfn.NORM.S.INV` |
| `_xlfn.NUMBERVALUE` |
| `_xlfn.PDURATION` |
| `_xlfn.PERCENTILE.EXC` |
| `_xlfn.PERCENTILE.INC` |
| `_xlfn.PERCENTRANK.EXC` |
| `_xlfn.PERCENTRANK.INC` |
| `_xlfn.PERMUTATIONA` |
| `_xlfn.PHI` |
| `_xlfn.POISSON.DIST` |
| `_xlfn.QUARTILE.EXC` |
| `_xlfn.QUARTILE.INC` |
| `_xlfn.QUERYSTRING` |
| `_xlfn.RANK.AVG` |
| `_xlfn.RANK.EQ` |
| `_xlfn.RRI` |
| `_xlfn.SEC` |
| `_xlfn.SECH` |
| `_xlfn.SHEET` |
| `_xlfn.SHEETS` |
| `_xlfn.SKEW.P` |
| `_xlfn.STDEV.P` |
| `_xlfn.STDEV.S` |
| `_xlfn.SWITCH` |
| `_xlfn.T.DIST` |
| `_xlfn.T.DIST.2T` |
| `_xlfn.T.DIST.RT` |
| `_xlfn.T.INV` |
| `_xlfn.T.INV.2T` |
| `_xlfn.T.TEST` |
| `_xlfn.TEXTJOIN` |
| `_xlfn.UNICHAR` |
| `_xlfn.UNICODE` |
| `_xlfn.VAR.P` |
| `_xlfn.VAR.S` |
| `_xlfn.WEBSERVICE` |
| `_xlfn.WEIBULL.DIST` |
| `WORKDAY.INTL` |
| `_xlfn.XOR` |
| `_xlfn.Z.TEST` |
@section ww_formulas_errors Dealing with formula errors
If there is an error in the syntax of a formula it is usually displayed in
Excel as @c \#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 @b 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
@ref ww_formulas_non_us above.
3. Ensure the formula is in English, see @ref ww_formulas_non_us above.
4. Ensure that the formula doesn't contain an Excel 2010+ future function as
listed above (@ref ww_formulas_future). If it does then ensure that the
correct prefix is used.
Finally if you have completed all the previous steps and still get a
@c \#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](http://xmlsoft.org/xmllint.html) 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>
@section ww_formulas_results 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()`:
@code
worksheet_write_formula_num(worksheet, 0, 0, "=2+2", NULL, 4);
@endcode
Next: @ref working_with_dates
*/

View File

@ -966,6 +966,7 @@ lxw_error worksheet_write_string(lxw_worksheet *worksheet,
* worksheet_write_formula(worksheet, 1, 0, "=SUM(1; 2; 3)", NULL);
* @endcode
*
* See also @ref working_with_formulas.
*/
lxw_error worksheet_write_formula(lxw_worksheet *worksheet,
lxw_row_t row,
@ -1296,6 +1297,7 @@ lxw_error worksheet_write_blank(lxw_worksheet *worksheet,
* worksheet_write_formula() function is the recommended way of writing
* formulas.
*
* See also @ref working_with_formulas.
*/
lxw_error worksheet_write_formula_num(lxw_worksheet *worksheet,
lxw_row_t row,
@ -2990,9 +2992,9 @@ void worksheet_protect(lxw_worksheet *worksheet, const char *password,
* @param auto_style Use Automatic outline style.
*
* The `%worksheet_outline_settings()` method is used to control the
* appearance of outlines in Excel. Outlines are described the section on
* appearance of outlines in Excel. Outlines are described the section on
* @ref working_with_outlines.
*
*
* The `visible` parameter is used to control whether or not outlines are
* visible. Setting this parameter to False will cause all outlines on the
* worksheet to be hidden. They can be un-hidden in Excel by means of the
@ -3016,7 +3018,7 @@ void worksheet_protect(lxw_worksheet *worksheet, const char *password,
*
* The default settings for all of these parameters in libxlsxwriter
* correspond to Excel's default parameters and are shown below:
*
*
* @code
* worksheet_outline_settings(worksheet1, LXW_TRUE, LXW_TRUE, LXW_TRUE, LXW_FALSE);
* @endcode