Skip to content

Instantly share code, notes, and snippets.

@hughsw
Created June 23, 2019 01:15
Show Gist options
  • Select an option

  • Save hughsw/abcd4b5554c20a5d8fd40725a1e60aa4 to your computer and use it in GitHub Desktop.

Select an option

Save hughsw/abcd4b5554c20a5d8fd40725a1e60aa4 to your computer and use it in GitHub Desktop.

Revisions

  1. hughsw created this gist Jun 23, 2019.
    36 changes: 36 additions & 0 deletions excelgist.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // See: https://gist.github.com/nealrs/4bdcb6316665338d9a1f7c2b690d6a19

    const Excel = require('exceljs');

    // create workbook & add worksheet
    const workbook = new Excel.Workbook();
    const worksheet = workbook.addWorksheet('Discography');

    // add column headers
    worksheet.columns = [
    { header: 'Album', key: 'album'},
    { header: 'Year', key: 'year'}
    ];

    // add row using keys
    worksheet.addRow({album: "Taylor Swift", year: 2006});

    // add rows the dumb way
    worksheet.addRow(["Fearless", 2008]);

    // add an array of rows
    const rows = [
    ["Speak Now", 2010],
    {album: "Red", year: 2012}
    ];
    worksheet.addRows(rows);

    // edit cells directly
    worksheet.getCell('A6').value = "1989";
    worksheet.getCell('B6').value = 2014;

    // save workbook to disk
    const spreadsheetName = 'taylor_swift.xlsx';
    workbook.xlsx.writeFile(spreadsheetName).then(() => console.log(`saved spreadsheet: ${spreadsheetName}`));