Excel Export force text format

Excel Export force text format

eddiefantasticeddiefantastic Posts: 7Questions: 3Answers: 0

I have a very similar issue to this...

https://datatables.net/forums/discussion/50970/excel-export-format-cells-as-text

...and the solution there does give the desired result, almost.

My problem is that I am also using colvis so I don't know which column letter(s) I need to apply the following to...

$("row c", sheet).attr( "s", "50" );

I would like to only apply that to columns where columnDef type = "string".

Any ideas?

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    There isn't an API method to get the column type unfortunately, so the best option is probably to apply a class to the columns that you have as strings - columns.className.

    Then in your customize function you can loop over the columns (columns().every()) and check the class name of each (column().node() to get the element and they use jQuery or classList to check the class name for the cell). If it matches, apply the 50 style to the corresponding column.

    Allan

  • eddiefantasticeddiefantastic Posts: 7Questions: 3Answers: 0

    Thanks for the help. Not pretty but this seems to work...

    var cell_letter = "A";
    table.columns().eq(0).each(function(index) {
        var column = table.column(index);
        if (column.visible()) {
            var node = column.nodes()[0];
            if ($(node).hasClass("dt-string")) {
                //console.log("We have a String on cell letter " + cell_letter);
                $("row c[r^=" + cell_letter + "]", sheet).attr("s", "50");
            }
            cell_letter = getNextChar(cell_letter);
        }
    });
    

    The extra function. This will need adjusting if I ever have more than 26 possible columns...

    function getNextChar(c) {
        return String.fromCharCode(c.charCodeAt(0) + 1);
    }
    
Sign In or Register to comment.