Skip to content

Commit

Permalink
Feat: add excel json
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed Oct 30, 2024
1 parent a589c6c commit a877723
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
41 changes: 39 additions & 2 deletions bin/xcmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @Author: HxB
* @Date: 2022-04-25 16:27:06
* @LastEditors: DoubleAm
* @LastEditTime: 2024-09-23 18:08:19
* @LastEditTime: 2024-10-30 14:31:25
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand All @@ -31,8 +31,9 @@ const {
getAllFilePath
} = require('../utils/files');
const { cmd } = require('../utils/cmd');
const { node2es6, sortJSON, mergeObj, versionUpgrade } = require('../utils/tools');
const { node2es6, sortJSON, mergeObj, versionUpgrade, isValidJson, jsonToExcel } = require('../utils/tools');
const nodeCmd = require('node-cmd');
const readline = require('readline');

// http://patorjk.com/software/taag/
const logo = () => {
Expand Down Expand Up @@ -704,6 +705,42 @@ program
console.log(`JSON 文件合并完成,并保存为 【${mergedFilePath}】。`);
});

program
.option('json2excel <projectCode> [jsonFilePath]', 'json2excel <projectCode> [jsonFilePath]')
.command('json2excel <projectCode> [jsonFilePath]')
.description('将 JSON 数据转化为 Excel')
.action((projectCode, jsonFilePath) => {
if (!projectCode) {
console.error('请提供 Project Code');
return;
}

if (!jsonFilePath) {
// 如果未提供JSON文件路径,则提示输入JSON内容
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question('请输入 JSON 数据: \n', (jsonData) => {
rl.close();
if (isValidJson(jsonData)) {
jsonToExcel(projectCode, JSON.parse(jsonData));
} else {
console.error('输入的内容不是有效的JSON格式');
}
});
} else {
const filePath = getResolvePath(jsonFilePath);
const jsonData = getJSONFileObj(filePath);
if (jsonData) {
jsonToExcel(projectCode, jsonData);
} else {
console.error('JSON 文件中的内容不是有效的 JSON 格式');
}
}
});

program
.option('increase-memory-limit', 'increase-memory-limit')
.command('increase-memory-limit')
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.5.10",
"version": "1.5.11",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down Expand Up @@ -34,7 +34,8 @@
"download-git-repo": "^3.0.2",
"fs-extra": "^11.2.0",
"node-cmd": "^5.0.0",
"rimraf": "^5.0.5"
"rimraf": "^5.0.5",
"xlsx": "^0.18.5"
},
"time": "2717021815012024"
}
68 changes: 66 additions & 2 deletions utils/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* @Author: HxB
* @Date: 2024-05-11 17:59:32
* @LastEditors: DoubleAm
* @LastEditTime: 2024-06-26 09:42:31
* @LastEditTime: 2024-10-30 14:53:20
* @Description: 转化 commonjs 为 es6 modules
* @FilePath: \js-xcmd\utils\tools.js
*/
const XLSX = require('xlsx');

const node2es6 = (transferObj) => {
console.log('---转换 Dev 开始---');
Expand Down Expand Up @@ -109,9 +110,72 @@ const versionUpgrade = (version, maxVersionCode = 99) => {
return tempVersionArr.reverse().join('.');
};

const isValidJson = (json) => {
try {
JSON.parse(json);
return true;
} catch (error) {
return false;
}
};

const jsonToExcel = (projectCode, jsonData) => {
const workbook = XLSX.utils.book_new();
const sheetName = 'Sheet1';
const worksheetData = [];

for (const key in jsonData) {
const rowData = {
'项目编码 / Project Code': projectCode,
'模块编码 / Module Code': 'ALL',
'模块名称 / Module Name': 'ALL',
'模块英文名称 / Module Name (En)': 'ALL',
'文案编码 / Text Key': `${projectCode}.ALL.${key}`,
'国家 / Country': 'ALL',
'翻译(中文) / Translation (ZH)': jsonData[key],
'翻译(英文) / Translation (EN)': '',
'翻译(阿拉伯语) / Translation (AR)': '',
'翻译(西班牙语) / Translation (ES)': '',
'翻译(葡萄牙语) / Translation (PT)': '',
'翻译(土耳其语) / Translation (TR)': '',
'翻译(意大利语) / Translation (IT)': ''
};
worksheetData.push(rowData);
}
const worksheet = XLSX.utils.json_to_sheet(worksheetData);

// 设置表头高度
// worksheet['!rows'] = [{ hpx: 50 }];

// 设置列宽
worksheet['!cols'] = [
{ wpx: 100 }, // 项目编码 / Project Code
{ wpx: 100 }, // 模块编码 / Module Code
{ wpx: 100 }, // 模块名称 / Module Name
{ wpx: 100 }, // 模块英文名称 / Module Name (En)
{ wpx: 300 }, // 文案编码 / Text Key
{ wpx: 100 }, // 国家 / Country
{ wpx: 200 }, // 翻译(中文) / Translation (ZH)
{ wpx: 200 }, // 翻译(英文) / Translation (EN)
{ wpx: 200 }, // 翻译(阿拉伯语) / Translation (AR)
{ wpx: 200 }, // 翻译(西班牙语) / Translation (ES)
{ wpx: 200 }, // 翻译(葡萄牙语) / Translation (PT)
{ wpx: 200 }, // 翻译(土耳其语) / Translation (TR)
{ wpx: 200 } // 翻译(意大利语) / Translation (IT)
];

XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);

const excelFilePath = `output_${projectCode}_V${Date.now()}.xlsx`;
XLSX.writeFile(workbook, excelFilePath);
console.log(`Excel 文件已生成: ${excelFilePath}`);
};

module.exports = {
node2es6,
sortJSON,
mergeObj,
versionUpgrade
versionUpgrade,
isValidJson,
jsonToExcel
};

0 comments on commit a877723

Please sign in to comment.