文章序
一个js文件,可以便捷的替换目标文件夹下所有文件的目标字段为其他内容,可以用在任何需要批量替换的地方
代码
const fs = require('fs');
const nodeXlsx = require('node-xlsx');
//处理excel
const xlsx = nodeXlsx.parse('./string.xlsx');
let excel_content = xlsx[0].data; //取出excel文件中的第一个工作表中的全部数据
excel_content.splice(0, 1); //一般来说表中的第一条数据可能是标题没有用,所以删掉
//遍历目录找到所有文件
function run(path) {
const files = fs.readdirSync(path);
files.forEach(file => {
const newPath = path + '/' + file;
if (fs.statSync(newPath).isFile()) {
replaceI18n(newPath, excel_content);
} else {
run(newPath);
}
})
}
//替换文件内国际化
function replaceI18n(path, excel) {
const data = fs.readFileSync(path, 'utf-8');
const list = data.split(',');
for(let key1 in list) {
for(let key2 in excel) {
if(key1 = key2) {
list[key1] = excel[key2];
break;
}
}
}
}
run('./lang/i18n');
网友评论