-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (72 loc) · 2.49 KB
/
index.js
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
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable security/detect-object-injection */
import fs from 'node:fs';
import XLSX from 'xlsx';
import { formatDate, isDateColumn } from './utilities.js';
XLSX.set_fs(fs);
const timeDataSheetName = 'Time Data';
const approvedTimeDataSheetName = 'Approved Time Data';
function parseTimeDataSheet(worksheet) {
const rows = XLSX.utils.sheet_to_json(worksheet, {
raw: false,
rawNumbers: true
});
const timeData = {};
let dateMin = '';
let dateMax = '';
for (const row of rows) {
if (row.EmpNo === undefined || row.Code === undefined) {
continue;
}
if (!Object.hasOwn(timeData, row.EmpNo)) {
timeData[row.EmpNo] = {
employeeNumber: row.EmpNo,
employeeName: row.Name,
payCodes: {}
};
}
const payCode = {
payCode: row.Code,
payCodeDescription: row.Description,
hours: {}
};
for (const [columnName, columnValue] of Object.entries(row)) {
if (!isDateColumn(columnName)) {
continue;
}
const dateString = formatDate(columnName);
const hours = Number.parseFloat(columnValue);
payCode.hours[dateString] = hours;
if (dateMin === '' || dateMin > dateString) {
dateMin = dateString;
}
if (dateMax === '' || dateMax < dateString) {
dateMax = dateString;
}
}
timeData[row.EmpNo].payCodes[row.Code] = payCode;
}
return {
timeData,
dateMin: dateMin === '' ? undefined : dateMin,
dateMax: dateMax === '' ? undefined : dateMax
};
}
/**
* Parses a time data Excel file from Avanti into a JavaScript object.
* @param {string} xlsxFilePath - The path to the time data Excel file.
* @returns {TimeData} Parsed time data
*/
export function parseTimeData(xlsxFilePath) {
const workbook = XLSX.readFile(xlsxFilePath, {});
const timeDataObject = parseTimeDataSheet(workbook.Sheets[timeDataSheetName]);
const approvedTimeDataObject = parseTimeDataSheet(workbook.Sheets[approvedTimeDataSheetName]);
const timeData = {
dateMin: timeDataObject.dateMin,
dateMax: timeDataObject.dateMax,
timeData: timeDataObject.timeData,
approvedTimeData: approvedTimeDataObject.timeData
};
return timeData;
}
export * as types from './types.js';