Export to Excel with color (again)
Export to Excel with color (again)
I have been going around the houses all afternoon on this one. I have a table generated from AJAX that contains colour coding. The table contains thousands of rows and hundreds of columns. Some cells contain color coding on the background which I want to export. I have seen a few examples and discussions but they are all very specific like only affect column B or a specific color. I don't want to hardcode it, I just want it to export the color its set to wherever it is.
To do this, we customize the function fir excelHtml5 and loop through each cell table.cells().every and read the backgroundcolour attribute and then set the attribute to something similar using the stylesheet definitions. However, I can't get this to work. I'm sure its all fine the problem is that I don't get along with javascript's scattyness.
Ideally, I'd like to add a data attribute to my table at source e.g. data-attr='35' and then simply pass it through at export.
<td data-attr='35'>I want this cell to export with the attribute 35 - Normal text, red background, thin black border</td>
table.cells().every( function () {
var data = this.data();
var a = $(this).attr("data-attr");
if(a) {
console.log(counter, a)
}
This doesn't work as it doesn't pick it up - this syntax works elsewhere but not here (see why I'm often confused with js.) I don't really know what data actually is, if its the element, the data in between the tags or if its some magic thing from data tables.
Some examples use:
var color = $(data).css("background-color");
So this requires me to set the color and then create some mapping bodge in the export - I don't really want to do it that way, but even so I can't get it to pick up a color anyway. It doesn't work because it fell over setting data because it tried to evaluate the cell contents "S100 - Baked Beans" cannot evaluate expression; or if it got that far there is no property "css" which is odd as I directly lifted it from an example that works.
Replies
This example from this thread may help, as it's demonstrating how to change the colour. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.
Colin
p.s. apologies for the late reply, the spam filter caught your message.