Bug in Excel export?
Bug in Excel export?
I recently upgraded to the latest releases of Datatables, Editor, and a few other plugins and I noticed now that I get an error when I try to programmatically export to an Excel file:
datatables.js:38395 Uncaught TypeError: Cannot read property 'toString' of null
at _excelColWidth (datatables.js:38395)
at _Api.action (datatables.js:39015)
at action (datatables.js:35096)
at HTMLAnchorElement.<anonymous> (datatables.js:35111)
at HTMLAnchorElement.dispatch (jquery.js:4435)
at HTMLAnchorElement.elemData.handle (jquery.js:4121)
at Object.trigger (jquery.js:4350)
at HTMLAnchorElement.<anonymous> (jquery.js:4901)
at Function.each (jquery.js:374)
at jQuery.fn.init.each (jquery.js:139)
The code that triggers the export is:
myDT.button('0-0').trigger();
where the buttons
config is this:
//...
buttons: [
{ extend: 'collection',
text: 'Export',
buttons: [
{ extend: 'excel',
exportOptions: {
orthogonal: 'export',
}
},
'csv',
]
},
]
//...
The list of all my custom Datatables download build is: JSZip 2.5.0, Moment 2.13.0, jQuery Mask 1.13.4, Select2 4.0.1, DataTables 1.10.13, Buttons 1.2.4, Column visibility 1.2.4, Flash export 1.2.4, HTML5 export 1.2.4, ColReorder 1.3.2, Editor 1.6.1, Field type - Display 1.5.6, Field type - Mask 1.5.6, Select 1.2.0
This question has accepted answers - jump to:
Answers
Can you link to the page showing the issue so I can take a look please? It will be something related to the data in the table and I'd need to know what that data is to be able to replicate the issue and resolve it.
Allan
Unfortunately, I cannot link to the page because it's on an internal project that I don't have the right to make public. I can try to give you a JS export of the data. Here's the data in JSON for a table with two rows that has the bug:
Oh please excuse me, it was dumb of me not to mention where the bogue is in the DataTables code. It's on the 9th line of the function
_excelColWidth
. The line looks like this:It seems like it fails when the data in a given cell is
null
and not empty string. May I suggest an improvement to something like the following (it's what I've done in my local copy of DataTables) ? It works now after I made this change.That looks just about perfect - the only catch would be
false
sincefalse.toString() === "false"
. With the above that would be replaced with an empty string.How about the following?:
Allan
Looks good to me. Rant-y side note: it's a real shame in JS (AFAIK) that you cannot do something as simple as:
Because that's really what you're going for and semantically it makes more sense. You wanna know if
point
has a methodtoString
and if so, call it. sigh.Yup! Completely agree. I went around a few twists with
typeof
etc before I decided to just go with the above.Fix committed here. The nightly will rebuild with the change shortly (5 minutes) and it will be in the next release.
Thanks for this!
Allan
Awesome, thanks Allan!