Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 67x 1x 3x 3x 3x 3x 2x 1x 1x 1x 1x 50x 1x 48x 48x 48x 116x 116x 1x 1x | import { IStudentSchedule } from '../TimeTable'
import xlsx from 'node-xlsx'
import { getAllDayBetween } from './time'
interface ParserResult {
scheduleData: IStudentSchedule[]
studentCode: string
studentName: string
}
const validateSheet = (workSheet: string[][]) => {
try {
return (
workSheet[0][0].toUpperCase() == 'BAN CƠ YẾU CHÍNH PHỦ' &&
workSheet[1][0].toUpperCase() == 'HỌC VIỆN KỸ THUẬT MẬT MÃ' &&
!!workSheet[5][5] &&
!!workSheet[5][2]
)
} catch (error) {
return false
}
}
const getStudentCode = (workSheet: string[][]) => {
return workSheet[5][5]
}
const getStudentName = (workSheet: string[][]) => {
return workSheet[5][2]
}
const filterData = (item: string[]) => {
return !!item[0] && (parseInt(item[0]) > 0 || item[0].toLowerCase() == 'thứ')
}
const fieldName = [
'thứ',
'mã học phần',
'tên học phần',
'lớp học phần',
'cbgd',
'tiết học',
'phòng học',
'thời gian học',
]
const parser = async (buffer: Buffer): Promise<ParserResult> => {
try {
const workSheet = xlsx.parse(buffer)[0].data as string[][]
if (!validateSheet(workSheet))
return Promise.reject(Error('Không phải thời khóa biểu học viện mật mã'))
const studentCode = getStudentCode(workSheet)
const studentName = getStudentName(workSheet)
const [header, ...dataToParse] = workSheet.filter(filterData)
const [
dateIndex,
subjectCodeIndex,
subjectNameIndex,
classNameIndex,
teacherIndex,
lessonIndex,
roomIndex,
timeIndex,
] = fieldName.map((field) => header.findIndex((e) => !!e && e.toLowerCase() === field))
const scheduleData: IStudentSchedule[] = []
for (const row of dataToParse) {
const [timeStart, timeEnd] = row[timeIndex].split('-')
const dates = getAllDayBetween(timeStart, timeEnd, parseInt(row[dateIndex]) || 1)
for (const date of dates) {
scheduleData.push({
date,
day: row[dateIndex],
subjectCode: row[subjectCodeIndex],
subjectName: row[subjectNameIndex],
className: row[classNameIndex],
teacher: row[teacherIndex],
lesson: row[lessonIndex] as IStudentSchedule['lesson'],
room: row[roomIndex],
})
}
}
return {
scheduleData,
studentCode,
studentName,
}
} catch (error: any) {
return Promise.reject(`Có lỗi xảy ra trong quá trình parse: ${error.message}`)
}
}
export default parser
|