Possible to use custom function on expression inside repeat table?
See original GitHub issueI’m trying to call a custom function on an expression inside the repeat table and it doesn’t work. According to the document, “Expressions found in the cells of the table row are evaluated against the object from the list”. It can be inferred from the quote that in my case, the expression is evaluated against Map<String,String>
in which the custom function is not present. Am i missing something or this feature hasn’t been implemented yet?
Here are the codes:
public interface DateFormatFunction {
String formatDate(final String dateString, final String format) throws ParseException;
}
public static class DateFormatFunctionImpl implements DateFormatFunction {
private static final String INPUT_FORMAT = "dd/MM/YYYY";
@Override
public String formatDate(final String inputDateString, final String outputFormat) throws ParseException {
final SimpleDateFormat inputFormatter = new SimpleDateFormat(INPUT_FORMAT);
final Date inputDate = inputFormatter.parse(inputDateString);
final SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat);
return outputFormatter.format(inputDate);
}
}
public static class TemplateContext {
private List<Map<String, String>> items;
public List<Map<String, String>> getItems() {
return items;
}
public void setItems(List<Map<String, String>> items) {
this.items = items;
}
}
@Test
public void testMap2() throws FileNotFoundException, IOException {
TemplateContext context = new TemplateContext();
List<Map<String, String>> csvRecords = parseCSV(new File("sample_1.csv"));
context.setItems(csvRecords);
try (FileInputStream inputStream = new FileInputStream("report_template.docx")) {
DocxStamper<TemplateContext> stamper = new DocxStamperConfiguration()
.exposeInterfaceToExpressionLanguage(DateFormatFunction.class, new DateFormatFunctionImpl())
.build();
final ByteArrayOutputStream reportOutputStream = new ByteArrayOutputStream();
stamper.stamp(inputStream, context, reportOutputStream);
final ByteArrayInputStream reportInputStream = new ByteArrayInputStream(reportOutputStream.toByteArray());
try (FileOutputStream pdfFileOutputStream = new FileOutputStream("report_result.pdf")) {
convertDocxToPdf(reportInputStream, pdfFileOutputStream);
}
}
}
My template:
Thanks, Le
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:5
Top Results From Across the Web
Custom Functions Made Easy in Power BI Desktop - RADACAD
It is easy to consume a function in Query Editor from a table. Go to Add Columns, and click on Invoke Custom Function...
Read more >Passing expression to custom function in R - Stack Overflow
I need to use replicate inside a custom function (the details are not important but it involves random number generation. Let's say I'm...
Read more >Custom Function in Query - Microsoft Power BI Community
Solved: I Can do this task using modelling and DAX. But i want it using "invoke Custom Functions". Requirement is i have key...
Read more >Custom eval functions - Splunk Documentation
You can use your custom eval functions in multiple searches within the same SPL module. To use a custom function in another SPL...
Read more >Learn SQL: User-Defined Functions - SQLShack
Note: You'll call a function by simply using its name and providing the parameters needed. If the function is value-based, then you'll be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I updated the test RepeatTableRowTest on a fork branch to reproduce the issue
https://github.com/alvinmeimoun/docx-stamper/tree/issue/60/reproduce
EDIT : Made a workaround https://github.com/alvinmeimoun/docx-stamper/tree/issue/60/workaround I didn’t made a PR because i’m not 100% confident of my implementation. And with my implementation all stamped classes must have an empty constructor so it can break source for some users.
If i find some free time i will check more but this workaround will unblock my actual project and i will use it until a better fix will be merged and released
@alvinmeimoun, @KasparScherrer, @vegemite4me we’ll take care of this subject when possible in our fork. https://github.com/verronpro/docx-stamper/