TLE2ToCZML/xlsx.js

178 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2024-02-22 11:07:01 +08:00
const fs = require('fs')
const xlsx = require("node-xlsx")
/**
* 缺位补0
*/
const padding2 = (part) => {
if (part.length === 1) {
return "0" + part
} else {
return part;
}
}
/**
* 日期格式转换
*
* @param {date|number} date 日期
* @param {string} fmt 要转换为的格式默认 yyy-MM-dd HH:mm:ss
*/
const formatDate = (date, fmt) => {
date = date == undefined ? new Date() : date;
date = typeof date == 'number' ? new Date(date) : date;
fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
let obj =
{
'y': date.getFullYear(), // 年份注意必须用getFullYear
'M': date.getMonth() + 1, // 月份注意是从0-11
'd': date.getDate(), // 日期
'q': Math.floor((date.getMonth() + 3) / 3), // 季度
'w': date.getDay(), // 星期注意是0-6
'H': date.getHours(), // 24小时制
'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
'm': date.getMinutes(), // 分钟
's': date.getSeconds(), // 秒
'S': date.getMilliseconds() // 毫秒
};
let week = ['天', '一', '二', '三', '四', '五', '六'];
for (let i in obj) {
fmt = fmt.replace(new RegExp(i + '+', 'g'), function (m) {
let val = obj[i] + '';
if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
for (let j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
return m.length == 1 ? val : val.substring(val.length - m.length);
});
}
return fmt;
}
/**
* 解析Excel表达的日期数字并转换为指定格式的日期字符串
*
* @param {number} numb Excel解析出的数字形式的日期
* @param {string} format 要转换为的格式默认 yyy-MM-dd HH:mm:ss
*/
const formatExcelDate = (numb, format) => {
const time = new Date((numb - 2) * 24 * 3600000 + 1);
time.setYear(time.getFullYear() - 70);
time.setHours(time.getHours() - 8);
return formatDate(time, format);
}
const handleDate = (date, fmt) => {
if (!date) {
return date;
}
date = date.trim ? date.trim() : date;
if (/^[\d.]+$/.test(date)) {
let dateNum = parseFloat(date);
// 大于 1000 万说明是一个毫秒数,直接解析并转换为指定格式即可
if (dateNum > 10000000) {
return formatDate(dateNum, fmt);
}
// 否则认为这个是一个 Excel 格式的日期
date = formatExcelDate(dateNum, fmt);
console.log(date, typeof (date), dateNum)
} else {
// 处理中文冒号,和 yyyy/MM/dd 格式的问题
date = date.replace(//g, ":").replace(/\//g, "-");
}
// 将不规则的格式,例如 "2020-1-1 1:3:3" 转换成 yyyy-MM-dd HH:mm:ss
// 再转为 Date 对象进行指定的格式化
console.log(date);
let dtPars = date.split(/\s+/g)
let dPars = dtPars[0].split("-");
dPars[1] = padding2(dPars[1])
dPars[2] = padding2(dPars[2])
dtPars[0] = dPars.join("-");
if (!dtPars[1]) {
dtPars[1] = '00:00:00';
} else {
let tPars = dtPars[1].split(':');
tPars[0] = padding2(tPars[0]);
// 支持分缺失
tPars[1] = padding2(tPars[1] || '00');
// 支持秒缺失
tPars[2] = padding2(tPars[2] || '00');
dtPars[1] = tPars.join(':');
}
return formatDate(new Date(dtPars.join(" ")), fmt);
}
const loadData = ()=>{
// 以 buffer 形式导入
const workSheetsFromBuffer = xlsx.parse(fs.readFileSync(`./public/excel/卫星数据匹配.xlsx`))
// console.log(JSON.stringify(workSheetsFromBuffer, null, 2))
// 以文件形式导入
// const workSheetsFromFile = xlsx.parse(`./public/excel/卫星数据匹配.xlsx`)
// console.log(JSON.stringify(workSheetsFromFile, null, 2))
let arr = {
BEIDOU:[],
GALILEO:[],
GLONASS:[],
GPS:[]
}; // 全部表中的数据
workSheetsFromBuffer.forEach((sheet) => {
if(sheet['name'] == '北斗(最终)' || sheet['name'] == 'GPS最终' || sheet['name'] == 'GLONASS最终' || sheet['name'] == 'GALILEO最终') {
let satellite
switch(sheet['name']){
case('北斗(最终)'):
satellite = 'BEIDOU'
break;
case('GPS最终'):
satellite = 'GPS'
break;
case('GLONASS最终'):
satellite = 'GLONASS'
break;
case('GALILEO最终'):
satellite = 'GALILEO'
break;
default:
break;
}
for (let i = 1; i < sheet['data'].length; i++) {
// excel第一行是是表头所以从1开始循环
let row = sheet['data'][i] // 获取行数据
if (row && row.length > 0) {
// moment处理 ISO 8601格式的时间,
if(sheet['name'] == '北斗(最终)'){
arr[`${satellite}`].push({
rawname: row[0].trim(), // row[0]对应表格里A列
showname: row[1], // row[1]对应表格里B列
airDefenseNumber: row[2], // row[2]对应表格里C列
PRN: row[3], // row[3]对应表格里D列
type: row[4], // row[4]对应表格里E列
launchDate: handleDate(row[5], 'yyyy-MM-dd'), // row[5]对应表格里F列
runningCycle: row[6], // row[6]对应表格里G列
orbitalInclination: row[7], // row[7]对应表格里H列
apogeeAltitude: row[8], // row[8]对应表格里I列
perigeeAltitude: row[9], // row[9]对应表格里J列
atomicClockType: row[10], // row[10]对应表格里K列
beidouType: row[11] // row[11]对应表格里L列
})
}else{
arr[`${satellite}`].push({
rawname: row[0].trim(), // row[0]对应表格里A列
showname: row[1], // row[1]对应表格里B列
airDefenseNumber: row[2], // row[2]对应表格里C列
PRN: row[3], // row[3]对应表格里D列
type: row[4], // row[4]对应表格里E列
launchDate: handleDate(row[5], 'yyyy-MM-dd'), // row[5]对应表格里F列
runningCycle: row[6], // row[6]对应表格里G列
orbitalInclination: row[7], // row[7]对应表格里H列
apogeeAltitude: row[8], // row[8]对应表格里I列
perigeeAltitude: row[9] // row[9]对应表格里J列
})
}
}
}
}
})
return arr
}
module.exports = { loadData }