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

@gtomato-web/xlsx2json

v1.2.2

Published

Covert .xlsx to .json

Downloads

102

Readme

XLSX2JSON

Cover XLSX to JSON

Table of Contents

Installation

Install the package as a dev dependency:

NPM

$ npm install -D @gtomato-web/xlsx2json

Yarn

$ yarn add -D @gtomato-web/xlsx2json

Usage

See CLI Arguments for more information.

NPM

Add script to package.json

{
  ...,
  "scripts": {
    ...,
    "xlsx2json": "xlsx2json -c <path-to-config-file>"
  }
  ...
}
$ npm run xlsx2json -c <path-to-config-file>

Yarn

$ yarn xlsx2json -c <path-to-config-file> 

CLI Arguments

| Argument | Alias | Usage | Optional | Description | Default | |-----------|:-----:|-----------------------------------|:--------:|----------------------------------|:-------:| | --config | -c | --config path/to/config/file.json | N | Path to config file | | | --help | -h | | Y | Show help | false | | --dry-run | -d | | Y | Dry run, no file will be written | false | | --quiet | -q | | Y | Quiet mode, no console output | false |

Config file

Create a config file in JSON format(suggest: xlsx2json.config.json) with the following structure:

type Dir = string;
type SheetName = string;
type ColName = string;
type RowNumber = number;

interface Config {
  src: Dir;
  dist: Dir;
  targets: Array<ColName>;
  keys: Array<ColName>;
  ignore?: {
    rows?: Array<RowNumber> | Record<SheetName, Array<RowNumber>>;
    sheets?: Array<SheetName>;
  },
  options?: {
    autoLookupParent?: boolean,
    fallbackValue?: string | false,
    stringTransformation?: 'camelCase' | 'constantCase' | 'headerCase' | 'paramCase' | 'pascalCase' | 'snakeCase' | false
    overridingDuplicate?: boolean
  }
}

| Options(LV1) | Options(LV2) | Optional | Description | Default | |--------------|----------------------|:--------:|-------------------------------------------------------------------------------------------------------------------------|:------------------------------:| | dist | | N | Path to the destination directory | | | ignore | | Y | | | | | rows | Y | - If rows in array, will ignore listed rows in all sheets | [] | | | | | - If rows in object, will ignore listed rows in corresponding sheets | | | | sheets | Y | Sheets to ignore by their sheet name | [] | | keys | | N | List of column names of fields to create the path to the value in dot notation | | | options | | Y | | | | | autoLookupParent | Y | Set to true to enable parent lookup, read How auto parent lookup works to learn more | true | | | fallbackValue | Y | - If fallbackValue is a string, row(s) with no value will fall back to the provided value | "Missing value in source file" | | | | | - If fallbackValue is false, row(s) with no value will be skipped | | | | stringTransformation | Y | Set to "camelCase" to have camel case object key(s)e.g. exampleOfCamelCase | "camelCase" | | | | | Set to "constantCase" to have constant case object key(s)e.g. EXAMPLE_OF_CONSTANT_CASE | | | | | | Set to "headerCase" to have header case object key(s)e.g. Example-Of-Header-Case | | | | | | Set to "paramCase" to have param case object key(s)e.g. example-of-param-case | | | | | | Set to "pascalCase" to have pascal case object key(s)e.g. ExampleOfPascalCase | | | | | | Set to "snakeCase" to have snake case object key(s)e.g. example_of_snake_case | | | | | | Set to false to disable string transformation | | | | overridingDuplicate | Y | Set to true to replace the value of a duplicate key with the value that follows it | false | | src | | N | Path to the source .xlsx file | | | targets | | N | List of column names of target value field, each item will output a file | |

Example

How auto parent lookup works

Say this is the source file:

| # | A | B | C | D | E | |---|---------|-------------|-------------------|-------------------|-------------------| | 1 | section | key | en_us | zh_cn | zh_hk | | 2 | home | title | title in en | title in cn | title in hk | | 3 | | description | description in en | description in cn | description in hk | | 4 | contact | | contact in en | contact in cn | contact in hk | | 5 | setting | title | title in en | title in cn | title in hk | | 6 | | description | description in en | description in cn | description in hk |

Config will be like...

// xlsx2json.config.json
{
  "src": "path/to/source/file.xlsx",
  "dist": "path/to/destination/directory",
  "targets": [
    "en_us",
    "zh_cn",
    "zh_hk"
  ],
  "keys": [
    "section",
    "key"
  ],
  "options": {
    "autoLookupParent": true,
    "fallbackValue": "",
    "overridingDuplicate": false
  }
}

And output will be like... (we only show en_us output in example, actual output will be 3 files)

// en_us.json
{
  "home": {
    "title": "title in en",
    "description": "description in en"
  },
  "contact": "contact in en",
  "setting": {
    "title": "title in en",
    "description": "description in en"
  }
}

How fallback value works

Say this is the source file:

| # | A | B | C | D | E | |---|---------|-------------|-------------------|-------------------|-------------------| | 1 | section | key | en_us | zh_cn | zh_hk | | 2 | home | title | title in en | title in cn | title in hk | | 3 | | description | | | | | 4 | contact | | contact in en | contact in cn | contact in hk | | 5 | setting | title | | | | | 6 | | description | description in en | description in cn | description in hk |

Config will be like...

// xlsx2json.config.json
{
  "src": "path/to/source/file.xlsx",
  "dist": "path/to/destination/directory",
  "targets": [
    "en_us",
    "zh_cn",
    "zh_hk"
  ],
  "keys": [
    "section",
    "key"
  ],
  "options": {
    "autoLookupParent": true,
    "fallbackValue": "Let's fallback",
    "overridingDuplicate": false
  }
}

And output will be like... (we only show en_us output in example, actual output will be 3 files)

// en_us.json
{
  "home": {
    "title": "title in en",
    "description": "Let's fallback"
  },
  "contact": "contact in en",
  "setting": {
    "title": "Let's fallback",
    "description": "description in en"
  }
}

How overriding duplicate works

Say this is the source file:

| # | A | B | C | D | E | |---|---------|-------------|----------------------------|----------------------------|----------------------------| | 1 | section | key | en_us | zh_cn | zh_hk | | 2 | home | title | original title in en | original title in cn | original title in hk | | 3 | | title | let's crash in en | let's crash in cn | let's crash in hk | | 4 | | description | description in en | description in cn | description in hk | | 5 | contact | | contact in en | contact in cn | contact in hk | | 6 | setting | title | title in en | title in cn | title in hk | | 7 | | description | original description in en | original description in cn | original description in hk | | 8 | | description | let's crash in en | let's crash in cn | let's crash in hk |

Config will be like...

// xlsx2json.config.json
{
  "src": "path/to/source/file.xlsx",
  "dist": "path/to/destination/directory",
  "targets": [
    "en_us",
    "zh_cn",
    "zh_hk"
  ],
  "keys": [
    "section",
    "key"
  ],
  "options": {
    "autoLookupParent": true,
    "fallbackValue": "",
    "overridingDuplicate": true
  }
}

And output will be like... (we only show en_us output in example, actual output will be 3 files)

// en_us.json
{
  "home": {
    "title": "let's crash in en",
    "description": "description in en"
  },
  "contact": "contact in en",
  "setting": {
    "title": "title in en",
    "description": "let's crash in en"
  }
}