Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Added the new feature:
Browse files Browse the repository at this point in the history
- new tag => ~
- new CLI => --dict, --key, --sheetname
  • Loading branch information
seanmars committed Feb 16, 2017
1 parent 5732920 commit 1dd8297
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 24 deletions.
18 changes: 14 additions & 4 deletions bin/e2jt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ cmder.version(pkg.version)
.option('-s, --space <space>', 'The white space to insert into the output JSON string for readability purposes')
.option('-t, --template <template>', 'The file path of JSON template')
.option('-o, --outputdir <outputdir>', 'The path of output folder')
.action(function(file, sheet, output) {
.option('--key <name of key>', 'The name of key for data list')
.option('--sheetname [true|false]', 'Auto add sheet name.[true|false]', /^(true|false)$/i, false)
.option('--dict [true|false]', 'Is key-value JSON?[true|false]', /^(true|false)$/i, false)
.action(function (file, sheet, output) {
inputFile = file;
inputSheet = sheet ? sheet : path.basename(file, path.extname(file));
outputFile = (output ? output : inputSheet) + '.json';
Expand All @@ -38,16 +41,23 @@ if (cmder.template) {
parseCallback();
}

function parseCallback(err, jsonObj) {
function parseCallback(err, templateJson) {
if (err) {
throw err;
}

var fp = path.resolve(inputFile);
var data = e2jt.parse(fp, inputSheet, jsonObj);
var data = e2jt.parse(fp, inputSheet, templateJson, {
nameOfKey: cmder.key,
isKeyVal: cmder.dict,
isAddSheetName: cmder.sheetname
});
var opdir = cmder.outputdir || path.dirname(inputFile);
var space = {
spaces: Number(cmder.space) || 0
};
outputFile = path.resolve(path.join(opdir, outputFile));
e2jt.save(outputFile, data, function(err) {
e2jt.save(outputFile, data, space, function (err) {
if (err) {
console.log(err);
return;
Expand Down
97 changes: 77 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/

var excel2jsontemplate = (function () {
const DEFAULT_ATTR_SHEETNAME = 'sheetname';
const DEFAULT_ATTR_DATAS = 'datas';

function InitException(message) {
this.message = message;
this.name = 'InitException';
Expand Down Expand Up @@ -294,6 +297,29 @@ var excel2jsontemplate = (function () {
return cells;
}

/**
* fetch name of key
*
* @method fetchNameOfKeyJson
*
* @param {Object} ws
* @param {Array} cells
*
* @return {string} name of key.
*/
function fetchNameOfKeyJson(ws, cells) {
for (var cell of cells) {
var valAddr = {
c: cell.c + 1,
r: cell.r
};

var zVal = XLSX.utils.encode_cell(valAddr);

return ws[zVal].v;
}
}

/**
* fetch raw data(s)
*
Expand Down Expand Up @@ -361,11 +387,12 @@ var excel2jsontemplate = (function () {
* @param {string} sheetName
* @param {string} tagTitle
* @param {string} tagAttribute
* @param {string} tagNameOfKey
* @param {string} tagIgnore
*
* @return {JSON} result The data of JSON.
*/
function parseSheet(filePath, sheetName, tagTitle, tagAttribute, tagIgnore) {
function parseSheet(filePath, sheetName, tagTitle, tagAttribute, tagNameOfKey, tagIgnore) {
var ws = loadSheet(filePath, sheetName);
if (!ws) {
return;
Expand All @@ -387,6 +414,8 @@ var excel2jsontemplate = (function () {
var attrCells = findTagCells(ws, range, titleCell, tagAttribute);
// find all tag of ignore cells
var ignoreCells = findTagCells(ws, range, titleCell, tagIgnore);
// find all tag of name of key cells
var nameOfKeyCells = findTagCells(ws, range, titleCell, tagNameOfKey);

// find all titles with titleCell
var titles = findTitleCells(ws, range, titleCell, ignoreCells);
Expand All @@ -396,11 +425,14 @@ var excel2jsontemplate = (function () {

// fetch all attribute cell(s)
var attrs = fetchAttrJson(ws, attrCells);
// fetch name of key
var nameOfKey = fetchNameOfKeyJson(ws, nameOfKeyCells);

// fetch all raw datas
var rawDatas = fetchRawData(ws, range, titleCell, titles, ignoreCells);

return {
nameOfKey: nameOfKey,
attrs: attrs,
rawDatas: rawDatas
};
Expand All @@ -413,10 +445,11 @@ var excel2jsontemplate = (function () {
*
* @param {Object} data
* @param {JSON} template
* @param {Boolean} isKeyVal
*
* @return {JSON} result Mapped data.
*/
function map(data, template) {
function map(data, template, isKeyVal) {
var type = template.constructor;

var obj;
Expand Down Expand Up @@ -482,6 +515,11 @@ var excel2jsontemplate = (function () {
}
break;
}

if (isKeyVal) {
obj._key = keyVal;
obj._val = obj[keyVal];
}
}

return obj;
Expand All @@ -494,12 +532,13 @@ var excel2jsontemplate = (function () {
*
* @param {Array} rawData
* @param {JSON} template
* @param {Boolean} isKeyVal
*
* @return {Array} result Array of JSON objects with template.
*/
function transform(rawData, template) {
function transform(rawData, template, isKeyVal) {
// init result object
var result = [];
var result = isKeyVal ? {} : [];

// foreach data to mapping to template
for (var index in rawData) {
Expand All @@ -508,8 +547,12 @@ var excel2jsontemplate = (function () {
}

// mapping
var obj = map(rawData[index], template);
result.push(obj);
var obj = map(rawData[index], template, isKeyVal);
if (isKeyVal) {
result[obj._key] = obj._val;
} else {
result.push(obj);
}
}

return result;
Expand All @@ -523,16 +566,26 @@ var excel2jsontemplate = (function () {
* @param {string} filePath
* @param {string} sheetName
* @param {JSON} template
* @param {string} tagTitle [default = '#']
* @param {string} tagAttribute [default = '^']
* @param {string} tagIgnore [default = '!']
* @param {Object} options
* @param {Boolean} options.useSheetname = false
* @param {Boolean} options.isKeyVal = false
* @param {string} options.tagTitle = '#'
* @param {string} options.tagAttribute = '^'
* @param {string} options.tagNameOfKey = '~'
* @param {string} options.tagIgnore = '!'
*
* @return {Object} The JSON Object of data
*/
function parse(filePath, sheetName, template, tagTitle, tagAttribute, tagIgnore) {
tagTitle = tagTitle || '#';
tagAttribute = tagAttribute || '^';
tagIgnore = tagIgnore || '!';
function parse(filePath, sheetName, template, options) {
options = options || {};

nameOfKey = options.nameOfKey;
isAddSheetName = options.isAddSheetName || false;
isKeyVal = options.isKeyVal || false;
tagTitle = options.tagTitle || '#';
tagAttribute = options.tagAttribute || '^';
tagNameOfKey = options.tagNameOfKey || '~';
tagIgnore = options.tagIgnore || '!';

// check sheet name is empty or not
if (!sheetName) {
Expand All @@ -546,22 +599,26 @@ var excel2jsontemplate = (function () {
}

// get data
var objJson = parseSheet(filePath, sheetName, tagTitle, tagAttribute, tagIgnore);
var objJson = parseSheet(filePath, sheetName,
tagTitle, tagAttribute, tagNameOfKey, tagIgnore);
objJson = objJson || {
attrs: {},
rawDatas: []
};

// copy top-level attribute
var result = objJson.attrs;
// transform the name attribute
if (!_u.isEmpty(objJson.attrs) && result.hasOwnProperty('name')) {
result.name = result.name;
} else {
result['name'] = sheetName;
if (isAddSheetName) {
result[DEFAULT_ATTR_SHEETNAME] = sheetName;
}
// transform the data if necessary
result.datas = _u.isEmpty(template) ? objJson.rawDatas : transform(objJson.rawDatas, template);
datas = _u.isEmpty(template) ? objJson.rawDatas : transform(objJson.rawDatas, template, isKeyVal);
if (isKeyVal) {
result = datas;
} else {
nameOfKey = nameOfKey ? nameOfKey : (objJson.nameOfKey || DEFAULT_ATTR_DATAS);
result[nameOfKey] = datas;
}

return result;
}
Expand Down

0 comments on commit 1dd8297

Please sign in to comment.