Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save a cell with number having comma separator for 1000's #216

Closed
ashwin-ragha1 opened this issue Apr 27, 2015 · 5 comments
Closed

Save a cell with number having comma separator for 1000's #216

ashwin-ragha1 opened this issue Apr 27, 2015 · 5 comments

Comments

@ashwin-ragha1
Copy link

Hi,

I'm having the following files in my page ...
xlsx.core.min.js
Blob.js
FileSaver.js
Export2Excel.js

And I'm calling the function export_table_to_excel by passing html table which has a cell that contains a number with comma separator... e.g. 1,000.00

When I open the exported excel file, I find that this cell is left aligned and I want to make it right aligned.

Please help.

@SheetJSDev
Copy link
Contributor

@ashwin-ragha1 Export2Excel.js is interpreting the cell as text. The relevant line in the generateArray function is

            if(cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;

This is a super simple check, and it works for very basic data like 10000 but will not work for numbers like "1,000.00". There are simple workarounds for special cases, such as commas:

            var cellValueNoCommas = cellValue.replace(/,/g,"");
            if(cellValue !== "" && cellValueNoCommas == +cellValueNoCommas) cellValue = +cellValueNoCommas;

If you had a list of cases to consider, we could devise an appropriate technique.

The larger problem (of determining whether a string of characters is text or numeric or datetime) is much more challenging since it would have to be locale-specific (for example, in some locales, the thousands separator is "." and the decimal separator is ",")

@ashwin-ragha1
Copy link
Author

Hi,
Thanks for your quick reply...
Currently I'm just dealing with English US locale so I don't have other cases.

I modified your suggestion a bit as I'm also having other cell values with a comma which are string.

var cellValueNoCommas = cellValue.replace(/,/g, "");
var num = parseFloat(cellValueNoCommas);
if (!isNaN(num)) {
if (cellValue !== "" && cellValueNoCommas == +cellValueNoCommas) cellValue = +cellValueNoCommas;
}
else {
if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
}

Now even though the values in that particular column are right - aligned, I lose the comma.

I would like to export the values with a comma. Is there any way of doing that?

@ashwin-ragha1
Copy link
Author

With the same files as above -

xlsx.core.min.js
Blob.js
FileSaver.js
Export2Excel.js

I also notice that when we run the export code in IE9, it doesn't work. It throws an error ArrayBuffer is undefined. So is there an alternate way to get it working in IE9?

@thisissami
Copy link

Hey I have a similar issue to this, where commas are causing issues (though I'm not trying to export - just trying to read). @ashwin-ragha1 - are the files that you mentioned part of this repository or taken from elsewhere? @SheetJSDev - if I wanted to solve my issue, should I be looking into the xlsx.js file in the dist folder?

@SheetJSDev
Copy link
Contributor

To write cells with thousands separators, after parsing you can set the individual cell's z property to "#,##0.00. This will use thousands separator and will force 2 decimal places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants