mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
update
This commit is contained in:
308
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/ExcelToJson.js
vendored
Normal file
308
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/ExcelToJson.js
vendored
Normal file
@@ -0,0 +1,308 @@
|
||||
"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;
|
62
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/JsonToTs.js
vendored
Normal file
62
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/JsonToTs.js
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createTs = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const main_1 = require("./main");
|
||||
const fs = require('fs');
|
||||
async function createTs(name, fieldType, data, primary) {
|
||||
// 主键参数
|
||||
var script_init_params = "";
|
||||
var script_init_data = "";
|
||||
var script_init_var = "";
|
||||
var script_init_value = "";
|
||||
primary.forEach(key => {
|
||||
script_init_params += `${key}: number, `;
|
||||
script_init_data += `[${key}]`;
|
||||
script_init_var += `/** ${fieldType[key].zh} */\r private _${key}: number = 0;\r `;
|
||||
script_init_value += `this._${key} = ${key};\r `;
|
||||
});
|
||||
script_init_params = script_init_params.substring(0, script_init_params.length - 2);
|
||||
script_init_var = script_init_var.substring(0, script_init_var.length - 5);
|
||||
script_init_value = script_init_value.substring(0, script_init_value.length - 9);
|
||||
// 字段
|
||||
var field = "";
|
||||
for (var id in fieldType) {
|
||||
// if (primary.indexOf(id) == -1) {
|
||||
field += `
|
||||
/** ${fieldType[id].zh} */
|
||||
get ${id}(): ${fieldType[id].en} {
|
||||
return this.data.${id};
|
||||
}`;
|
||||
// }
|
||||
}
|
||||
var script = `import { JsonUtil } from "../../../../extensions/ngame/assets/ngame/util/JsonUtil";
|
||||
|
||||
export class Table${name} {
|
||||
static TableName: string = "${name}";
|
||||
|
||||
static getAllConfig(): { [id: string]: Table${name} } {
|
||||
return JsonUtil.get(Table${name}.TableName);
|
||||
}
|
||||
|
||||
static getConfig(id: number | string) {
|
||||
return Table${name}.getAllConfig()[id] as Table${name};
|
||||
}
|
||||
|
||||
private data: any;
|
||||
|
||||
init(${script_init_params}) {
|
||||
var table = JsonUtil.get(Table${name}.TableName);
|
||||
this.data = table${script_init_data};
|
||||
${script_init_value}
|
||||
}
|
||||
${script_init_var}
|
||||
${field}
|
||||
}`;
|
||||
var p = path_1.default.join(__dirname, main_1.config.PathTs);
|
||||
await fs.writeFileSync(`${p}Table${name}.ts`, script);
|
||||
}
|
||||
exports.createTs = createTs;
|
38
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/main.js
vendored
Normal file
38
JisolGameCocos/extensions/oops-plugin-excel-to-json/dist/main.js
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.methods = exports.config = exports.unload = exports.load = void 0;
|
||||
const ExcelToJson_1 = require("./ExcelToJson");
|
||||
/**
|
||||
* @en Hooks triggered after extension loading is complete
|
||||
* @zh 扩展加载完成后触发的钩子
|
||||
*/
|
||||
function load() { }
|
||||
exports.load = load;
|
||||
/**
|
||||
* @en Hooks triggered after extension uninstallation is complete
|
||||
* @zh 扩展卸载完成后触发的钩子
|
||||
*/
|
||||
function unload() { }
|
||||
exports.unload = unload;
|
||||
/**
|
||||
* @en
|
||||
* @zh 为扩展的主进程的注册方法
|
||||
*/
|
||||
exports.methods = {
|
||||
async excelToJson() {
|
||||
exports.config = await Editor.Profile.getProject("oops-plugin-excel-to-json");
|
||||
if (exports.config.PathExcel == null) {
|
||||
console.warn("项目->项目设置->Excel To Json->PathExcel 配置路径");
|
||||
return;
|
||||
}
|
||||
if (exports.config.PathJson == null) {
|
||||
console.warn("项目->项目设置->Excel To Json->PathJson 配置路径");
|
||||
return;
|
||||
}
|
||||
if (exports.config.PathTs == null) {
|
||||
console.warn("项目->项目设置->Excel To Json->PathTs 配置路径");
|
||||
return;
|
||||
}
|
||||
(0, ExcelToJson_1.run)();
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user