I am using ExcelJS library with Flexmonster to export my Pivot Table to Excel. I noticed that when I set the decimalPlaces value in format options and export the report the actual value is exported with a numberFormat applied. I wanted the values to be exported with the remaining numbers truncated and not be present in my exported excel. I tried doing this through customizeCellFunction where I forcefully truncated the values using the toFixed method but that had a weird issue where it didn't work for numbers less than 1000. Please help me fix this.
I can understand that the FormatObject is present only for changing the number format and not change the actual value but atleast modifying the values through customizeCell should be consistent for all values
Hello, Amogh!
Thank you for reaching out to us.
The described functionality for truncating the numbers can be achieved with Flexmonster's Excel export. Moreover, the decimalPlaces
parameter rounds numbers similarly to the toFixed
method, as is shown in this example: https://jsfiddle.net/flexmonster/0p5L48xv/
If this approach for number formatting does not work for you, could you please modify the JSFiddle to reproduce the issue or provide more details about your case?
Looking forward to hearing from you.
Best Regards,
Maksym
Hello Maksym,
What I meant when I said that numbers are not actually getting truncated is that the entire value is still available when exported to excel (just a number formatting is applied but if you edit the value it still has all the decimal places). I have shared a screenshot to show what I mean.
Using customizeCell like this: https://jsfiddle.net/fgza5mw9/11/ also didn't work. I am able to truncate the values after they've been added to the worksheet through ExcelJS but I don't have the context of whether a value is a currency, number or percent datatype so I'm not able to have different decimal places for them so ideally it should be possible through customizeCell.
Hello, Amogh!
Thank you for your detailed explanation.
Please note that this behavior is intended to keep the original values when editing cells, while only the format is changed. This way, the original value is still preserved after the export.
However, there is a workaround to remove the decimal part entirely. To do this, we recommend assigning the rounded number to a cell's text within the customizeCell
function:
function customizeCellFunction(cell, data) {
if (data.type == "value") {
cell.text = Math.round(data.value)
}
}
This is demonstrated in this JSFiddle: https://jsfiddle.net/flexmonster/dbf3twey/
Please let us know if it works for you.
Best Regards,
Maksym
I want to use toFixed instead of Math.round because I Math.round only returns Integer values while I want to control the number of digits after the decimal point. But like I said if I use toFixed, it doesn't work for values less than 1000. Please check the below screenshots. This seems to be a bug. I have also replicated it in jsfiddle: https://jsfiddle.net/jgmdy9a5/9/
Hello, Amogh!
Thank you for your reply.
This issue with the number not being truncated occurs because toFixed()
returns a string, and when the string is assigned to cell.text
only, the original numeric value remains unchanged. This is the default behavior, intentionally designed to keep the original values.
The numbers should be passed to a cell text with a number data type to change the formatted values: https://jsfiddle.net/flexmonster/9nutydLh/
While this behavior is not apparent, we have intentionally added this to support overriding exported values.
Please let us know if this solution works for you.
Best Regards,
Maksym
Hello Maksym,
This solution worked. Thank you for your help!
Hello, Amogh!
Thank you for your reply.
We are glad that the suggested solution worked for you.
Please let us know if more questions arise.
Best Regards,
Maksym