Monday, 1 May 2017

Dynamicly create columns using node xlsx

I am try to create an excel file from the data provided using npm xlsx. https://www.npmjs.com/package/xlsx

const players = [
    {
        "number": "34",
        "firstName": "Nate",
        "lastName": "Cole",
        "position": "Pitcher"
    },
    {
        "number": "5",
        "firstName": "Kyle",
        "lastName": "Carroll",
        "position": "Pitcher"
    }
];

Here is were I pick the Column Headers to use.

let worksheetColumns = [];

_.forEach(players, function (object) {
    _.forEach(object, function (value, key) {
        worksheetColumns.push(key);
    });
});

const uniqueColumns = _.uniq(worksheetColumns);

I can hard code columns and rows but I am looking to have the key be the Column headers and the values be the rows of data.

const workbook = {
    SheetNames: ["Test Sheet"],
    Sheets: {
        "Test Sheet": {
            "!ref":"A1:Z5",
            A1: { t:'s', v: "column" },
            A2: { t:'s', v: "row" }
        }
    }
};

const wopts = { bookType:'xlsx', bookSST:false, type:'binary' };

XLSX.writeFile(workbook, 'output.xlsx', wopts);



via John

No comments:

Post a Comment