1.2.2 • Published 7 months ago

@gtomato-web/xlsx2json v1.2.2

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
7 months ago

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

ArgumentAliasUsageOptionalDescriptionDefault
--config-c--config path/to/config/file.jsonNPath to config file
--help-hYShow helpfalse
--dry-run-dYDry run, no file will be writtenfalse
--quiet-qYQuiet mode, no console outputfalse

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)OptionalDescriptionDefault
distNPath to the destination directory
ignoreY
rowsY- If rows in array, will ignore listed rows in all sheets[]
- If rows in object, will ignore listed rows in corresponding sheets
sheetsYSheets to ignore by their sheet name[]
keysNList of column names of fields to create the path to the value in dot notation
optionsY
autoLookupParentYSet to true to enable parent lookup, read How auto parent lookup works to learn moretrue
fallbackValueY- 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
stringTransformationYSet 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
overridingDuplicateYSet to true to replace the value of a duplicate key with the value that follows itfalse
srcNPath to the source .xlsx file
targetsNList 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:

#ABCDE
1sectionkeyen_uszh_cnzh_hk
2hometitletitle in entitle in cntitle in hk
3descriptiondescription in endescription in cndescription in hk
4contactcontact in encontact in cncontact in hk
5settingtitletitle in entitle in cntitle in hk
6descriptiondescription in endescription in cndescription 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:

#ABCDE
1sectionkeyen_uszh_cnzh_hk
2hometitletitle in entitle in cntitle in hk
3description
4contactcontact in encontact in cncontact in hk
5settingtitle
6descriptiondescription in endescription in cndescription 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:

#ABCDE
1sectionkeyen_uszh_cnzh_hk
2hometitleoriginal title in enoriginal title in cnoriginal title in hk
3titlelet's crash in enlet's crash in cnlet's crash in hk
4descriptiondescription in endescription in cndescription in hk
5contactcontact in encontact in cncontact in hk
6settingtitletitle in entitle in cntitle in hk
7descriptionoriginal description in enoriginal description in cnoriginal description in hk
8descriptionlet's crash in enlet's crash in cnlet'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"
  }
}
1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

8 months ago