lg_frontend/test/test.js

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const XLSX = require('xlsx');
const fs = require('fs');
// 读取xlsx文件
const workbook = XLSX.readFile('./test.xlsx');
// 读取特定的worksheet默认第一个worksheet
const worksheet = workbook.Sheets['Sheet2'];
// 将worksheet转换为JSON对象
const jsonData = XLSX.utils.sheet_to_json(worksheet);
function dmsToDecimal(degrees, minutes, seconds) {
return degrees + minutes / 60 + seconds / 3600;
}
const results = []
for (const jsonDatum of jsonData) {
const location = jsonDatum['经纬度']
const [lat, lon] = location.split(' ')
let latStr = lat.replace(/\°|\'|["(N|E)]/g, '@')
let lonStr = lon.replace(/\°|\'|["(N|E)]/g, '@')
const [latDegrees, latMinutes, latSeconds] = latStr.split('@')
const [longDegrees, longMinutes, longSeconds] = lonStr.split('@')
const latitude = dmsToDecimal(parseInt(latDegrees), parseInt(latMinutes), parseInt(latSeconds));
const longitude = dmsToDecimal(parseInt(longDegrees), parseInt(longMinutes),parseInt( longSeconds));
results.push( {
longitude,
latitude
})
}
// 文件内容
const content = JSON.stringify(results, null, 2)
// 文件路径
const filePath = './output.json';
// 写入文件
fs.writeFile(filePath, content, 'utf8', function(err) {
if (err) {
return console.log(err);
}
console.log('文件已保存');
});