Skip to content

Commit 53512d8

Browse files
committed
Show example on how to implement SUM in Word
Using navigation expression we can iterate over all elements perform SUM aggregation over a property/field.
1 parent f1eb712 commit 53512d8

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

Intermediate/WordTables/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ alias can be defined like products = groups:sort(usage).products:filter(active)
3434

3535
and tag can look like `[[products.name]]` with all the behavior which was implicitly defined
3636

37-
Here, navigation was used to take top N elements from the result set in an idiomatic way.
37+
Here, navigation was used to take top N elements from the result set in an idiomatic way and for implementing Sum over existing field as `[[Fixed:Sum(Price)]]`
3838

3939
### merge-nulls/span-nulls metadata
4040

-5.59 KB
Binary file not shown.

Intermediate/WordTables/src/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ static object LimitDataTable(object parent, object value, string member, string
118118
return dt;
119119
}
120120

121+
static object SumExpression(object parent, object value, string member, string metadata)
122+
{
123+
var arr = value as object[];
124+
//check if plugin is applicable
125+
if (arr == null || !metadata.StartsWith("Sum(")) return value;
126+
if (arr.Length == 0 || arr.Contains(null)) return 0;
127+
var signature = arr[0].GetType();
128+
var propertyName = metadata.Substring(4, metadata.Length - 5);
129+
var property = signature.GetField(propertyName);
130+
var result = (decimal)0;
131+
foreach (var el in arr)
132+
result += (decimal)property.GetValue(el);
133+
return result;
134+
}
135+
121136
public static void Main(string[] args)
122137
{
123138
File.Copy("template/Tables.docx", "WordTables.docx", true);
@@ -139,6 +154,7 @@ public static void Main(string[] args)
139154
.NavigateSeparator(':', null)
140155
.Include(LimitDataTable)
141156
.Include(CollapseNonEmpty)
157+
.Include(SumExpression)
142158
.Build();
143159
var dynamicResize1 = new object[7, 3]{
144160
{"a", "b", "c"},

Intermediate/WordTables/src/main/java/hr/ngs/templater/example/WordTablesExample.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.awt.Desktop;
77
import java.io.*;
8+
import java.lang.reflect.Field;
89
import java.math.BigDecimal;
910
import java.sql.ResultSet;
1011
import java.sql.ResultSetMetaData;
@@ -88,6 +89,7 @@ public static void main(final String[] args) throws Exception {
8889
.navigateSeparator(':', null)
8990
.include(new LimitResultSet())
9091
.include(new CollapseNonEmpty())
92+
.include(new SumExpression())
9193
.build().open(templateStream, "docx", fos);
9294
tpl.process(arguments);
9395
tpl.close();
@@ -155,6 +157,29 @@ public Object navigate(Object parent, Object value, String member, String metada
155157
}
156158
}
157159

160+
static class SumExpression implements DocumentFactoryBuilder.Navigate {
161+
@Override
162+
public Object navigate(Object parent, Object value, String member, String metadata) {
163+
//check if plugin is applicable
164+
if (value instanceof List == false || !metadata.startsWith("Sum(")) return value;
165+
List arr = (List) value;
166+
if (arr.isEmpty() || arr.contains(null)) return 0;
167+
String propertyName = metadata.substring(4, metadata.length() - 1);
168+
Class<?> signature = arr.get(0).getClass();
169+
BigDecimal result = BigDecimal.ZERO;
170+
try {
171+
Field property = signature.getField(propertyName);
172+
for (Object el : arr) {
173+
BigDecimal f = (BigDecimal) property.get(el);
174+
result = result.add(f);
175+
}
176+
} catch (NoSuchFieldException | IllegalAccessException e) {
177+
throw new RuntimeException(e);
178+
}
179+
return result;
180+
}
181+
}
182+
158183
static class Limit10Table implements DocumentFactoryBuilder.Processor<ResultSet> {
159184
@Override
160185
public boolean tryProcess(String prefix, Templater templater, ResultSet resultSet) {
256 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)