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

json-as-excel

v1.0.9

Published

Convert json data to excel with nested excel header

Downloads

9

Readme

json-as-excel

Bugs Security Rating Reliability Rating Maintainability Rating Lines of Code

About

Take json object as an argument and convert them into excel data.
Json object i.e.

[
  {
    "study": {
      "science": {
        "bio": {
          "pharmacy": "Kamran Bains",
          "mbbs": {
            "general": "Chloe-Ann Vega",
            "md": "Amayah Barajas"
          }
        },
        "math": {
          "pureMath": "Safa Blackburn",
          "engineering": {
            "computer": {
              "hardware": "Kezia Gonzalez",
              "software": "Boyd Mcbride"
            },
            "civil": "Leela Romero",
            "mechanical": "Mateusz Thornton"
          }
        }
      },
      "management": {
        "bba": "Amelie Bell",
        "bbs": "Jevon Myers"
      }
    }
  },
  {
    "study": {
      "science": {
        "bio": {
          "pharmacy": "Riley-James Duran",
          "mbbs": {
            "general": "Glen Churchill",
            "md": "Sachin Deacon"
          }
        },
        "math": {
          "pureMath": "Rufus Redfern",
          "engineering": {
            "computer": {
              "hardware": "Jonah Best",
              "software": "Zion Ingram"
            },
            "civil": "Matei Gibbs",
            "mechanical": "Kaelan Mcdonnell"
          }
        }
      },
      "management": {
        "bba": "Spike Peel",
        "bbs": "Zakariyah Gray"
      }
    }
  }
];

is converted to below like excel.
alt text

No need to manually merge cells now !! 😊 🤩

Installation

npm install json-as-excel

Usage

const excel = require('json-as-excel');
const data = [
        {
          study: {
            science: {
              bio: {
                pharmacy: 'Kamran Bains',
                mbbs: {
                  general: 'Chloe-Ann Vega',
                  md: 'Amayah Barajas',
                },
              },
              math: {
                pureMath: 'Safa Blackburn',
                engineering: {
                  computer: {
                    hardware: 'Kezia Gonzalez',
                    software: 'Boyd Mcbride',
                  },
                  civil: 'Leela Romero',
                  mechanical: 'Mateusz Thornton',
                },
              },
            },
            management: {
              bba: 'Amelie Bell',
              bbs: 'Jevon Myers',
            },
          },
        },
        {
          study: {
            science: {
              bio: {
                pharmacy: 'Riley-James Duran',
                mbbs: {
                  general: 'Glen Churchill',
                  md: 'Sachin Deacon',
                },
              },
              math: {
                pureMath: 'Rufus Redfern',
                engineering: {
                  computer: {
                    hardware: 'Jonah Best',
                    software: 'Zion Ingram',
                  },
                  civil: 'Matei Gibbs',
                  mechanical: 'Kaelan Mcdonnell',
                },
              },
            },
            management: {
              bba: 'Spike Peel',
              bbs: 'Zakariyah Gray',
            },
          },
        },
      ]
const workbook = excel.generateExcel([
    {
      title: 'First sheet',
      data: data,
    },
  ]);

generateExcel function returns exceljs workbook instance. Hence, File I/O can be achieved same as in exceljs. For example:

// write to a file
await workbook.xlsx.writeFile('sample.xlsx');

For the detail reference of File I/O

Method

generateExcel([{ title, data, delimiter, options }]). Method generateExcel accepts array of objects. Each object represents individual sheet. This method returns exceljs workbook instance.

title

Title is name for sheet.

data

Data is json object whose keys are generated as header in excel and values are placed as new row per object.

delimiter (optional)

. is used as a default delimiter. If json data consists key with '.', one need to change delimiter to any other delimiter.

generateExcel([{title:"firstSheet", data:data, delimiter:"%"}])

options (optional)

options are the exceljs available worksheet options i.e. Worksheet Properties, Page Setup, Headers and Footers
More detail can be obtained from exceljs

 const options = {
   properties:{
     outlineLevelCol:2,
     tabColor:{
       argb:'FF00FF00'
     },
     defaultRowHeight:15
   },
   pageSetup:{
     fitToPage: true,
     fitToHeight: 5, 
     fitToWidth: 7
   }
 };
 generateExcel([{title:"firstSheet", data:data, delimiter:"%", options:options}])

headerFormatter (optional)

headerFormatter is a function that accepts header as an argument and return computed header.

 generateExcel([{title:"firstSheet", data:data, delimiter:"%",headerFormatter: (header) => header.toUpperCase()}])

In above example, excel that has header, all with upper case will be generated.

Acknowledgments

  1. exceljs
  2. flat

Issues

If any issue is found, please raise issue in github.

Changelog

| Version | Changes | | ----------- | ----------- | | 1.0.4 | Installation guide update in Readme | | | 1.0.5 | Example updated in githubBug FixesFixed crash when sheet data is empty objectCheck mandatory title and data for sheet configuration. If not provided, error is thrown | | | 1.0.6 | Test updated for case when data has same nested keyBug FixesCell merge issue when sheet data has same nested key | | | 1.0.7 | FeatureFeature to change header format is now added. For reference, look at headerFormatter option above. | | | 1.0.8 | Readme updated | | | 1.0.9 | Bug FixesWorksheet name already exists issue fixed. | |

MIT License

Copyright (c) 2021