json.excel
Version:
json to excel,or excel to json
52 lines (37 loc) • 1.57 kB
JavaScript
;
var path=require('path');
var fs=require('fs');
var xlsx=require('node-xlsx');
var handing=require('../libs/handing');
exports=module.exports=function(grunt){
grunt.registerMultiTask('go','json to excel,or excel to json',function(){
var options=this.options({
report:false
});
var json_file=options.json;
var excel_file=options.excel;
var to=options.to||'json';
var formating=options.formating === undefined ? true : options.formating;
if(!(json_file && excel_file)){
return console.log('there must be json and excel file directory!');
}
json_file=path.normalize(json_file);
excel_file=path.normalize(excel_file);
if(to === 'excel'){
var buffer=grunt.file.read(json_file);
var jsonstring=buffer.toString('utf8',0,buffer.length);
jsonstring=JSON.parse(jsonstring);
jsonstring=handing.json2excel(jsonstring);
var file=xlsx.build(jsonstring);
return grunt.file.write(excel_file,file,'binary');
}else{
var obj=xlsx.parse(excel_file);
obj=handing.excel2json(obj);
if(formating){
return grunt.file.write(json_file,JSON.stringify(obj, null, 4));
}else{
return grunt.file.write(json_file,JSON.stringify(obj));
}
}
});
};