libxlsxwriter
lambda.c
<< ignore_errors.c chart.c >>

Example of using the new Excel LAMBDA() function. It demonstrates how to create a lambda function in Excel and also how to assign a name to it so that it can be called as a user defined function. This particular example converts from Fahrenheit to Celsius.

lambda01.png
/*
* An example of using the new Excel LAMBDA() function with the libxlsxwriter
* library.
*
* Copyright 2014-2026, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("lambda.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Note that the formula name is prefixed with "_xlfn." and that the
* lambda function parameters are prefixed with "_xlpm.". These prefixes
* won't show up in Excel.
*/
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))(32)",
NULL);
/* Create the lambda function as a defined name and write it as a dynamic formula. */
"ToCelsius",
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))");
worksheet_write_dynamic_formula(worksheet, CELL("A2"), "=ToCelsius(212)", NULL);
workbook_close(workbook);
return 0;
}