clarketm / TableExport

The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
https://tableexport.travismclarke.com/
Apache License 2.0
888 stars 289 forks source link

Option to remove commas if cell value will be a number without the commas? #147

Open nowherenearithaca opened 6 years ago

nowherenearithaca commented 6 years ago

Was wondering how hard it would be to have the option to remove commas if the value about to be written for a cell is a number without the comma. Otherwise, you need to muck with it on the excel side.

Similarly, replace things like "(123)" with "-123"

nowherenearithaca commented 6 years ago

ok - it looks like there is a "formatValue" function that is added to the settings object internally, but not currently settable via settings - wonder if just allowing that would work...

nowherenearithaca commented 6 years ago

that worked - basically, you just replace this line at https://github.com/clarketm/TableExport/blob/a18b72420e5fa179134bd059b73d45f0957092ee/src/stable/js/tableexport.js#L72

with something like

settings.formatValue = settings.formatValue ? settings.formatValue.bind(this, settings.trimWhitespace) : self.formatValue.bind(this, settings.trimWhitespace);

and when you call tableExport, pass your function with parameter formatValue set to a function with a signature like this: formatValue(isTrimWhitespace, string) that does whatever formatting/cleaning you want

clarketm commented 6 years ago

@nowherenearithaca – You are correct, formatValue is not configurable via settings (although maybe it should be and I would be open to a PR if you have the bandwidth), but as a slightly less flexible workaround it can be set as a global configuration via mutating the TableExport prototype, which basically essentially changes what self.formatValue points to when it is binded.

e.g.

TableExport.prototype.formatValue = function (isTrimWhitespace, string) {
    return "custom logic here ... "string" will be the data that needs to be formatted"
}
// Run TableExport logic