Adding calculation expression in docx template



  • I am trying to do some basic calculation in docx template using handlebars in jsreport studio but I haven't been able to figure out how to write those expressions so that the docx recipe recognizes it.

    The use case is to add 2/3 column values, show total and some basic calculation with invoice which will have a table or a nested table.

    Please suggest how should I go about it.



  • Please try to share a playground demo so we can take a look there
    https://playground.jsreport.net/



  • I have created a sample document. Please find it here https://playground.jsreport.net/w/amitmishra3/HuZFiudt



  • Thank you. I try to take a look.



  • @jan_blaha any updates on this?



  • Sorry for the delay. Too much work with 2.6.0 release....

    You are trying to calculate data through {{price}} * {{qty}}.
    This may look to you as a multiplication but it is in fact just string concatenation.
    You need to do the math inside a helper function

    function itemTotal(price, qty) {
        return price * qty
    }
    

    and call it using {{itemTotal price, qty}}.
    The similar applies for the final total.

    Here you have the final demo
    https://playground.jsreport.net/w/anon/J5c7~HFx



  • Thanks a lot for the solution.
    In addition to this, if I am passing the docx template and data through a .NET code, do I have to pass the function as a string in the template object?



  • Yes. In case your template is not stored inside jsreport template store you need to pass helpers in

    Template.Helpers = "function itemTotal(price, qty) {
        return price * qty
    }
    
    function total() {
        return this.data.reduce((p, c) => p + (c.price * c.qty), 0)
    }"
    


  • That's very helpful.
    Does the .NET nuget package has docx recipe yet? or it's still being developed?



  • The local binary doesn't have it yet. Soon the 2.6.0 should have it.



  • That's great to know. Looking forward to it. Thanks a lot for the help.



  • I am trying to call the Docx engine through .net nuget package jsreport.Local --> jsreport.Client --> jsreport.Types. Since Docx recipe is not thr in enum I am not able to create Docx through code.
    Can you let me know when can we expect Docx recipe type in jsreport.client?



  • You don't need strong types. Just send an anonymous object using this interface method. The missing type shouldn't be a limitation for you.

    However, we will add types soon in the next days.



  • Thanks for your quick response. I tried to call the RenderAsync with Anonymous object but I am getting error related to recipe. Below is excerpts from my code.

    private static void GenerateWord(ReportingService rs)
    {
    var html = System.IO.File.ReadAllText(@"C:\temp\CV.htm");

            var prop = new JProperty( "name", "Test Name");
    
            var customReport = rs.RenderAsync(new {
                    Data = new JObject { prop } ,
                    Template = new {Content = html, Engine = Engine.Handlebars } } );
    
            using (var fs = File.Create(@"C:\temp\CV_converted.docx"))
            {
                customReport.Result.Content.CopyTo(fs);
            }
        }
    

    Above code gives Engine exception:
    Unable to render template. Engine '0' not found. If this is a custom engine make sure it's properly installed from npm

    Kindly share your thoughts. What should be the structure of anonymous object?



  • @meetvishalt There is now API section in the docx recipe documentation https://jsreport.net/learn/docx#api



  • @jan_blaha I tried passing the helper function as you suggested but I am getting an error. I have passed the function in json object as

    "helpers" : "function itemTotal(price, qty) {
    return price * qty
    }

    function total() {
    return this.data.reduce((p, c) => p + (c.price * c.qty), 0)
    }"



  • @amitmishra3 What error....?


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.