Export excel width size, based on header value

Export excel width size, based on header value

MoluMolu Posts: 16Questions: 3Answers: 0

export excel width size, based on header value. If in UI user re-arrange the column, width will remain same.
e.g- Name column is in index first - width can be col[1].attr("width",35);
but user rearrange the column and now Name column index is 4, so how can we set width based on column name.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • MoluMolu Posts: 16Questions: 3Answers: 0

    @colin - Request you to pls help on this.

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    You mentioned re-ordering in the original post, but that's not possible in your example. Please can you give steps on how to reproduce the issue you want help with,

    Colin

  • MoluMolu Posts: 16Questions: 3Answers: 0

    $('col', sheet).each(function(x) {
    console.log(x)
    console.log($('c[r=C'+x+'] t', sheet).text() );
    if ($('c[r=C'+x+'] t', sheet).text() === 'Office') {
    console.log("true");

            $(col[x+1]).attr('width', 100);
    
          }
          if($('c[r=A'+x+'] t', sheet).text() === 'Name'){
            console.log("true1");
            $(col[x+1]).attr('width', 50);
    
          }
    
      });
    
  • MoluMolu Posts: 16Questions: 3Answers: 0

    Above code is not working can any one pls help

  • MoluMolu Posts: 16Questions: 3Answers: 0

    var arr=["A","B","C","D","E","F"];
    for(let i=0;i<=6;i++){
    console.log($('row c[r=arr[i]'+1+'] t', sheet).text() )
    if($('row c[r=arr[i]'+1+'] t', sheet).text()==="Position"){}
    arr++;
    }

    can any one help me on this, this code giving me error.

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772
    Answer ✓

    Start by looking at the OpenXML docs to see how the cell selector works. You have if ($('c[r=C'+x+'] t' where x is the column index from the loop. However this is iterating column C from row 0 to row 5 giving the output of the cells in column 0. What you need to do is map the index the corresponding Excel column letter, ie, 0 = A, 1 = B, etc.

    Stack Overflow has some options for this. I updated the test case to show this:
    http://live.datatables.net/leyidetu/1/edit

    If you have more than 26 columns you will need to add the modulo calculation.

    Kevin

  • MoluMolu Posts: 16Questions: 3Answers: 0

    Thank you @kthorngren I am able to do so by below code, can you pls help me to do, text wrap of header column?
    var arr=["A","B","C","D","E","F"];
    $('col', sheet).each(function(x) {
    const value=$('c[r='+arr[x]+'1] t', sheet).text();
    console.log(value);

        if (value === 'Office') {
    
          $(col[x]).attr('width', 100); 
    
          }
         else if (value === 'Name') {
    
          $(col[x]).attr('width', 5);
    
           console.log("done");
    
          }
         else{
           $(col[x]).attr('width', 50); 
         }
    
    
      });
    
Sign In or Register to comment.