Accessing data passed over to JSReport POST API



  • Hi, I want to access data that is passed to the POST request in a loop within an xlsx recipe. There wasn't any issue passing the data outside the loop, only within a loop. Below is a sample of what I'm working on:

    {{#each data}}
    {{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}
    <row>
        <c t="inlineStr"><is><t>{{ #}}</t></is></c>
        <c t="inlineStr"><is><t>{{ name}}</t></is></c>
    </row>
    {{/xlsxAdd}}
    {{/each}}
    

    The data passed into the request body is as such:

    {
        "docs":[
            {
                "#":"2",
                "Name":"James"
            },
            {
                "#":"23",
                "Name":"Chloe"
            }
        ]
    }
    

    Thanks



  • It seems you loop over data property {{#each data}} but in the request, you have an array in docs property?



  • Oh! okay, i think i understand now, Thanks!
    But what if I want to pass data such as below? (I have a set of data in this format returned by another API call)
    [{"name":"James","age":12},
    {"name":"Chloe", "age": 5}]



  • I tried assigning the data array into a variable like this:

    var excelData = {"print":data} ;

    but I get a network error. The response from the API fails to load as well



  • I am confused.
    If it is possible, please create a playground demo, share a link here and describe what is the issue you want to solve there.



  • https://playground.jsreport.net/w/hernglx/fkqo1UQc
    this is the demo. An example of data that I want to pass to the API request can be seen in the sample data.
    If possible, I would like to know how to control the height of the cells as well.



  • Thank you for the demo. But what is wrong with it? It seems you have done it right and it works as it should.

    To find out how to set custom height...

    • open excel and create a one row sheet with custom height
    • save it
    • rename the xlsx file to zip file
    • decompress zip and look for file sheet1.xml
    • analyze the file and try to figure out what is there for height

    I see there something like this:

    <row r="1" spans="1:1" ht="105" customHeight="1" x14ac:dyDescent="0.35">
    <c r="A1" t="s">
      <v>0</v>
    </c>
    </row>
    

    The ht and customHeight attributes seem to be doing what you are looking for.



  • Thanks for the reply! I am able to control the height of each row successfully now.

    As for the data, it just wouldn't loop properly in the template. I think its probably because the data wasn't in object format. I was able to get it to work like this:

    //API data = excel
    //change array to object
        var rv = {};
        for (var i = 0; i < excel.length; ++i)
         { rv[i] = excel[i];}
    
        excel = {rv};
    

    Once again, thanks a lot for your help !


Log in to reply
 

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