question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to use custom function on expression inside repeat table?

See original GitHub issue

I’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: image

Thanks, Le

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

2reactions
alvinmeimouncommented, Oct 3, 2019

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

0reactions
josephverroncommented, Sep 30, 2022

@alvinmeimoun, @KasparScherrer, @vegemite4me we’ll take care of this subject when possible in our fork. https://github.com/verronpro/docx-stamper/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found