美文网首页
cocos creator excel转json

cocos creator excel转json

作者: cmd_ts | 来源:发表于2020-04-13 21:06 被阅读0次
 start () {
        console.log('csv to json');
        cc.loader.loadRes('csv/player.csv',(err,data)=>{
            if(err)
            {
                console.log('err:',err);
                
            }
            else
            {
                let asd = this.CSVToArray(data.text,',');
                console.log('asd:',asd);
                let array_json = {};
                let array_json_index = [];
                console.log('asd.length',asd.length)
                console.log('asd[4].length',asd[19].length);

                for(let i=0;i<asd[0].length;i++)
                {
                    array_json_index[i] = asd[0][i];
                }

                console.log('array_json_index[i]:',array_json_index);
                

                for(let i=1;i<asd.length;i++)
                {
                    array_json[i-1] = {};
                    for(let j=0;j<asd[i].length;j++)
                    {    
                        array_json[i-1][array_json_index[j]] = asd[i][j];
                        //console.log('asd[i][j]',i,j,asd[i][j]);
                    }
                }
                console.log('array_json:',array_json);
            }
            
            
        })
    }

    CSVToArray( strData, strDelimiter ) 
    {
        strDelimiter = (strDelimiter || ",");
        var objPattern = new RegExp(
        (
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
                ),
            "gi"
        );
        // var arrData = [{}];
        var arrData = [[]];
        //var arrData = {};
        var arrMatches = null;
        while (arrMatches = objPattern.exec( strData ))
        {
            var strMatchedDelimiter = arrMatches[ 1 ];
            if( strMatchedDelimiter.length &&(strMatchedDelimiter != strDelimiter))
            {
                arrData.push( [] );
            }
            if (arrMatches[ 2 ]){
                var strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( "\"\"", "g" ),
                    "\""
                );
            } else {
                var strMatchedValue = arrMatches[ 3 ];
            }
            console.log('strMatchedValue:',arrData.length,strMatchedValue);
            
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }
        if(arrData.length>0){
            arrData.pop();
        }
        return arrData;
    }
111.png

后续:
在cocos中转化excel影响性能,决定在外面将excel文件转化为json


123.png 222.png 3.png 4.png

npm install node-xlsx --save

var path = require("path");
var fs = require("fs");
var xlsx = require('node-xlsx');


var dirs = [];

//读取文件夹里所有的文件
fs.readdir(__dirname+'/excel/', function(err, files){
    
    (function iterator(i){
      if(i == files.length) {
        console.log('所有文件在一个数组里:',dirs);
        //把文件名提取出来
        for(let i1 =0;i1<dirs.length;i1++)
        {
            let ss = dirs[i1].split(".");
            console.log('把文件名提取出来:',ss[0]);

            //开始将excel文件转变成json文件
            //读取文件内容
            var obj = xlsx.parse(__dirname+'/excel/'+dirs[i1]);
            var excelObj=obj[0].data;
            console.log(excelObj);
            var data = [];
            for(var i in excelObj){
              var arr=[];
              var value=excelObj[i];
              for(var j in value){
                arr.push(value[j]);
              }
              data.push(arr);
            }

            console.log('data:',data);

            var array_json = {};
            var array_json_index = [];

            for(let i=1,j=0;i<data.length;i++,j++)
            {
                array_json_index[j] = data[i];
            }

            console.log('array_json_index:',array_json_index);

            for(let i=1;i<array_json_index.length;i++)
            {
                array_json[array_json_index[i][0]] = {};
                console.log('array_json_index[0].length',array_json_index[0].length);
                for(let j=1;j<array_json_index[0].length;j++)
                {
                  array_json[array_json_index[i][0]][array_json_index[0][j]] = array_json_index[i][j];
                }
            }
            console.log('array_json:',array_json);



            let str = JSON.stringify(array_json)
             
            let dir_json = ss[0]+'.json';
            fs.writeFile(__dirname+'/excel/'+dir_json,str,function(err){
            if (err) {res.status(500).send('Server is error...')}
            })
                    }



        return ;
      }
      fs.stat(path.join(__dirname+'/excel/', files[i]), function(err, data){     
        if(data.isFile()){               
            dirs.push(files[i]);
        }
        iterator(i+1);
       });   
    })(0);
});


相关文章

网友评论

      本文标题:cocos creator excel转json

      本文链接:https://www.haomeiwen.com/subject/nhlomhtx.html