mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
309 lines
13 KiB
JavaScript
309 lines
13 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.run = void 0;
|
|
const path_1 = __importDefault(require("path"));
|
|
const JsonToTs_1 = require("./JsonToTs");
|
|
const main_1 = require("./main");
|
|
const fs = require('fs');
|
|
const excel = require('exceljs');
|
|
/**
|
|
* Excel转Json数据
|
|
* @param {*} src 读取的excel文件目录
|
|
* @param {*} dst 导出的json文件目录
|
|
* @param {*} name excel文件名
|
|
* @param {*} isClient 是否为客户端数据
|
|
*/
|
|
async function convert(src, dst, name, isClient) {
|
|
let r = {};
|
|
let names = []; // 文名字段名
|
|
let keys = []; // 字段名
|
|
let types = []; // 通用字段数据类型
|
|
let types_client = {}; // 客户端数据类型
|
|
let primary = []; // 多主键配置
|
|
// let primary_index: number[] = [];
|
|
const workbook = new excel.Workbook();
|
|
// 读取excel
|
|
await workbook.xlsx.readFile(src);
|
|
const worksheet = workbook.getWorksheet(1); // 获取第一个worksheet
|
|
let rowNum = 0;
|
|
let rowStartIndex = 0;
|
|
worksheet.eachRow((row, rowNumber) => {
|
|
let data = {};
|
|
// console.log(row.values);
|
|
if (rowNumber <= 3) {
|
|
for (let index = 0; index < row.values.length; index++) {
|
|
const value = row.values[index];
|
|
if (value) {
|
|
// console.log(value);
|
|
if (rowNumber === 1) {
|
|
keys.push(value); // 字段英文名
|
|
if (primary.length == 0) {
|
|
rowStartIndex = index;
|
|
primary.push(value);
|
|
}
|
|
}
|
|
else if (rowNumber === 2) {
|
|
types.push(value); // 通用字段数据类型
|
|
}
|
|
else if (rowNumber === 3) {
|
|
names.push(value); // 字段中文名
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
if (rowNum == 0) {
|
|
rowNum = Math.min(keys.length, types.length, names.length);
|
|
}
|
|
for (let index = 0; index < rowNum; index++) {
|
|
const value = row.values[index + rowStartIndex];
|
|
let type = types[index];
|
|
let key = keys[index];
|
|
switch (type) {
|
|
case "int":
|
|
case "long":
|
|
data[key] = value ? parseInt(value) : 0;
|
|
types_client[key] = {
|
|
en: "number",
|
|
zh: names[index]
|
|
};
|
|
break;
|
|
case "float":
|
|
case "double":
|
|
data[key] = value ? parseFloat(value) : 0;
|
|
types_client[key] = {
|
|
en: "number",
|
|
zh: names[index]
|
|
};
|
|
break;
|
|
case "string":
|
|
data[key] = value ? value : "";
|
|
types_client[key] = {
|
|
en: "string",
|
|
zh: names[index]
|
|
};
|
|
break;
|
|
case "boolean":
|
|
data[key] = value == "true";
|
|
types_client[key] = {
|
|
en: "boolean",
|
|
zh: names[index]
|
|
};
|
|
break;
|
|
// case "any":
|
|
// data[key] = JSON.parse(value);
|
|
// types_client[key] = {
|
|
// en: "any",
|
|
// zh: names[index]
|
|
// };
|
|
// break;
|
|
case "cobj":
|
|
try {
|
|
data[key] = value ? JSON.parse(`{"${value.toString().replace(/[:]+/g, "\":").replace(/[,]+/g, ",\"")}}`) : "{}";
|
|
types_client[key] = {
|
|
en: "object",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-cobj-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr":
|
|
try {
|
|
let newValue = value ? `["${value.toString().replace(/[,]+/g, "\",\"")}"]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "string[]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr_int":
|
|
case "carr_long":
|
|
case "carr_float":
|
|
case "carr_double":
|
|
try {
|
|
let newValue = value ? `[${value}]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "number[]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr_int`);
|
|
}
|
|
break;
|
|
case "carr_string":
|
|
try {
|
|
let newValue;
|
|
if (value) {
|
|
if (value[0] == "\"") {
|
|
newValue = `[${value}]`;
|
|
}
|
|
else {
|
|
newValue = `["${value.toString().replace(/[,]+/g, "\",\"")}"]`;
|
|
}
|
|
}
|
|
else {
|
|
newValue = "[]";
|
|
}
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "string[]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr_string-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr_object":
|
|
case "carr_obj":
|
|
try {
|
|
let newValue = value ? `[{"${value.toString().replace(/[;]+/g, ",").replace(/[:]+/g, "\":").replace(/[,]+/g, "},{\"")}}]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "object[]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr_object-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr2":
|
|
try {
|
|
let newValue;
|
|
if (value) {
|
|
newValue = `"${value.toString().replace(/[:]+/g, "\":\"").replace(/[,]+/g, "\",\"")}"`;
|
|
newValue = `[[${newValue.replace(/[,]+/g, "],[").replace(/[:]+/g, ",")}]]`;
|
|
}
|
|
else {
|
|
newValue = "[]";
|
|
}
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "string[][]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr2-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr2_int":
|
|
case "carr2_long":
|
|
case "carr2_float":
|
|
case "carr2_double":
|
|
try {
|
|
let newValue = value ? `[[${value.toString().replace(/[,]+/g, "],[").replace(/[:]+/g, ",")}]]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "number[][]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr2_int-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr2_obj":
|
|
try {
|
|
let newValue = value ? `[{"${value.toString().replace(/[,]+/g, "\",\"").replace(/[:]+/g, "\":\"").replace(/[,]+/g, "},{").replace(/[;]+/g, "\",\"")}"}]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "object[][]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr2-- ${value}`);
|
|
}
|
|
break;
|
|
case "carr2_objnum":
|
|
try {
|
|
let newValue = value ? `[{"${value.toString().replace(/[:]+/g, "\":").replace(/[,]+/g, "},{\"").replace(/[;]+/g, ",\"")}}]` : "[]";
|
|
data[key] = JSON.parse(newValue);
|
|
types_client[key] = {
|
|
en: "object[][]",
|
|
zh: names[index]
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
console.log(`格式错误-carr2_objnum-- ${value}`);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// 生成数据(多主键)
|
|
if (rowNumber > 3) {
|
|
let temp = null;
|
|
for (var i = 0; i < primary.length; i++) {
|
|
let k = primary[i];
|
|
let id = data[k];
|
|
if (primary.length == 1) {
|
|
r[id] = data;
|
|
}
|
|
else {
|
|
if (i == primary.length - 1) {
|
|
temp[id] = data;
|
|
}
|
|
else if (i == 0) {
|
|
if (r[id] == undefined) {
|
|
r[id] = {};
|
|
}
|
|
temp = r[id];
|
|
}
|
|
else {
|
|
temp[id] = {};
|
|
temp = temp[id];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
// 写入流
|
|
if (r["undefined"] == null) {
|
|
await fs.writeFileSync(dst, JSON.stringify(r));
|
|
// 生成客户端脚本
|
|
if (isClient)
|
|
(0, JsonToTs_1.createTs)(name, types_client, r, primary);
|
|
console.log(isClient ? "客户端数据" : "服务器数据", "生成成功", dst);
|
|
}
|
|
else {
|
|
console.warn(isClient ? "客户端数据" : "服务器数据", "无数据", dst);
|
|
console.warn(r);
|
|
}
|
|
}
|
|
function run() {
|
|
var inputExcelPath = path_1.default.join(__dirname, main_1.config.PathExcel);
|
|
var outJsonPath = path_1.default.join(__dirname, main_1.config.PathJson);
|
|
const files = fs.readdirSync(inputExcelPath);
|
|
files.forEach((f) => {
|
|
let name = f.substring(0, f.indexOf("."));
|
|
let ext = f.toString().substring(f.lastIndexOf(".") + 1);
|
|
if (ext == "xlsx") {
|
|
convert(inputExcelPath + f, outJsonPath + name + ".json", name, true); // 客户端数据
|
|
}
|
|
});
|
|
}
|
|
exports.run = run;
|