''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Collections.Generic
Imports System.Globalization
Imports GrapeCity.Documents.Word
'' This example demonstrates the available aggregate functions that can be used with the 'calc'
'' report templates feature for data aggregation.
'' Unlike other report templates constructs, aggregates do not cause repetition of content
'' in the generated document. Instead, they aggregate data and insert the single resulting
'' value into the document.
'' Aggregate functions' arguments can be expressions involving data, other functions and constants.
Public Class DataTplAggregates
Function CreateDocx() As GcWordDocument
'' Generate a simple data source with random numeric data:
Dim values = New List(Of Decimal)
Dim rnd = Util.NewRandom()
Dim count = rnd.Next(10, 20)
For i = 0 To count - 1
values.Add(New Decimal(Math.Round(CDbl(rnd.Next(0, 1000000)) / rnd.Next(1, 100), 2)))
Next
Dim doc = New GcWordDocument()
doc.Body.Sections.First.PageSetup.Margin.Top = 36
doc.Body.Sections.First.PageSetup.Margin.Bottom = 36
doc.Body.Sections.First.PageSetup.Margin.Left = 36
doc.Body.Sections.First.PageSetup.Margin.Right = 36
'' Add the data source:
doc.DataTemplate.DataSources.Add("ds", values)
'' Styles and templates to show results:
Dim numberedListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "numberedListTemplate")
Dim bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate")
Dim exStyle = doc.Styles(BuiltInStyleId.Heading3)
Dim resStyle = doc.Styles(BuiltInStyleId.Strong)
Dim paras = doc.Body.Paragraphs
'' Function to add an example expression and its result:
Dim add As Action(Of String) =
Sub(expr)
'' Four zero-width spaces are inserted after the opening brace to prevent template expansion:
paras.Add(expr.Insert(1, New String(ChrW(&H200B), 4)) + " : ", exStyle).ListFormat.Template = bulletListTemplate
paras.Last.GetRange().Runs.Add(expr, resStyle)
End Sub
'' Print data to be summarized:
paras.Add("Data values to aggregate:", doc.Styles(BuiltInStyleId.Heading2))
Dim p = paras.Add("{{#ds}}{{ds.value}}{{/ds}}", doc.Styles(BuiltInStyleId.ListParagraph))
p.ListFormat.Template = numberedListTemplate
'' Print the results:
paras.Add("Simple aggregates:", doc.Styles(BuiltInStyleId.Heading2))
add("{{ calc Average(ds.value) }}")
add("{{ calc Count(ds.value) }}")
add("{{ calc First(ds.value) }}")
add("{{ calc Last(ds.value) }}")
add("{{ calc Max(ds.value) }}")
add("{{ calc Min(ds.value) }}")
add("{{ calc Sum(ds.value) }}")
paras.Add("Aggregates on expressions:", doc.Styles(BuiltInStyleId.Heading2))
add("{{ calc Sum(ds.value / Count(ds.value)) }}")
add("{{ calc Average(Pow(ds.value, 2)) }}")
add("{{ calc Average(Sqrt(ds.value)) }}")
add("{{ calc Sum(Iif(ds.value > 50, ds.value, 0)) }}")
'' Process the templates:
doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
'' Add a short note describing the demo at the top of the document:
paras.Insert(
"This example demonstrates the available aggregate functions that can be used with the 'calc' " +
"report templates feature for data aggregation. " +
"Unlike other report templates constructs, aggregates do not cause repetition of content " +
"in the generated document. Instead, they aggregate data and insert the (single) resulting " +
"value into the document. " +
"For example, to insert the sum of a field's values in all records of a data source, " +
"the template '{{calc Sum(ds.value)}}' can be used. " +
"Aggregate functions' arguments can be expressions involving data, other functions and constants. " +
"The data source used in this demo is a list of random decimal values. " +
"Please see this example's source code for full details.",
InsertLocation.Start)
paras.Insert("Report templates: calc aggregate functions", doc.Styles(BuiltInStyleId.Heading1), InsertLocation.Start)
'' Done:
Return doc
End Function
End Class