Excel customization: % with 4 decimal places
Excel customization: % with 4 decimal places
rf1234
Posts: 2,993Questions: 87Answers: 421
I am using the customize callback for Excel https://datatables.net/reference/button/excelHtml5
//custom button for cashflow excel generation
$.fn.dataTable.ext.buttons.excelCashFlow = {
extend: 'excel', filename: 'cashFlow', sheetName: 'cashflow1',
customize: function( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row c[r^="G"]', sheet).attr( 's', '60' ); //% 1 dec. place
},
exportOptions: {
format: {
body: function ( data, row, column, node ) {
// do something
},
header: function ( data, column ) {
// do something
}
}
}
};
I am also using the built in styling option 60 wich is % with one decimal place. My problem is that I need % with 4 decimal places. How can I get this done? Any ideas?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
There isn't a built in 4 dp percentage number formatter in the open spreadsheet spec, so you'd need to define your own. You can do that by adding a
numFmt
tag in here and then make use of that formatter by added a newxf
in here.I would suggest that you add those two tags in the
customize
callback so you don't need to modify the library every time you update.Allan
Thanks Allan, tried that yesterday as per this post by @j e harms https://datatables.net//forums/discussion/39707
but I couldn't get it working. I gave up ...
With the help of @bQt in this post https://datatables.net/forums/discussion/comment/116516/#Comment_116516 I got it working eventually. I added some inline comments. Hopefully this helps others too.
I added one new number format (percentage with 4 decimal places) and two styles (the style using the new number format and another style: bold and centered text, grey background which actually is a combination of the built in styles 7 and 51).
The numFmtId is arbitrary. Just take care you don't use a number that is already being used in an existing numFmt. For the styles: Since 67 styles (0 - 66) are currently built in I added styles 67 and 68. Whenever "buttons.html5.js" gets changed in a new release this may need maintenance! That's actually the only thing I dislike about this solution.
Here is my custom Excel export button with the additional styles and custom export options:
@allan: Would you have an idea how this could be avoided? (see above as well):
"For the styles: Since 67 styles (0 - 66) are currently built in I added styles 67 and 68. Whenever "buttons.html5.js" gets changed in a new release this may need maintenance! That's actually the only thing I dislike about this solution."
Yes, don't hard code the number, just count the number of elements instead:
$('xf', xlsx.xl['styles.xml']).length
or similar.The Excel export customisation is something I'm thinking of spinning out into a separate project. Might be a good one for an intern .
Allan
This XML stuff is driving me crazy. Makes me overlook even basic things. Sorry!
Ok I am counting the xfs now (whatever that means). Found that there are also xfs in
<cellStyleXfs> and more of them in <cellXfs> but I am only interested in the latter. Hence I changed the selector a bit:
So this is my code now (only the customize part, the rest is unchanged):
Thanks a lot for helping me out again, Allan!
Thanks for this practical case.
I ran into several more browser-related issues with this, I summarized the best way to go in this StackOverflow answer: https://stackoverflow.com/a/50719888/5426777.
Hope it will help you!
tried today your code @rf1234 .... it seems
//styleSheet.childNodes[0].childNodes[4]
//styleSheet.childNodes[0].childNodes[5]
has index inverted.... 5 is 4 and 4 is 5..... is it a typo error or some changes are done in the structure ?
I just double checked with file "buttons.html5.js" from the buttons extension version 1.6.1 and everything is ok and not inverted. The cellStyleXfs stuff comes before cellXfs.
Much simpler solution below. Can be added in
.customize
and you can change form as per your wish.It creates a new format and adds it to existing one. The identifies the cell format (mine is 9 but it could be 167 in your case) that applies to what data you give it and changes the the number formatting that corresponds to it.