npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easyexcel4nodeexport

v1.0.1

Published

Easy Excel Export exports excel without complex code to calculate cell addess and style

Downloads

9

Readme

License

easyexcel4nodeexport

An Extension for excel4node that enable us to export excel without worring about complex logic to set content and styles on excel cell with help of excel cell addresses. You just need to supply simple array object, file related detials like (name,worksheet name,file title) and This extension takes care for the rest of export.

Basic Usage

router.get('/export/example1',async function(req, res, next){
  let conf = { worksheetName: 'Excel Example 1', fileTitle: 'Excel Example 1' }
  const utilExcel = new excelHelper.excelHelpers({ conf: conf })
  let result = [{date:'20/05/2010', firstName: 'John', lastName: 'Canidy', year: 2010}]
  return await utilExcel.exportExcel({ data: result, res, callfunc: new ExcelService(req).export_Example_1 });
})
class ExcelService {

  constructor(req) {
    this._req = req;
  }
  
  // Basic Export
  async export_Example_1({ data, ws, wb }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let defaultStyle = helpers.defaultStyle();
    let grid = helpers.grid, excelData = [], rowIndex = 1;
    let Subheadings = ['Date', 'First Name', 'Last Name', 'Year'];
// By declaring style at row level, Applies to  entire row
    rowIndex = helpers.createRow({ grid, elements: [{ data: "Example 1", no_h_merge: Subheadings.length }], x: rowIndex, y: 1, style: helpers.getHeading()});
    rowIndex = helpers.createRow({ grid, elements: Subheadings, x: rowIndex, y: 1, style: helpers.getSubHeading() });

    for (let index = 0; index < data.length; index++) {
      const d = data[index];
      excelData = [d.date,d.firstName,d.lastName, d.year];

      rowIndex = helpers.createRow({ grid, elements: excelData, x: rowIndex, y: 1, style: defaultStyle })
    }

    helpers.fillGrid(ws, grid);
    return { ws, wb };
  }
}

Example 2

If any one want some cells to be vertically merged

router.get('/export/example2',async function(req, res, next){
  let conf = { worksheetName: 'Excel Example 2', fileTitle: 'Excel Example 2' }
  const utilExcel = new excelHelper.excelHelpers({ conf: conf })
  let result = [
    {date:'20/05/2010', firstName: 'John', lastName: 'Canidy', year: 2010, address:["address 1", "address 2"]},
    {date:'20/03/2010', firstName: 'Mary', lastName: 'Can', year: 2010, address:["address 3", "address 4"]}
]
  return await utilExcel.exportExcel({ data: result, res, callfunc: new ExcelService(req).export_Example_2 });  
})
class ExcelService {
    async export_Example_2({ data, ws, wb, returnGrid = false }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let defaultStyle = helpers.defaultStyle();
    let grid = helpers.grid, excelData = [], rowIndex = 1;
    let Subheadings = ['Date', 'First Name', 'Last Name', 'Year','Address'];

    rowIndex = helpers.createRow({ grid, elements: [{ data: "Example 2", no_h_merge: Subheadings.length }], x: rowIndex, y: 1, style: helpers.getHeading()});
    rowIndex = helpers.createRow({ grid, elements: Subheadings, x: rowIndex, y: 1, style: helpers.getSubHeading() });

    for (let index = 0; index < data.length; index++) {
      const d = data[index];
      excelData = [d.date,d.firstName,d.lastName, d.year];

      let cells = []
      d.address.forEach(element => {
        cells.push({ data: element })        
      });
      excelData.push({ cells: cells })
      rowIndex = helpers.createRow({ grid, elements: excelData, x: rowIndex, y: 1, style: defaultStyle })
    }
    
    if (!returnGrid) {
      helpers.fillGrid(ws, grid);
      return { ws, wb };
    }
    else {
      return grid;
    }
  }
}

Example 3

If any one want some cells to be vertically merge and Horizontally merge.

router.get('/export/example3',async function(req, res, next){
  let conf = { worksheetName: 'Excel Example 3', fileTitle: 'Excel Example 3' }
  const utilExcel = new excelHelper.excelHelpers({ conf: conf })
  let result = [
    {date:'20/05/2010', firstName: 'John', lastName: 'Canidy', year: 2010, address:["address 1", "address 2"]},
    {date:'20/03/2010', firstName: 'Mary', lastName: 'Can', year: 2010, address:["address 3", "address 4"]}
]
  return await utilExcel.exportExcel({ data: result, res, callfunc: new ExcelService(req).export_Example_3 });  
})
class ExcelService {
  // Merging cell with styling
  async export_Example_3({ data, ws, wb }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let defaultStyle = helpers.defaultStyle();
    let grid = helpers.grid, excelData = [], rowIndex = 1;
    let Subheadings = [{ data: 'Date',no_h_merge: 2, style: helpers.getSucessStyle() }, 'First Name', 'Last Name', 'Year','Address'];

    rowIndex = helpers.createRow({ grid, elements: [{ data: "Example 2", no_h_merge: Subheadings.length + 1 }], x: rowIndex, y: 1, style: helpers.getHeading()});
    rowIndex = helpers.createRow({ grid, elements: Subheadings, x: rowIndex, y: 1, style: helpers.getSubHeading() });

    for (let index = 0; index < data.length; index++) {
      const d = data[index];
      excelData = [{ data: d.date, no_h_merge: 2, style: helpers.getDangerStyle() }, d.firstName, d.lastName, d.year];

      let cells = []
      d.address.forEach(element => {
        cells.push({ data: element })        
      });
      excelData.push({ cells: cells })
      rowIndex = helpers.createRow({ grid, elements: excelData, x: rowIndex, y: 1, style: helpers.getSucessStyle() })
    }
    helpers.fillGrid(ws, grid);
    return { ws, wb };    
  }
}

