EXCEL转json软件使用,下载,cocos js读取

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
	require('TableUtil').initAllTable ();//加载所有表
    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值