EXCEL转json软件使用,下载,cocos js读取
程序员文章站
2022-07-13 13:39:40
...
excel格式:
第一行id,必须英文
第二行数据类型
第三行文字描述
文件名必须英文
第一列默认为主键
使用:
下载解压打开config,将路径改为自己导入和导出文件夹的位置
点击excel2json.bat运行
读取json:
var TableUtil = function () {
};
var p = TableUtil.prototype;
var allTables = {};
p.initAllTable = function () {
//p.loadCSV('item', allTables);
//p.loadCSV('bullet', allTables);
p.loadJson();
};
p.loadJson = function () {
allTables = {};
// 加载 test assets 目录下所有资源
cc.loader.loadResDir("json", function (err, assets) {
// ...
if (err) {
cc.log(err);
return;
}
//cc.log('assets:', assets);
for (let i = 0; i < assets.length; i++) {
let asset = assets[i];
allTables[asset.name] = asset.json;
}
cc.log('allTables:', allTables);
//let testItem = p.GetSkillById(100110);
//cc.log('testItem:', testItem);
});
};
p.GetItemById = function (id) {
let item = null;
let table = allTables.item;
if (table != null) {
item = table['' + id];
}
return item;
};
p.GetItemIconById = function (id) {
let item = p.GetItemById(id);
if (item) {
return item.icon;
}
return null;
};
p.GetBulletById = function (id) {
let item = null;
let table = allTables.bullet;
//cc.log('GetBulletById, id=', id);
if (table != null) {
item = table['' + id];
}
return item;
};
p.GetSkillById = function (id) {
let item = null;
let table = allTables.skill;
cc.log('GetSkillById, id=', id);
if (table != null) {
item = table['' + id];
}
return item;
};
p.GetRoleById = function (id) {
let item = null;
let table = allTables.role;
cc.log('GetRoleById, id=', id);
if (table != null) {
item = table['' + id];
}
return item;
};
p.GetTableByName = function (tableName) {
return allTables[tableName];
};
/*
p.loadCSV = function (tableName, tables) {
let table = [];
cc.loader.loadRes('csv/' + tableName, function (err, data) {
if (err) {
cc.error(err.message || err);
return;
}
cc.log('data:', data);
let mapArr = data.text.split("\r\n");
let names = mapArr[0].split(",");
cc.log('loadCSV, names:', names);
for (var i = 1; i < mapArr.length; i++) {
let arr = mapArr[i].split(",");
let id = parseInt(arr[0]);
if (id >= 0) {
let row = {};
row.id = id;
for (let j = 1; j < names.length && j < arr.length; j++) {
row[names[j]] = arr[j];
}
table.push(row);
}
}
//...
tables[tableName] = table;
cc.log('loadCSV: ', tableName, table);
});
};
p.GetItemById = function (id) {
let item = null;
let table = allTables.item;
if (table != null) {
for (let i = 0; i < table.length; i++) {
if (table[i].id == id) {
item = table[i];
break;
}
}
}
return item;
};
*/
module.exports = p;
使用:
//json文件路径:resources/json
var commoditys = require('TableUtil').GetTableByName('XML');//XML为json文件名
var commodity = commoditys["thing1"];//通过对象id取到对象
var name = commodity.name;//取对象属性
var type = commodity.type;
//遍历
for (const key in object) {
if (object.hasOwnProperty(key)) {
const element = object[key];
}
}
链接:https://pan.baidu.com/s/1lD_cf54ff-3S-F0_Zy4SSQ
提取码:oba2