Giter Site home page Giter Site logo

jobyrao / json-key-string-xlsx Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 1.0 139 KB

A library that converts Excel files into JSON format, with support for customizable JSON structures.

License: MIT License

JavaScript 100.00%
json exceltojson jsontoexcel xlsx

json-key-string-xlsx's Introduction

json-key-string-xlsx

npm version GitHub Workflow Status codecov npms.io (quality) GitHub issues license

Introduction

Convert between json and xlsx files by key string in a browser or NodeJS.

Quick Preview

Content structure of xlsx file

  1. The first column is a string of JSON key names.
  2. The other columns are the values of the JSON key string.
lang cn en
userInfo[0].name 用户名 username
userInfo[0].nickname 昵称 nickname

Excel file will be converted to

Similarly, the JSON can also be converted to xlsx file.

[
  {
    "lang": "cn",
    "userInfo": [
      {
        "name": "用户名",
        "nickname": "昵称"
      }
    ]
  },
  {
    "lang": "en",
    "userInfo": [
      {
        "name": "username",
        "nickname": "nickname"
      }
    ]
  }
]

Getting Started

Install

In the browser, just add a script tag:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/xlsx.full.min.js"></script>
<script src="dist/json-key-string-xlsx.umd.min.js"></script>
CDN Availability (click to show)
unpkg https://unpkg.com/json-key-string-xlsx/
jsDelivr https://jsdelivr.com/package/npm/json-key-string-xlsx

With npm:

$ npm i json-key-string-xlsx --save

Usage

1. Convert xlsx file to JSON

Sample files of this api: google docs or tencent docs

Commonjs

const XLSX2JSON = require('json-key-string-xlsx');
const xlsx2json = new XLSX2JSON();
const path = require('path');
const xlsxPath = path.join('./excel.xlsx');

const jsonData = xlsx2json.parse2json(xlsxPath);
// console.log(xlsx2json.parse2jsonCover);

ES Module

import xlsxJsonJs from 'json-key-string-xlsx';
const xlsx2json = new xlsxJsonJs();

UMD

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/xlsx.full.min.js"></script>
<script src="dist/json-key-string-xlsx.umd.min.js"></script>

<input type="file" name="file" id="file">
<script type="text/javascript">
  const xlsx2json = new jsonKeyStringXlsx();
  function handleFile(e) {
    const files = e.target.files, f = files[0];
    const reader = new FileReader();
    reader.onload = function(e) {
      const data = new Uint8Array(e.target.result);
      const jsonData = xlsx2json.parse2json(data, {type: 'array'});
    };
    reader.readAsArrayBuffer(f);
  }
  document.getElementById('file').addEventListener('change', handleFile, false);
</script>
console.log(jsonData) (click to show)
[
  {
    "filename": "cn",
    "lang": "cn",
    "title": "文章标题",
    "userInfo": [
      {
        "name": "用户名"
      },
      {
        "nickname": "用户昵称"
      }
    ],
    "disclaimer": {
      "title": "免责申明",
      "content": [
        "1,非人工检索方式",
        {
          "c": {
            "a": [
              {
                "b": [
                  "6,网络传播权"
                ]
              }
            ]
          }
        },
        "3,自动搜索获得",
        "4,自行承担风险",
        "5,个人隐私权"
      ]
    },
    "statusCode": {
      "200": "成功",
      "404": "失败"
    },
    "companyInfo": {
      "name": "公司名",
      "address": {
        "city": [
          "广州市",
          "北京市"
        ],
        "address": [
          "番禺万达",
          "某大厦"
        ]
      },
      "industry": "互联网"
    },
    "fortest1": {
      "key": "test1 key值,再次",
      "key2": "test1 key2值"
    },
    "fortest2": [
      {
        "key": "test2 key值,原始"
      },
      {
        "key2": "test2 key值,覆盖"
      },
      {
        "key2": "test2 key2值"
      }
    ],
    "fortest3": [
      {
        "key": "test3 对象改为数组"
      }
    ],
    "fortest4": {
      "key": "test4 数组改为对象"
    }
  },
  {
    "filename": "en",
    ......
  }
]

If some keys are overwritten, you can get details from xlsx2json.parse2jsonCover.

[ 'sheet name "main", row 12, value "disclaimer.content[1].c.a[0].b[0]"',
  'sheet name "main", row 16, value "fortest1.key"',
  'sheet name "main", row 21, value "fortest2[1].key2"',
  'sheet name "main", row 23, value "fortest3[].key"',
  'sheet name "main", row 25, value "fortest4.key"' ]

2. Convert JSON to xlsx file

The objData can also be an array of object.

const XLSX2JSON = require('json-key-string-xlsx');
const xlsx2json = new XLSX2JSON();
const objData = {
  "lang": "en",
  "userInfo": [
    {
      "name": "username",
      "nickname": "nickname"
    }
  ]
}
const aoaData = xlsx2json.json2XlsxByKey(objData);
// const aoaData = xlsx2json.json2XlsxByKey(objData, outputPath);
// const arrayBufferData = xlsx2json.json2XlsxByKey(objData, {type: 'array'});

Output file:

lang en
userInfo[0].name username
userInfo[0].nickname nickname

License

MIT

json-key-string-xlsx's People

Contributors

jobyrao avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

gwened

json-key-string-xlsx's Issues

MODULE_NOT_FOUND

I installed using npm.

I added the correct import statement to the script but when I ran it I got the MODULE_NOT_FOUND error.

I modified the "package.json" file manually for the package -

Changed -

"main": "dist/xlsx-json-js.cjs.js",
"module": "dist/xlsx-json-js.esm.js",

To -

"main": "dist/xlsx-json-js.cjs.min.js",
"module": "dist/xlsx-json-js.esm.min.js",

And it worked after that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.