gpt4 book ai didi

javascript - 将 excel 转换为 json 数组时无法解析日期

转载 作者:行者123 更新时间:2023-12-02 22:03:14 26 4
gpt4 key购买 nike

我必须创建一个用于文件上传的 api,它是一个 xlsx 文件。我已使用 multer 成功上传文件并能够打开它我需要解析该文件并将其转换为数组 json 对象。我的代码是这样的:-

let workbook = xlsx.readFile(`uploads/${req.file.filename}`)
let worksheet = workbook.Sheets['Sheet1']
let headers = {}
let data = []
for (z in worksheet) {
if (z[0] === '!') continue
//parse out the column, row, and value
let tt = 0
for (let i = 0; i < z.length; i++) {
if (!isNaN(z[i])) {
tt = i
break
}
tt = i
}
let col = z.substring(0, tt)
let row = parseInt(z.substring(tt))
let value = worksheet[z].v
//store header names
if (row == 1 && value) {
headers[col] = value
continue
}
if (!data[row]) data[row] = {}
data[row][headers[col]] = value
}
// drop those first two rows which are empty
data.shift()
data.shift()
console.log(data)
const studentList = []
data.map(object => {
if (object['Year of Enrollment']) object['Year of Enrollment'] = parseInt(object['Year of Enrollment'])
if (object['Semester']) object['Semester'] = parseInt(object['Semester'])
if (object['Aadhar Number']) object['Aadhar Number'] = parseInt(object['Aadhar Number'])
if (object['Phone Number']) object['Phone Number'] = parseInt(object['Phone Number'])
if (object['Alternate Phone Number']) object['Alternate Phone Number'] = parseInt(object['Alternate Phone Number'])
let obj = {
"student_first_name": object['First Name'],
"student_last_name": object['Last Name'],
"student_branch": object['Branch'],
"student_year": object['Year of Enrollment'],
"student_semester": object['Semester'],
"student_gender": object['Gender'],
"student_email": object['Email'],
"student_address": object['Address'],
"student_linkedin_profile": object['Linkedin Profile'],
"student_github_profile": object['Github Profile'],
"student_aadhar_number": object['Aadhar Number'],
"student_phone_number": object['Phone Number'],
"student_date_of_birth": object['Date of Birth'],
college_id: req.body.college_id
}
if (object['Alternate Email']) obj.student_alternate_email = object['Alternate Email']
if (object['Alternate Phone Number']) obj.student_alternate_phone_number = object['Alternate Phone Number']
studentList.push(obj)
})
console.log(studentList)

在输出中,我得到了这个,但不知道出生日期发生了什么:-

[
{
student_first_name: 'Ekansh',
student_last_name: 'Jain',
student_branch: 'CSE',
student_year: 2017,
student_semester: 6,
student_gender: 'male',
student_email: '17etcd11@technar.org',
student_address: 'Udfr',
student_linkedin_profile: 'http://linkedin.com/ekansh',
student_github_profile: 'http://github.com/ekansh',
student_aadhar_number: 123456789011,
student_phone_number: 7894561230,
student_date_of_birth: 36270,
college_id: 'college-f9NsLo7Ww6oTIlICElG5GZD5JHKTxqbF',
student_alternate_email: 'ejundertaker@gmail.com'
}
]

在 Excel 文件中,出生日期字段中的值为 20-04-1999有人可以帮助我,使输出看起来像这样:-

[
{
student_first_name: 'Ekansh',
student_last_name: 'Jain',
student_branch: 'CSE',
student_year: 2017,
student_semester: 6,
student_gender: 'male',
student_email: '17etcd11@technar.org',
student_address: 'Udfr',
student_linkedin_profile: 'http://linkedin.com/ekansh',
student_github_profile: 'http://github.com/ekansh',
student_aadhar_number: 123456789011,
student_phone_number: 7894561230,
student_date_of_birth: "1999-04-20",
college_id: 'college-f9NsLo7Ww6oTIlICElG5GZD5JHKTxqbF',
student_alternate_email: 'ejundertaker@gmail.com'
}
]

最佳答案

在读取 xlsx 文件(第一行)时只需将 cellDates true 作为选项传递,如下代码:-

let workbook = xlsx.readFile(`uploads/${req.file.filename}`, { cellDates: true })
let worksheet = workbook.Sheets['Sheet1']
let headers = {}
let data = []
for (z in worksheet) {
if (z[0] === '!') continue
//parse out the column, row, and value
let tt = 0
for (let i = 0; i < z.length; i++) {
if (!isNaN(z[i])) {
tt = i
break
}
tt = i
}
let col = z.substring(0, tt)
let row = parseInt(z.substring(tt))
let value = worksheet[z].v
//store header names
if (row == 1 && value) {
headers[col] = value
continue
}
if (!data[row]) data[row] = {}
data[row][headers[col]] = value
}
// drop those first two rows which are empty
data.shift()
data.shift()
console.log(data)
const studentList = []
data.map(object => {
if (object['Year of Enrollment']) object['Year of Enrollment'] = parseInt(object['Year of Enrollment'])
if (object['Semester']) object['Semester'] = parseInt(object['Semester'])
if (object['Aadhar Number']) object['Aadhar Number'] = parseInt(object['Aadhar Number'])
if (object['Phone Number']) object['Phone Number'] = parseInt(object['Phone Number'])
if (object['Alternate Phone Number']) object['Alternate Phone Number'] = parseInt(object['Alternate Phone Number'])
let obj = {
"student_first_name": object['First Name'],
"student_last_name": object['Last Name'],
"student_branch": object['Branch'],
"student_year": object['Year of Enrollment'],
"student_semester": object['Semester'],
"student_gender": object['Gender'],
"student_email": object['Email'],
"student_address": object['Address'],
"student_linkedin_profile": object['Linkedin Profile'],
"student_github_profile": object['Github Profile'],
"student_aadhar_number": object['Aadhar Number'],
"student_phone_number": object['Phone Number'],
"student_date_of_birth": object['Date of Birth'],
college_id: req.body.college_id
}
if (object['Alternate Email']) obj.student_alternate_email = object['Alternate Email']
if (object['Alternate Phone Number']) obj.student_alternate_phone_number = object['Alternate Phone Number']
studentList.push(obj)
})
console.log(studentList)

关于javascript - 将 excel 转换为 json 数组时无法解析日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59809449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com