lambda/lambda.rs
1use edit_xlsx::{Workbook, WorkbookResult, Write};
2
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8
9 // Write a Lambda function to convert Fahrenheit to Celsius to a cell.
10 //
11 // Note that the lambda function parameters must be prefixed with
12 // "_xlpm.". These prefixes won't show up in Excel.
13 worksheet.write_formula("A1", "_xlfn.LAMBDA(_xlpm.a, _xlpm.b, SQRT((_xlpm.a^2+_xlpm.b^2)))(3, 4)")?;
14
15
16 // The user defined name needs to be written explicitly as a dynamic array
17 // formula.
18 let a = 6;
19 let b = 8;
20 worksheet.write_formula("A2", &format!("=HYPOTENUSE({a}, {b})"))?;
21
22 // Create the same formula (without an argument) as a defined name and use that
23 // to calculate a value.
24 //
25 // Note that the formula name is prefixed with "_xlfn." (this is normally
26 // converted automatically by write_formula() but isn't for defined names)
27 // and note that the lambda function parameters are prefixed with
28 // "_xlpm.". These prefixes won't show up in Excel.
29 workbook.define_name("HYPOTENUSE", "_xlfn.LAMBDA(_xlpm.a, _xlpm.b, SQRT((_xlpm.a^2+_xlpm.b^2)))")?;
30
31
32 workbook.save_as("examples/lambda.xlsx")?;
33 Ok(())
34}