Example 4

If any one want Two cells to be vertically merge.

router.get('/export/example4',async function(req, res, next){
  let conf = { worksheetName: 'Excel Example 4', fileTitle: 'Excel Example 4' }
  const utilExcel = new excelHelper.excelHelpers({ conf: conf })
  let result = { merge1: [], merge2: [] };
  result.merge1 = [{date:'20/05/2010', firstName: 'John', lastName: 'Canidy', year: 2010}]
  result.merge2 = [
    {date:'20/05/2010', firstName: 'John', lastName: 'Canidy', year: 2010, address:["address 1", "address 2"]},
    {date:'20/03/2010', firstName: 'Mary', lastName: 'Can', year: 2010, address:["address 3", "address 4"]}
]

  return await utilExcel.exportExcel({ data: result, res, callfunc: new ExcelService(req).export_Example_4 });  
})

class ExcelService {
  // Merging grid 1
  async merging_grid_1({ data, ws, wb, returnGrid = false, rowIndex = 1 }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let defaultStyle = helpers.defaultStyle();
    let grid = helpers.grid, excelData = [];
    let Subheadings = ['Date', 'First Name', 'Last Name', 'Year'];

    rowIndex = helpers.createRow({ grid, elements: [{ data: "Example 1", no_h_merge: Subheadings.length }], x: rowIndex, y: 1, style: helpers.getHeading()});
    rowIndex = helpers.createRow({ grid, elements: Subheadings, x: rowIndex, y: 1, style: helpers.getSubHeading() });

    for (let index = 0; index < data.length; index++) {
      const d = data[index];
      excelData = [d.date,d.firstName,d.lastName, d.year];

      rowIndex = helpers.createRow({ grid, elements: excelData, x: rowIndex, y: 1, style: defaultStyle })
    }

    if (!returnGrid) {
      helpers.fillGrid(ws, grid);
      return { ws, wb };
    }
    else {
      return grid;
    }
  }

  // Merging grid 2
  async merging_grid_2({ data, ws, wb, returnGrid = false, rowIndex = 1 }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let defaultStyle = helpers.defaultStyle();
    let grid = helpers.grid, excelData = [];
    let Subheadings = ['Date', 'First Name', 'Last Name', 'Year','Address'];

    rowIndex = helpers.createRow({ grid, elements: [{ data: "Example 2", no_h_merge: Subheadings.length }], x: rowIndex, y: 1, style: helpers.getHeading()});
    rowIndex = helpers.createRow({ grid, elements: Subheadings, x: rowIndex, y: 1, style: helpers.getSubHeading() });

    for (let index = 0; index < data.length; index++) {
      const d = data[index];
      excelData = [d.date,d.firstName,d.lastName, d.year];

      let cells = []
      d.address.forEach(element => {
        cells.push({ data: element })        
      });
      excelData.push({ cells: cells })
      rowIndex = helpers.createRow({ grid, elements: excelData, x: rowIndex, y: 1, style: defaultStyle })
    }
    
    if (!returnGrid) {
      helpers.fillGrid(ws, grid);
      return { ws, wb };
    }
    else {
      return grid;
    }
  }

  // Exporting two tables vertically
  async export_Example_4({ data, ws, wb }) {
    const helpers = new excelHelper.excelHelpers({ wb })
    let excelgrid = Object({}, helpers.grid);

    let merge1grid = await new ExcelService().merging_grid_1({ data: data.merge1, ws: ws, wb: wb, returnGrid: true })
    let merge2grid = await new ExcelService().merging_grid_2({ data: data.merge2, ws: ws, wb: wb, returnGrid: true, rowIndex: merge1grid.rows.length + 2 });
    
    excelgrid.rows = [
      ...merge1grid.rows,
      ...merge2grid.rows
    ]

    helpers.fillGrid(ws, excelgrid);
    return { ws, wb };
  }
}

If any one want to use custom style other than this than use excel4node style object in place if style function.