Calculation functions calculate inner expression and write result as output. Currently, a function subset of our Calc engine has been adapted to use with the template engine and hence the feature supports various binary, unary and arithmetic operators, aggregate functions, constants, and text functions. The examples below show how to use aggregate and arithmetic operations to create expressions in Calc function.
Refer to the following example code to calculate the average using multiple arguments:
C# |
Copy Code |
---|---|
// Initialize GcWordDocument. GcWordDocument doc = new GcWordDocument(); var ds = new int[] { 1, 2, -2, 16, 7, 5, 4 }; // Bind data source. doc.DataTemplate.DataSources.Add("ds", ds); // Calculate average using multiple arguments. doc.Body.Paragraphs.Add("{{ calc Average(ds.value+Sum(ds.value)/2)}}"); doc.DataTemplate.Process(); doc.Save("complex-expression-inside-average.docx"); |
Refer to the following example code to calculate the sum using a constant:
C# |
Copy Code |
---|---|
// Initialize GcWordDocument. var doc = new GcWordDocument(); var ds = new int[] { 1, 2, -2, 16, 7, 5, 4 }; // Bind data source. doc.DataTemplate.DataSources.Add("ds", ds); // Calculate sum using a constant. doc.Body.Paragraphs.Add("{{ calc Sum(2 + ds.value)}}"); doc.DataTemplate.Process(); doc.Save("sum-with-constant.docx"); |
Refer to the following example code to calculate the average using two collections inside the aggregate function:
C# |
Copy Code |
---|---|
// Initialize GcWordDocument. var doc = new GcWordDocument(); var ds = new int[] { 1, 2, -2, 16, 7, 5, 4 }; var ds2 = new int[] { 5, 3, }; // Bind data source. doc.DataTemplate.DataSources.Add("ds", ds); doc.DataTemplate.DataSources.Add("ds2", ds2); // Calculate average using two collections inside the aggregate function. doc.Body.Paragraphs.Add("{{ calc Average(ds.value+ds2.value)}}"); doc.DataTemplate.Process(); doc.Save("sum-with-two-colls.docx"); |
To view Calc functions in action, see Calculation demo sample.
DsWord provides Format function that can calculate inner expressions and output the result in different formats and cultures. The Format function is used with the Calc Expression.
Refer to the following example code to display the data in date formatting:
C# |
Copy Code |
---|---|
// Add dates in date format and different locales. var paras = doc.Body.Paragraphs; var p = paras.Add("Date formatting examples:", style); p.ListFormat.Template = list; p = paras.Add("Date with default formatting: {{calc Format(dsDate.value)}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Date formatted as ('D', 'en-US'): {{calc Format(dsDate.value, \"D\", \"en-US\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Date formatted as ('D', 'fr-FR'): {{calc Format(dsDate.value, \"D\", \"fr-FR\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Date formatted as ('D', 'ja-JP'): {{calc Format(dsDate.value, \"D\", \"ja-JP\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; |
Refer to the following example code to display the data in number formatting:
C# |
Copy Code |
---|---|
// Add number in different fromats and cultures. p = paras.Add("Number formatting examples:", style); p.ListFormat.Template = list; p = paras.Add("Number with default formatting: {{calc Format(dsNum.value)}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Number formatted as ('C', 'en-US'): {{calc Format(dsNum.value, \"C\", \"en-US\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Number formatted as ('C', 'fr-FR'): {{calc Format(dsNum.value, \"C\", \"fr-FR\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Number formatted as ('C3', 'ja-JP'): {{calc Format(dsNum.value, \"C3\", \"ja-JP\")}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; |
Refer to the following example code to display the data in expression formatting:
C# |
Copy Code |
---|---|
// Add expressions in different formats. p = paras.Add("Expression formatting examples:", style); p.ListFormat.Template = list; p = paras.Add("Numeric expression: {{calc Format(dsNum.value / 2)}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; p = paras.Add("Boolean expression: {{calc Format(IsFirst(dsNum.value))}}", style); p.ListFormat.Template = list; p.ListFormat.LevelNumber = 1; |
Limitations