1

I am able to give color, width for columns also & now I want to format a number that which we are sending to excel(i.e, I am sending 0 it should appear as 0.00 in excel): How is it possible?

gone through Define cell format on AlaSQL/JS-XLSX Excel Export,

Community
  • 1
  • 1
BABAJAN
  • 11
  • 3

1 Answers1

0

I know it would be too late to answer this but would help for people who are looking for formatting number with fixed decimal as 2. You can define your custom function by adding to alasql.fn object as below

alasql.fn.precise_round = function (num, dec) {
        if ((typeof num !== 'number') || (typeof dec !== 'number'))
            return false;
        console.log(num);
        var num_sign = num >= 0 ? 1 : -1;

        return (Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec);
    }

    alasql.fn.numberWithCommas = function (num) {
        return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }

You can now use this function inside your alasql select query

var data = alasql("SELECT INTO XLSX precise_round(column_name) from ?", [objData]);
alasql("SELECT INTO XLSX('Test.xlsx', { headers: true}) FROM ?", [data]);

Assuming one of your column in objData holds numbers including 0 as well which is output as 0.00 as per your requirement

Anil Sarkar
  • 150
  • 8