//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
namespace GcWordWeb.Samples
{
// 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.
public class DataTplAggregates
{
public GcWordDocument CreateDocx()
{
// Generate a simple data source with random numeric data:
var values = new List<decimal>();
var count = Util.NextRnd(10, 20);
for (int i = 0; i < count; ++i)
values.Add(new decimal(Math.Round((double)Util.NextRnd(0, 1000000) / Util.NextRnd(1, 100), 2)));
var doc = new GcWordDocument();
// Add the data source:
doc.DataTemplate.DataSources.Add("ds", values);
// A list template to print the random data:
var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "myListTemplate");
// Print data to be summarized:
doc.Body.Paragraphs.Add("Data values to aggregate:", doc.Styles[BuiltInStyleId.Heading2]);
var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.value}}{{/ds}}", doc.Styles[BuiltInStyleId.ListParagraph]);
p.ListFormat.Template = myListTemplate;
// Print the results:
doc.Body.Paragraphs.Add("Results:", doc.Styles[BuiltInStyleId.Heading2]);
// Average (\x200B is a zero-width space to prevent template expansion):
doc.Body.Paragraphs.Add("{\x200b{ calc Average(ds.value) }} : {{ calc Average(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Count:
doc.Body.Paragraphs.Add("{\x200B{ calc Count(ds.value) }} : {{ calc Count(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// First:
doc.Body.Paragraphs.Add("{\x200B{ calc First(ds.value) }} : {{ calc First(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Last:
doc.Body.Paragraphs.Add("{\x200B{ calc Last(ds.value) }} : {{ calc Last(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Max:
doc.Body.Paragraphs.Add("{\x200B{ calc Max(ds.value) }} : {{ calc Max(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Min:
doc.Body.Paragraphs.Add("{\x200B{ calc Min(ds.value) }} : {{ calc Min(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Sum:
doc.Body.Paragraphs.Add("{\x200B{ calc Sum(ds.value) }} : {{ calc Sum(ds.value) }}", doc.Styles[BuiltInStyleId.Heading3]);
// Process the templates:
doc.DataTemplate.Process();
// Add a short note describing the demo at the top of the document:
doc.Body.Paragraphs.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. " +
"The data source used in this demo is a list of random decimal values. " +
"Please see this sample source code for full details.",
InsertLocation.Start);
doc.Body.Paragraphs.Insert("Report templates: calc aggregate functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
// Done:
return doc;
}
}
}