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

xlsx-to-json-plus

v1.0.1

Published

让excel表达复杂的json格式,将xlsx文件转成json。

Downloads

8

Readme

Build Status

English Document

简介

让excel表达复杂的json格式,将xlsx文件转成json。

install

$npm install xlsx-to-json-plus

使用

目前只支持.xlsx格式,不支持.xls格式。 example目录可找到完整例子。

var magic = require('xlsx-to-json-plus');

["./example/excel/heroes.xlsx"].forEach(function (element) {

  magic.toJson(
    path.join(__dirname, element),  //excell file
    path.join(__dirname, "./json"), //json dir
    2,  //excell head line number
    "," //array separator
  );

});

示例1 test.xlsx

test.xlsx

输出如下:

[{
    "id": 123,
    "desc": "description",
    "flag": true,
    "otherid": [1, 2],
    "words": ["哈哈", "呵呵"],
    "map": [true, true],
    "data": {
        "a": 123,
        "b": 45
    },
    "hero": [
      {"id": 2,"level": 30},
      {"id": 3,"level": 80}
    ]
}, {
    "id": 456,
    "desc": "描述",
    "flag": false,
    "otherid": [3, 5, 8],
    "words": ["shit", "my god"],
    "map": [false, true],
    "data": {
        "a": 11,
        "b": 22
    },
    "hero": [
      {"id": 9, "level": 38 },
      {"id": 17,"level": 100}
    ]
}]

支持以下数据类型

  • number 数字类型
  • boolean 布尔
  • string 字符串
  • date 日期类型
  • object 对象,复杂的嵌套可以通过外键来实现,见“外键类型的sheet关联”
  • number-array 数字数组
  • boolean-array 布尔数组
  • string-array 字符串数组
  • object-array 对象数组,复杂的嵌套可以通过外键来实现,见“外键类型的sheet关联”

excel表头规则

  • 基本数据类型(string,number,bool)时候,一般不需要设置会自动判断,但是也可以明确声明数据类型。
  • 字符串类型:此列表头的命名形式 列名#string
  • 数字类型:此列表头的命名形式 列名#number
  • 日期类型:列名#date 。格式YYYY/M/D H:m:s or YYYY/M/D or YYYY-M-D H:m:s or YYYY-M-D。(==注意:目前xlsx文件里面列必须设置为文本类型,如果是日期类型的话,会导致底层插件解析出来错误格式的时间==).
  • 布尔类型:此列表头的命名形式 列名#bool
  • 基本类型数组:此列表头的命名形式 列名#[]
  • 对象:此列表头的命名形式 列名#{}
  • 对象数组:此列表头的命名形式列名#[{}]
  • id:此列表头的命名形式列名#id,用来生成对象格式的输出,以该列字段作为key,一个sheet中不能存在多个id类型的列,否则会被覆盖,相关用例请查看test/heroes.xlsx
  • id[]:此列表头的命名形式列名#id[],用来约束输出的值为对象数组,相关用例请查看test/stages.xlsx

数据规则

  • 关键符号都是半角符号。
  • 数组使用逗号,分割。
  • 对象属性使用分号;分割。
  • 列格式如果是日期,导出来的是格林尼治时间不是当时时区的时间,列设置成字符串可解决此问题。

外键类型的sheet关联

  • sheet名称必须为【列名@sheet名称】,例如存在一个名称为a的sheet,会导出一个a.json,可以使用一个名称为b@a的sheet为这个json添加一个b的属性
  • 外键类型的sheet(sub sheet)顺序上必须位于被关联的sheet(master sheet)之后
  • master sheet的输出类型如果为对象,则sub sheet必须也存在master sheet同列名并且类型为id的列作为关联关系;master sheet的输出类型如果为数组,则sub sheet按照数组下标(行数)顺序关联
  • 相关用例请查看test/heroes.xlsx

原理说明

  • 依赖 node-xlsx 这个项目解析xlsx文件。
  • xlsx就是个zip文件,解压出来都是xml。有一个xml存的string,有相应个xml存的sheet。通过解析xml解析出excel数据(json格式),这个就是node-xlsx 做的工作。
  • 本项目只需利用 node-xlsx 解析xlsx文件,然后拼装自己的json数据格式。

补充