I'm using DataTables plugin to export a monthly calendar view; I need to set a cell style inside the excel file based on the class of the corrisponding cell in the DataTables calendar view.
I know how to style the exported excel file using the customize: function( xlsx, index ) {}
however I'm not able to find, in the examples I saw on the forum, a way to set the style of the excel cell based on the class of the corrispondig cell in the DataTables view.
I have created my own xml style like this:
customize: function( xlsx, index ) {
var new_style = '<?xml version="1.0" encoding="UTF-8"?>'+
'<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" '+
'xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" '+
'xmlns:x14ac="https://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac">'+
'<numFmts count="2">'+
'<numFmt numFmtId="164" formatCode="0.0" />'+
'<numFmt numFmtId="165" formatCode="\d\d\d" />'+
'</numFmts>'+
'<fonts count="4" x14ac:knownFonts="1">'+
...... a lot of stuff here ....
'<extLst>'+
'<ext xmlns:x14="https://schemas.microsoft.com/office/spreadsheetml/2009/9/main" uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}">'+
'<x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1" />'+
'</ext>'+
'</extLst>'+
'</styleSheet>';
This is a styles.xml
as you can find iside an .xlsx
file if you change the extension to .zip
and than unzip it.
To apply the styles to the excel cell I'm doing:
xlsx.xl['styles.xml'] = $.parseXML(new_style);
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr( 's', '1' );
$('row:eq(1) c', sheet).attr( 's', '2' );
$('row:eq(2) c', sheet).attr( 's', '3' );
}
What I need to do is something like:
$('row:eq(3) c', sheet).hasClass('custom').attr( 's', '1' ); //not working
or:
$('row c[r^="B"]', sheet).each( function () {
if ( $(this).hasClass('custom') ) {
$(this).attr( 's', '4' );
}
}); // not working
Basically I'm working on a row of cells (more than 30, so I have AA, AB, AC and so on) and i need a method to discriminate some of them to add a different style, as you can see the header has 31 cells with calendars day/name and i would like the colums with Saturday and Sunday to be with a gray background like they are in the datatable table.
This is the datatable:
And this is the excel file so far, i need the Sab and Dom columns to be gray
UPDATE * with @andrewjames solution and @georg solution for double letters posted here Convert numbers to letters beyond the 26 character alphabet
function colName(n) {
var ordA = 'A'.charCodeAt(0);
var ordZ = 'Z'.charCodeAt(0);
var len = ordZ - ordA + 1;
var s = "";
while(n >= 0) {
s = String.fromCharCode(n % len + ordA) + s;
n = Math.floor(n / len) - 1;
}
return s;
}
var cellIndexes = tabellaOre.cells(".Sab, .Dom").indexes();
for (var i = 0; i < cellIndexes.length; i++) {
var cellIndex = cellIndexes[i];
var tblRow = cellIndex['row']+4; //changed to my needs
var tblCol = cellIndex['column']; //removed +1
// var xlCol = String.fromCharCode(64 + tblCol); changed with follow
var xlCol = colName(tblCol);
// .... previous stuff here, it was already in a for() loop, so still working
$('row c[r='+xlCol+tblRow+']', sheet).attr('s','12');
}
and this is the result:
As @andrewjames correctly says in his anwer:
My naive implementation will fail for more than 26 columns:
The colName(n)
function solved the problem.
One last step would be to style the cells with thick borders with their own style, but I can consider this as solved.