dd
This commit is contained in:
324
extensions/Excel转JSON/panel/index.js
Normal file
324
extensions/Excel转JSON/panel/index.js
Normal file
@@ -0,0 +1,324 @@
|
||||
'use strict';
|
||||
// panel/index.js, this filename needs to match the one registered in package.json
|
||||
const packageName = "exceltojson";
|
||||
const fs = require('fs');
|
||||
const { join } = require('path');
|
||||
const path = require('path');
|
||||
const xlsx = require('../node_modules/node-xlsx');
|
||||
const Store = require('../node_modules/electron-store');
|
||||
const Vue = require('../vue');
|
||||
const store = new Store();
|
||||
var s_data= {};
|
||||
let json_name = '.json'; //生成的文件名字
|
||||
exports.template = fs.readFileSync(join(__dirname, './index.html'), 'utf8') ,
|
||||
exports.style = fs.readFileSync(join( __dirname,'./index.css'), 'utf8'),
|
||||
|
||||
|
||||
exports.$ = {
|
||||
app: '#app'
|
||||
}
|
||||
exports.ready = function() {
|
||||
new Vue({
|
||||
el: this.$.app,
|
||||
data () {
|
||||
return {
|
||||
excel_dir:"",
|
||||
json_dir:"",
|
||||
ts_dir:"",
|
||||
msg:"",
|
||||
}
|
||||
|
||||
},
|
||||
created(){
|
||||
//读取缓存的值
|
||||
console.log('created')
|
||||
let store_excel = store.get('excel_dir');
|
||||
let store_json = store.get('json_dir');
|
||||
let ts_dir = store.get('ts_dir');
|
||||
if(store_excel != undefined)
|
||||
{
|
||||
this.excel_dir = store_excel;
|
||||
}
|
||||
if(store_json != undefined)
|
||||
{
|
||||
this.json_dir = store_json
|
||||
}
|
||||
if(ts_dir != undefined)
|
||||
{
|
||||
this.ts_dir = ts_dir
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
onBtnClcik(){
|
||||
// todo
|
||||
console.log('onBtnClcik')
|
||||
},
|
||||
async btn_excel(){
|
||||
const config = {
|
||||
type: 'directory',
|
||||
};
|
||||
const res = await Editor.Dialog.select(config);
|
||||
|
||||
if (!res.canceled)
|
||||
{
|
||||
let dir = res.filePaths[0];
|
||||
store.set('excel_dir', dir);
|
||||
this.excel_dir = dir;
|
||||
}
|
||||
},
|
||||
async btn_json(){
|
||||
const config = {
|
||||
type: 'directory',
|
||||
};
|
||||
const res = await Editor.Dialog.select(config);
|
||||
if (!res.canceled)
|
||||
{
|
||||
let dir =res.filePaths[0];
|
||||
store.set('json_dir',dir);
|
||||
this.json_dir = dir;
|
||||
}
|
||||
},
|
||||
async btn_ts(){
|
||||
const config = {
|
||||
type: 'directory',
|
||||
};
|
||||
const res = await Editor.Dialog.select(config);
|
||||
if (!res.canceled)
|
||||
{
|
||||
let dir =res.filePaths[0];
|
||||
store.set('ts_dir',dir);
|
||||
this.ts_dir = dir;
|
||||
}
|
||||
},
|
||||
btn_go(){
|
||||
console.log('btn_go')
|
||||
if(this.excel_dir == '' || this.excel_dir == undefined)
|
||||
{
|
||||
alert('请输入excel所在目录');
|
||||
return;
|
||||
}
|
||||
else if(this.json_dir == '' || this.json_dir == undefined)
|
||||
{
|
||||
alert('请输入json输出目录');
|
||||
return;
|
||||
}
|
||||
if(this.json_dir.indexOf('resources') === -1) {
|
||||
alert('json输出目录必须为resources下目录');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readdir(path.join(this.excel_dir+'/'), (err, files)=>{
|
||||
for(let i=0;i<files.length;i++)
|
||||
{
|
||||
|
||||
if(~files[i].indexOf('.xls') && files[i].indexOf('.~') === -1)
|
||||
{
|
||||
let file_name = files[i].split(".")[0];
|
||||
//开始将excel文件转变成json文件
|
||||
//读取文件内容
|
||||
let obj = xlsx.parse(path.join(this.excel_dir+'/', files[i]));
|
||||
var excelObj=obj[0].data;
|
||||
console.log(excelObj);
|
||||
|
||||
var array_json = {
|
||||
"list":[],
|
||||
};
|
||||
var array_json_index = [];
|
||||
|
||||
//将第一行文字注释先干掉
|
||||
for(let i=0,j=0;i<excelObj.length;i++,j++)
|
||||
{
|
||||
array_json_index[j] = excelObj[i];
|
||||
}
|
||||
const firstData = array_json_index[0];
|
||||
const typeData = array_json_index[1];
|
||||
//将唯一键设置为key;
|
||||
for(let i=3;i<array_json_index.length;i++)
|
||||
{
|
||||
const curDataList = array_json_index[i];
|
||||
const resultData ={};
|
||||
for (let index = 0; index < firstData.length; index++) {
|
||||
const keyStr = firstData[index];
|
||||
if(typeData[index] === 'int') {
|
||||
resultData[keyStr] = parseInt(curDataList[index]) ;
|
||||
} else if(typeData[index] === 'float') {
|
||||
resultData[keyStr] = parseFloat(curDataList[index]) ;
|
||||
} else if(typeData[index] === 'boolean') {
|
||||
resultData[keyStr] = Boolean(curDataList[index]) ;
|
||||
}
|
||||
else {
|
||||
resultData[keyStr] = curDataList[index];
|
||||
}
|
||||
}
|
||||
|
||||
array_json.list.push(resultData)
|
||||
}
|
||||
|
||||
s_data = {};
|
||||
s_data= array_json;
|
||||
let s_str = JSON.stringify(s_data);
|
||||
fs.writeFile(path.join(this.json_dir+`/${file_name}`)+json_name,s_str,function(err){
|
||||
if (err) {
|
||||
console.log('write is error...',err);
|
||||
} else {
|
||||
console.log('转换成功');
|
||||
// Editor.Ipc.sendToPanel(packageName,'sendMessage', '转换成功');
|
||||
|
||||
}
|
||||
});
|
||||
this.savaTS(array_json_index,file_name)
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
savaTS(jsonData,file_name) {
|
||||
const className = `${file_name}List`;
|
||||
let dmUrl = path.join(this.ts_dir+`/${className}.ts`);
|
||||
console.log('dmUrl...',dmUrl);
|
||||
const interfaceName = `I${file_name}Info`;
|
||||
|
||||
let interfaceStr = `\n\n\n\nexport interface ${interfaceName} {\n`
|
||||
const firstData = jsonData[0];
|
||||
const secondData = jsonData[1];
|
||||
const thirdData = jsonData[2];
|
||||
let allKeyStr = '';
|
||||
// interface 组装
|
||||
for (let index = 0; index < firstData.length; index++) {
|
||||
const element = firstData[index];
|
||||
const desc = thirdData[index];
|
||||
const typeDesc = this.getTypeByStr(secondData[index]);
|
||||
const valueStr = `\t/**\n\t* ${desc}\n\t*\n\t* @type {${typeDesc}}\n\t* @memberof ${interfaceName}\n\t*/\n\t${element}: ${typeDesc}\n`;
|
||||
allKeyStr += this.addKeyStr(element,typeDesc,className,desc)+'\n'
|
||||
interfaceStr+=valueStr;
|
||||
}
|
||||
interfaceStr +='\n}'
|
||||
|
||||
// 类组装
|
||||
const getInstanceStr = this.getInstanceStr(className);
|
||||
console.log('getInstanceStr',getInstanceStr);
|
||||
let classStr = `import { resources } from "cc";\n\nclass ${className} {\n`
|
||||
const staticStr = `\t/**\n\t* 静态对象\n\t*\n\t* @public\n\t* @static\n\t* @type {${className}}\n\t* @memberof ${className}\n\t*/\n\tprivate static instance: ${className};\n`;
|
||||
const listStr = `\t/**\n\t* 列表\n\t*\n\t* @public\n\t* @type {Array<${interfaceName}>}\n\t* @memberof ${className}\n\t*/\n\tpublic list: Array<${interfaceName}> = [];\n`;
|
||||
const instanceStr = `\t/**\n\t* 配置单例\n\t*\n\t* @static\n\t* @returns {${className}}\n\t* @memberof ${className}\n\t*/\n\t${getInstanceStr}`
|
||||
const dataLengthStr = this.addDataLength(className);
|
||||
const loadConfigStr = this.loadConfigStr(file_name,className);
|
||||
const initStr = this.initStr(className);
|
||||
const exportStr = `\n\nexport default ${className}.getInstance();`;
|
||||
classStr += staticStr+listStr+instanceStr +loadConfigStr+'\n'+initStr+'\n'+ allKeyStr+'\n'+dataLengthStr + '\n}' ;
|
||||
let resultStr = classStr + exportStr+interfaceStr;
|
||||
console.log('resultStr',resultStr);
|
||||
fs.writeFile(dmUrl,resultStr,function(err){
|
||||
if (err) {
|
||||
console.log('write is error...',err);
|
||||
} else {
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
},
|
||||
getInstanceStr(className) {
|
||||
|
||||
|
||||
const loadStr = `public static getInstance(): ${className} {
|
||||
if (!${className}.instance) {
|
||||
${className}.instance = new ${className}();
|
||||
}
|
||||
|
||||
return ${className}.instance;
|
||||
}
|
||||
`
|
||||
return loadStr
|
||||
},
|
||||
|
||||
initStr(className) {
|
||||
const configStr = `
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @memberof TableBigWheelConfigList
|
||||
*/
|
||||
public async init(): Promise<void> {
|
||||
if(${className}.instance.list.length === 0) {
|
||||
await this.loadConfig();
|
||||
}
|
||||
}`
|
||||
|
||||
return configStr;
|
||||
},
|
||||
|
||||
loadConfigStr(file_name,className) {
|
||||
const index = this.json_dir.indexOf('resources');
|
||||
const jsonDir = this.json_dir.substring(index+10,this.json_dir.length);
|
||||
const jsonPath = `${jsonDir}/${file_name}`;
|
||||
const configStr = `
|
||||
/**
|
||||
* 加载文件
|
||||
*
|
||||
* @return {*} {Promise<void>}
|
||||
* @memberof LanguageList
|
||||
*/
|
||||
public async loadConfig(): Promise<void> {
|
||||
const data: any = await new Promise((resolve: any, reject: any): any => {
|
||||
resources.load('${jsonPath}', (error: any, texture: any): any => {
|
||||
if (error) {
|
||||
console.log('loadRes error', '${jsonPath}', JSON.stringify(error));
|
||||
resolve(null);
|
||||
} else {
|
||||
resolve(texture);
|
||||
}
|
||||
});
|
||||
});
|
||||
${className}.instance.list = data.json.list;
|
||||
}`
|
||||
return configStr;
|
||||
},
|
||||
|
||||
addDataLength(className) {
|
||||
const dataLengthStr = `
|
||||
/**
|
||||
* 数据长度
|
||||
*
|
||||
* @return {*} {number}
|
||||
* @memberof ${className}
|
||||
*/
|
||||
public dataLength(): number {
|
||||
return this.list.length;
|
||||
}`
|
||||
return dataLengthStr;
|
||||
},
|
||||
addKeyStr(key,type,className,titleDesc) {
|
||||
const keyStr = `
|
||||
/**
|
||||
* ${titleDesc}
|
||||
*
|
||||
* @param {number} index
|
||||
* @return {*} {${type}}
|
||||
* @memberof ${className}
|
||||
*/
|
||||
public ${key}(index: number): ${type} {
|
||||
return this.list[index].${key};
|
||||
}`
|
||||
return keyStr
|
||||
},
|
||||
getTypeByStr(typeStr) {
|
||||
if(typeStr === 'int' || typeStr === 'float') {
|
||||
return 'number';
|
||||
} else if(typeStr === 'float') {
|
||||
resultData[keyStr] = parseFloat(curDataList[index]) ;
|
||||
} else if(typeStr=== 'boolean') {
|
||||
return 'boolean';
|
||||
} else {
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
})
|
||||
};
|
||||
Reference in New Issue
Block a user