const xlsx = require('xlsx');
const https = require('node:https');
doRank();
function doRank(){
const arr = [];
const allUserList = getAllData();
// console.log(allUserList);
getRank().then(result=>{
const rankList = result.data;
for(const item of rankList){
const obj = {};
for(const v of allUserList){
if(item.userEmail === v[1]){
obj.mail = item.userEmail;
obj.rank = item.rank;
if(v[2]){
const temp = JSON.parse(JSON.parse(v[2]));
obj.country = temp['name'];
}
if(v[3]){
const temp = JSON.parse(v[3]);
obj.name = temp['receiverName'];
obj.phone = temp['phone'];
obj.code = temp['code'];
obj.address = temp['detail'];
}
}
}
arr.push(obj);
}
console.log(arr);//获取最终数据
//写入xls
let jsonWorkSheet = xlsx.utils.json_to_sheet(arr);
let workBook = {
SheetNames: ['jsonWorkSheet'],
Sheets: {
'jsonWorkSheet': jsonWorkSheet,
}
};
// 将workBook写入文件
xlsx.writeFile(workBook, "./final.xlsx");
});
}
function getAllData(){
let arr = [];
let workbook = xlsx.readFile('./mls.xlsx');
let sheetNames = workbook.SheetNames;
// 获取第一个workSheet
let sheet1 = workbook.Sheets[sheetNames[0]];
// console.log(sheet1);
let range = xlsx.utils.decode_range(sheet1['!ref']);
//循环获取单元格值
for (let R = range.s.r; R <= range.e.r; ++R) {
let row_value = '';
const arr2 = [];
for (let C = range.s.c; C <= range.e.c; ++C) {
let cell_address = {c: C, r: R}; //获取单元格地址
let cell = xlsx.utils.encode_cell(cell_address); //根据单元格地址获取单元格
//获取单元格值
if (sheet1[cell]) {
// 如果出现乱码可以使用iconv-lite进行转码
// row_value += iconv.decode(sheet1[cell].v, 'gbk') + ", ";
row_value += sheet1[cell].v + ", ";
arr2.push(sheet1[cell].v);
} else {
row_value += ", ";
arr2.push('');
}
}
arr.push(arr2);
}
return arr;
}
// console.log(arr);
function getRank(){
return new Promise(resolve=>{
https.get('https://www.xxx.com/rankAll',(res)=>{
res.setEncoding('utf8');
var rawData = '';
res.on('data',(chunk)=>{
rawData += chunk;
});
res.on('end',()=>{
try{
const parseData = JSON.parse(rawData);
resolve(parseData);
}catch(err){
console.log(err);
}
});
});
});
}
网友评论