Initial commit, 90% there

This commit is contained in:
mdares
2025-12-02 16:27:21 +00:00
commit 755028af7e
7353 changed files with 1759505 additions and 0 deletions

6
node_modules/convert-excel-to-json/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,6 @@
language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"

21
node_modules/convert-excel-to-json/LICENSE.md generated vendored Executable file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 INFOinvest http://infoinvest.com.br
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

377
node_modules/convert-excel-to-json/README.md generated vendored Executable file
View File

@@ -0,0 +1,377 @@
# convert-excel-to-json
[![Build Status](https://api.travis-ci.org/DiegoZoracKy/convert-excel-to-json.svg)](https://travis-ci.org/DiegoZoracKy/convert-excel-to-json)
Convert Excel to JSON, mapping sheet columns to object keys.
Key features:
- Define a specific Range (e.g. `'A1:E6'`)
- Specify a column to key mapping (e.g. `{porperty1: 'CELLVALUE A1', property2: 'CELLVALUE B1'}`)
- Get just specific sheets (e.g. `{sheets: ['sheet1', 'sheet2']}`)
## Install
### NPM / Node
```javascript
npm install convert-excel-to-json
```
or to use it via command-line
```javascript
npm install -g convert-excel-to-json
```
## Usage / Examples
For all the examples, lets suppose that our excel file has two sheets, named as 'sheet1' and 'sheet2'.
### CLI
OBS: All the following examples can be used via command-line, in this case, the `--config` parameter expects a valid JSON string.
```javascript
$ convert-excel-to-json --config='{"sourceFile": "tests/test-data.xlsx"}'
```
In order to use it passing in only the **sourceFile** without extra configuration:
```javascript
$ convert-excel-to-json --sourceFile="tests/test-data.xlsx"
```
To check the help section:
```javascript
$ convert-excel-to-json --help
```
### Simple conversion
Just gets all the rows, for each sheet, where each row will be represented by an object with a structure like `{ COLUMN: 'CELLVALUE' }`, e.g. from a sheet with only one column ( the column A) and two rows `[{A: 'VALUE OF A1'}, {A: 'VALUE OF A2'}]`
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx'
});
// result will be an Object containing keys with the same name as the sheets found on the excel file. Each of the keys will have an array of objects where each of them represents a row of the container sheet. e.g. for a excel file that has two sheets ('sheet1', 'sheet2')
{
sheet1: [{
A: 'data of cell A1',
B: 'data of cell B1',
C: 'data of cell C1'
}],
sheet2: [{
A: 'data of cell A1',
B: 'data of cell B1',
C: 'data of cell C1'
}]
}
```
### Converting an xlsx that you have as a Buffer
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const fs = require('fs');
const result = excelToJson({
source: fs.readFileSync('SOME-EXCEL-FILE.xlsx') // fs.readFileSync return a Buffer
});
// result will be an Object containing keys with the same name as the sheets found on the excel file. Each of the keys will have an array of objects where each of them represents a row of the container sheet. e.g. for a excel file that has two sheets ('sheet1', 'sheet2')
{
sheet1: [{
A: 'data of cell A1',
B: 'data of cell B1',
C: 'data of cell C1'
}],
sheet2: [{
A: 'data of cell A1',
B: 'data of cell B1',
C: 'data of cell C1'
}]
}
```
### Identifying header rows
You will notice that if your sheet has some top rows setup as a header (it is very common), the first position of our result will have this data, which in this case it should not be very useful. So we can tell the module how many of the rows are headers, so we can skip them and get only the data.
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
header:{
// Is the number of rows that will be skipped and will not be present at our result object. Counting from top to bottom
rows: 1 // 2, 3, 4, etc.
}
});
// result will be an Object like the previous example, but without the rows that was defined as headers
```
### Only to specific sheets
Just gets all the rows for each sheet defined on the config object
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
header:{
rows: 1
},
sheets: ['sheet2']
});
// result will be an Object like:
{
sheet2: [{
A: 'data of cell A1',
B: 'data of cell B1',
C: 'data of cell C1'
}]
}
```
### Mapping columns to keys
#### One config to all sheets
Gets all the rows, for each sheet, but defining which columns should be returned and how they should be named on the result object.
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
columnToKey: {
A: 'id',
B: 'firstName'
}
});
// result will be an Object like:
{
sheet1: [{
id: 'data of cell A1',
firstName: 'data of cell B1'
}],
sheet2: [{
id: 'data of cell A1',
firstName: 'data of cell B1'
}]
}
```
#### Config per sheet
Gets all the rows, for each sheet, but defining which columns should be returned and how they should be named on the result object, **per sheet**.
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
sheets:[{
name: 'sheet1',
columnToKey: {
A: 'id',
B: 'ProductName'
}
},{
name: 'sheet2',
columnToKey: {
A: 'id',
B: 'ProductDescription'
}
}]
});
// result will be an Object like:
{
sheet1: [{
id: 'data of cell A1',
ProductName: 'data of cell B1'
}],
sheet2: [{
id: 'data of cell A1',
ProductDescription: 'data of cell B1'
}]
}
```
**OBS:** The config *header.rows* can also be defined per sheet, like in the previous example of *columnToKey*. e.g.
```javascript
{
sourceFile: 'SOME-EXCEL-FILE.xlsx',
sheets:[{
name: 'sheet1',
header:{
rows: 1
},
columnToKey: {
A: 'id',
B: 'ProductName'
}
},{
name: 'sheet2',
header:{
rows: 3
},
columnToKey: {
A: 'id',
B: 'ProductDescription'
}
}]
}
```
### Mapping columns to keys :: Special Variables
#### Cell Variables
A value from a specific cell can be defined as a key name (e.g. `{ A: '{{A1}}' }`). e.g. if we have 3 rows allocated for a header, but the text value is specified at the first row:
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
header:{
rows: 3
}
columnToKey: {
'A': '{{A1}}',
'B': '{{B1}}'
}
});
// result will be an Object like:
{
sheet1: [{
THE-VALUE-OF-THE-CELL-A1: 'data of cell A1',
THE-VALUE-OF-THE-CELL-B1: 'data of cell B1'
}],
sheet2: [{
THE-VALUE-OF-THE-CELL-A1: 'data of cell A1',
THE-VALUE-OF-THE-CELL-B1: 'data of cell B1'
}]
}
```
**OBS:** {{columnHeader}} will follow the config *header.rows* or, in case it is not specified, it will always treat the first row as a header.
#### Automatic key/property naming following the column header {{columnHeader}}
To return all the data but having the object keys named as a row header found at the excel, instead of the column letters, is just use two special configs. Check the following *columnToKey*:
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
columnToKey: {
'*': '{{columnHeader}}'
}
});
// result will be an Object like:
{
sheet1: [{
THE-VALUE-OF-THE-HEADER-CELL-A1: 'data of cell A1',
THE-VALUE-OF-THE-HEADER-CELL-B1: 'data of cell B1',
THE-VALUE-OF-THE-HEADER-CELL-C1: 'data of cell C1'
}],
sheet2: [{
THE-VALUE-OF-THE-HEADER-CELL-A1: 'data of cell A1',
THE-VALUE-OF-THE-HEADER-CELL-B1: 'data of cell B1',
THE-VALUE-OF-THE-HEADER-CELL-C1: 'data of cell C1'
}]
}
```
**OBS:** {{columnHeader}} will follow the config *header.rows* or, in case it is not specified, it will always treat the first row as a header.
### Range
A specific range can be defined. Also like the previous configs, for all the sheets or per sheet.
#### One Range for all sheets
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
range: 'A2:B3',
sheets: ['sheet1', 'sheet2']
});
// result will be an Object like:
{
sheet1: [{
A: 'data of cell A2',
B: 'data of cell B2'
},{
A: 'data of cell A3',
B: 'data of cell B3'
}],
sheet2: [{
A: 'data of cell A2',
B: 'data of cell B2'
},{
A: 'data of cell A3',
B: 'data of cell B3'
}]
}
```
#### A Range per sheet
```javascript
'use strict';
const excelToJson = require('convert-excel-to-json');
const result = excelToJson({
sourceFile: 'SOME-EXCEL-FILE.xlsx',
sheets: [{
name: 'sheet1',
range: 'A2:B2'
},{
name: 'sheet2',
range: 'A3:B4'
}]
});
// result will be an Object like this:
{
sheet1: [{
A: 'data of cell A2',
B: 'data of cell B2'
],
sheet2: [{
A: 'data of cell A3',
B: 'data of cell B3'
},{
A: 'data of cell A4',
B: 'data of cell B4'
}]
}
```

22
node_modules/convert-excel-to-json/bin/cli.js generated vendored Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env node
require('magicli')({
commands: {
'convert-excel-to-json': {
options: [{
name: 'config',
description: 'A full config in a valid JSON format',
type: 'JSON'
},{
name: 'sourceFile',
description: `The sourceFile path (to be used without the 'config' parameter`,
type: 'String'
}]
}
},
pipe: {
after: JSON.stringify
}
});

View File

@@ -0,0 +1,162 @@
'use strict';
const XLSX = require('xlsx');
const extend = require('node.extend');
const excelToJson = (function() {
let _config = {};
const getCellRow = cell => Number(cell.replace(/[A-z]/gi, ''));
const getCellColumn = cell => cell.replace(/[0-9]/g, '').toUpperCase();
const getRangeBegin = cell => cell.match(/^[^:]*/)[0];
const getRangeEnd = cell => cell.match(/[^:]*$/)[0];
function getSheetCellValue(sheetCell) {
if (!sheetCell) {
return undefined;
}
if (sheetCell.t === 'z' && _config.sheetStubs) {
return null;
}
return (sheetCell.t === 'n' || sheetCell.t === 'd') ? sheetCell.v : (sheetCell.w && sheetCell.w.trim && sheetCell.w.trim()) || sheetCell.w;
};
const parseSheet = (sheetData, workbook) => {
const sheetName = (sheetData.constructor == String) ? sheetData : sheetData.name;
const sheet = workbook.Sheets[sheetName];
const columnToKey = sheetData.columnToKey || _config.columnToKey;
const range = sheetData.range || _config.range;
const headerRows = (sheetData.header && sheetData.header.rows) || (_config.header && _config.header.rows);
const headerRowToKeys = (sheetData.header && sheetData.header.rowToKeys) || (_config.header && _config.header.rowToKeys);
let strictRangeColumns;
let strictRangeRows;
if (range) {
strictRangeColumns = {
from: getCellColumn(getRangeBegin(range)),
to: getCellColumn(getRangeEnd(range))
};
strictRangeRows = {
from: getCellRow(getRangeBegin(range)),
to: getCellRow(getRangeEnd(range))
};
}
let rows = [];
for (let cell in sheet) {
// !ref is not a data to be retrieved || this cell doesn't have a value
if (cell == '!ref' || (sheet[cell].v === undefined && !(_config.sheetStubs && sheet[cell].t === 'z'))) {
continue;
}
const row = getCellRow(cell);
const column = getCellColumn(cell);
// Is a Header row
if (headerRows && row <= headerRows) {
continue;
}
// This column is not _configured to be retrieved
if (columnToKey && !(columnToKey[column] || columnToKey['*'])) {
continue;
}
// This cell is out of the _configured range
if ((strictRangeColumns && strictRangeRows) && (column < strictRangeColumns.from || column > strictRangeColumns.to || row < strictRangeRows.from || row > strictRangeRows.to)) {
continue;
}
const rowData = rows[row] = rows[row] || {};
let columnData = (columnToKey && (columnToKey[column] || columnToKey['*'])) ?
columnToKey[column] || columnToKey['*'] :
(headerRowToKeys) ?
`{{${column}${headerRowToKeys}}}` :
column;
let dataVariables = columnData.match(/{{([^}}]+)}}/g);
if (dataVariables) {
dataVariables.forEach(dataVariable => {
let dataVariableRef = dataVariable.replace(/[\{\}]*/gi, '');
let variableValue;
switch (dataVariableRef) {
case 'columnHeader':
dataVariableRef = (headerRows) ? `${column}${headerRows}` : `${column + 1}`;
default:
variableValue = getSheetCellValue(sheet[dataVariableRef]);
}
columnData = columnData.replace(dataVariable, variableValue);
});
}
if (columnData === '') {
continue;
}
rowData[columnData] = getSheetCellValue(sheet[cell]);
if (sheetData.appendData) {
extend(true, rowData, sheetData.appendData);
}
}
// removing first row i.e. 0th rows because first cell itself starts from A1
rows.shift();
// Cleaning empty if required
if (!_config.includeEmptyLines) {
rows = rows.filter(v => v !== null && v !== undefined);
}
return rows;
};
const convertExcelToJson = function(config = {}, sourceFile) {
_config = config.constructor === String ? JSON.parse(config) : config;
_config.sourceFile = _config.sourceFile || sourceFile;
// ignoring empty lines by default
_config.includeEmptyLines = _config.includeEmptyLines || false;
// at least sourceFile or source has to be defined and have a value
if (!(_config.sourceFile || _config.source)) {
throw new Error(':: \'sourceFile\' or \'source\' required for _config :: ');
}
let workbook = {};
if (_config.source) {
workbook = XLSX.read(_config.source, {
sheetStubs: true,
cellDates: true
});
} else {
workbook = XLSX.readFile(_config.sourceFile, {
sheetStubs: true,
cellDates: true
});
}
let sheetsToGet = (_config.sheets && _config.sheets.constructor === Array) ?
_config.sheets :
Object.keys(workbook.Sheets).slice(0, (_config && _config.sheets && _config.sheets.numberOfSheetsToGet) || undefined);
let parsedData = {};
sheetsToGet.forEach(sheet => {
sheet = (sheet.constructor == String) ? {
name: sheet
} : sheet;
parsedData[sheet.name] = parseSheet(sheet, workbook);
});
return parsedData;
};
return convertExcelToJson;
}());
module.exports = excelToJson;

35
node_modules/convert-excel-to-json/package.json generated vendored Normal file
View File

@@ -0,0 +1,35 @@
{
"name": "convert-excel-to-json",
"version": "1.7.0",
"description": "Convert Excel to JSON",
"homepage": "https://github.com/DiegoZoracKy/convert-excel-to-json/",
"main": "./lib/convert-excel-to-json.js",
"bin": "./bin/cli.js",
"author": {
"name": "Diego ZoracKy",
"email": "diego.zoracky@gmail.com",
"url": "https://github.com/DiegoZoracKy/"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/DiegoZoracKy/convert-excel-to-json.git"
},
"keywords": [
"excel to json",
"converts excel"
],
"scripts": {
"test": "mocha ./tests -b"
},
"dependencies": {
"argparse": "^1.0.2",
"magicli": "0.0.8",
"node.extend": "^2.0.2",
"xlsx": "^0.12.1"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^5.2.0"
}
}

776
node_modules/convert-excel-to-json/tests/main.test.js generated vendored Normal file

File diff suppressed because one or more lines are too long

Binary file not shown.

BIN
node_modules/convert-excel-to-json/tests/test-data.xlsx generated vendored Normal file

Binary file not shown.