refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
This commit is contained in:
@@ -12,12 +12,22 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
import ExcelJS from "exceljs"
|
||||
import ExcelJS, { Worksheet, Cell } from "exceljs"
|
||||
import fs from "fs"
|
||||
import parseAbsence from "../utils/parseAbsence.js"
|
||||
import parseAbsence, { AbsenceResult } from "../utils/parseAbsence.js"
|
||||
import parseTeachers from "../utils/parseTeachers.js"
|
||||
|
||||
export default async function parseV1V2(downloadedFilePath) {
|
||||
interface DatedSheet {
|
||||
sheet: Worksheet;
|
||||
dateKey: string;
|
||||
}
|
||||
|
||||
interface ScheduleDay {
|
||||
[key: string]: any;
|
||||
ABSENCE?: AbsenceResult[];
|
||||
}
|
||||
|
||||
export default async function parseV1V2(downloadedFilePath: string) {
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.readFile(downloadedFilePath);
|
||||
const teacherMap = await parseTeachers();
|
||||
@@ -32,7 +42,7 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
|
||||
const today = getCurrentDateObject();
|
||||
|
||||
const datedSheets = [];
|
||||
const datedSheets: DatedSheet[] = [];
|
||||
|
||||
for (const sheet of workbook.worksheets) {
|
||||
const match = sheet.name.toLowerCase().match(dateRegex);
|
||||
@@ -53,7 +63,7 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
});
|
||||
}
|
||||
|
||||
const sheetsByDate = {};
|
||||
const sheetsByDate: Record<string, Worksheet[]> = {};
|
||||
for (const item of datedSheets) {
|
||||
sheetsByDate[item.dateKey] ??= [];
|
||||
sheetsByDate[item.dateKey].push(item.sheet);
|
||||
@@ -61,20 +71,23 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
|
||||
const upcomingSheets = Object.values(sheetsByDate).map((sheets) => {
|
||||
if (sheets.length === 1) return sheets[0].name;
|
||||
return (sheets.find((s) => s.state !== "hidden") ?? sheets[0]).name;
|
||||
const found = sheets.find((s) => s.state !== "hidden");
|
||||
return (found ?? sheets[0]).name;
|
||||
});
|
||||
|
||||
const final = [];
|
||||
const final: ScheduleDay[] = [];
|
||||
|
||||
let finalIndex = 0
|
||||
for (const key of upcomingSheets) {
|
||||
const currentSheet = workbook.getWorksheet(key);
|
||||
if (!currentSheet) continue;
|
||||
|
||||
final.push({});
|
||||
|
||||
const regex = /[AEC][0-4][a-c]?\s*\/.*/s;
|
||||
const prefixRegex = /[AEC][0-4][a-c]?/;
|
||||
const classes = [];
|
||||
const matchingKeys = [];
|
||||
const classes: string[] = [];
|
||||
const matchingKeys: string[] = [];
|
||||
|
||||
currentSheet.eachRow((row) => {
|
||||
row.eachCell((cell) => {
|
||||
@@ -95,7 +108,7 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
})
|
||||
})
|
||||
|
||||
function letterToNumber(letter) {
|
||||
function letterToNumber(letter: string) {
|
||||
return letter.toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
|
||||
}
|
||||
|
||||
@@ -104,17 +117,20 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
for (const matchingKey of matchingKeys) {
|
||||
const matchingCell = currentSheet.getCell(matchingKey);
|
||||
const rowNumber = matchingCell.row;
|
||||
const allKeys = [];
|
||||
const allKeys: string[] = [];
|
||||
|
||||
// Get all cells in the same row
|
||||
const row = currentSheet.getRow(rowNumber);
|
||||
const row = currentSheet.getRow(Number(rowNumber));
|
||||
row.eachCell((cell) => {
|
||||
if (cell.address !== matchingKey) {
|
||||
allKeys.push(cell.address);
|
||||
}
|
||||
})
|
||||
|
||||
let final2 = [];
|
||||
// Use an array directly, initialized with nulls or sparse array logic
|
||||
// The original code used `let final2 = []` but treated it as object `final2[parsedKey] = ...`
|
||||
// Then `Array.from(final2)` converts it to array.
|
||||
let final2: (string | null)[] = [];
|
||||
|
||||
for (const key of allKeys) {
|
||||
const cell = currentSheet.getCell(key);
|
||||
@@ -124,13 +140,16 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
try {
|
||||
const regex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
|
||||
const cellText = cell.text || "";
|
||||
if (regex.test(cellText.trim()) || cellText.trim().length == 0 || cell.fill?.fgColor === undefined) {
|
||||
// @ts-ignore - fgColor is missing in type definition for some versions or intricate structure
|
||||
const fgColor = cell.fill?.fgColor;
|
||||
if (regex.test(cellText.trim()) || cellText.trim().length == 0 || fgColor === undefined) {
|
||||
d = false;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
if (d) {
|
||||
let text = cell.text;
|
||||
// @ts-ignore
|
||||
if (cell.fill?.fgColor?.argb == "FFFFFF00") {
|
||||
text += "\n(bude upřesněno)";
|
||||
}
|
||||
@@ -140,19 +159,19 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
}
|
||||
}
|
||||
|
||||
final2 = Array.from(final2, (item) => (item === undefined ? null : item));
|
||||
while (final2.length < 10) {
|
||||
final2.push(null);
|
||||
const final2Array = Array.from(final2, (item) => (item === undefined ? null : item));
|
||||
while (final2Array.length < 10) {
|
||||
final2Array.push(null);
|
||||
}
|
||||
|
||||
final[finalIndex][classes[classI]] = final2.slice(1, 11);
|
||||
final[finalIndex][classes[classI]] = final2Array.slice(1, 11);
|
||||
|
||||
classI++;
|
||||
}
|
||||
|
||||
// ABSENCE
|
||||
final[finalIndex]["ABSENCE"] = [];
|
||||
let absenceKey = null;
|
||||
let absenceKey: string | null = null;
|
||||
|
||||
currentSheet.eachRow((row) => {
|
||||
row.eachCell((cell) => {
|
||||
@@ -166,13 +185,13 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
if (absenceKey) {
|
||||
const absenceCell = currentSheet.getCell(absenceKey);
|
||||
const rowNumber = absenceCell.row;
|
||||
const allAbsenceKeys = [];
|
||||
const allAbsenceKeys: string[] = [];
|
||||
|
||||
// Get all cells in the same row as absence
|
||||
const row = currentSheet.getRow(rowNumber);
|
||||
const row = currentSheet.getRow(Number(rowNumber));
|
||||
row.eachCell((cell) => {
|
||||
if (cell.address !== absenceKey) {
|
||||
allAbsenceKeys.push(cell.address);
|
||||
if (cell.address !== absenceKey) { // absenceKey is checked above to be non-null
|
||||
allAbsenceKeys.push(cell.address);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -189,7 +208,9 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
}
|
||||
|
||||
const data = parseAbsence(value, teacherMap);
|
||||
final[finalIndex]["ABSENCE"].push(...data);
|
||||
if (final[finalIndex]["ABSENCE"]) {
|
||||
final[finalIndex]["ABSENCE"]!.push(...data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,10 +258,10 @@ export default async function parseV1V2(downloadedFilePath) {
|
||||
// Modify the data for v1
|
||||
const copy = JSON.parse(JSON.stringify(data));
|
||||
|
||||
copy.schedule.forEach(day => {
|
||||
copy.schedule.forEach((day: ScheduleDay) => {
|
||||
if (!Array.isArray(day.ABSENCE)) return;
|
||||
|
||||
day.ABSENCE = day.ABSENCE.map(old => {
|
||||
day.ABSENCE = day.ABSENCE.map((old: any) => {
|
||||
if (old.type === "zastoupen") {
|
||||
return {
|
||||
type: "invalid",
|
||||
@@ -15,14 +15,30 @@
|
||||
import fs from "fs";
|
||||
import parseAbsence from "../utils/parseAbsence.js"
|
||||
import parseTeachers from "../utils/parseTeachers.js"
|
||||
import ExcelJS from "exceljs"
|
||||
import ExcelJS, { Worksheet, Cell, Row } from "exceljs"
|
||||
import JSZip from "jszip";
|
||||
import { parseStringPromise } from "xml2js";
|
||||
|
||||
interface ThemeColors {
|
||||
[key: number]: string | null;
|
||||
}
|
||||
|
||||
interface Lesson {
|
||||
text: string;
|
||||
backgroundColor: string | null;
|
||||
foregroundColor?: string;
|
||||
willBeSpecified?: boolean;
|
||||
}
|
||||
|
||||
interface ResolvedDay {
|
||||
dateKey: string;
|
||||
sheet: Worksheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read theme colors from the workbook
|
||||
*/
|
||||
async function getThemeColors(filePath) {
|
||||
async function getThemeColors(filePath: string): Promise<ThemeColors | null> {
|
||||
const data = fs.readFileSync(filePath);
|
||||
const zip = await JSZip.loadAsync(data);
|
||||
|
||||
@@ -39,13 +55,13 @@ async function getThemeColors(filePath) {
|
||||
|
||||
if (!scheme) return null;
|
||||
|
||||
function getColor(node) {
|
||||
function getColor(node: any) {
|
||||
if (node["a:srgbClr"]) return node["a:srgbClr"][0].$.val;
|
||||
if (node["a:sysClr"]) return node["a:sysClr"][0].$.lastClr;
|
||||
return null;
|
||||
}
|
||||
|
||||
const colors = {
|
||||
const colors: ThemeColors = {
|
||||
0: getColor(scheme["a:dk1"]?.[0]),
|
||||
1: getColor(scheme["a:lt1"]?.[0]),
|
||||
2: getColor(scheme["a:dk2"]?.[0]),
|
||||
@@ -64,12 +80,12 @@ async function getThemeColors(filePath) {
|
||||
/**
|
||||
* Apply Excel tint to a base hex color
|
||||
*/
|
||||
function applyTintToHex(hex, tint = 0) {
|
||||
function applyTintToHex(hex: string, tint: number = 0) {
|
||||
const r = parseInt(hex.slice(0, 2), 16);
|
||||
const g = parseInt(hex.slice(2, 4), 16);
|
||||
const b = parseInt(hex.slice(4, 6), 16);
|
||||
|
||||
const tintChannel = (c) =>
|
||||
const tintChannel = (c: number) =>
|
||||
tint > 0 ? Math.round(c + (255 - c) * tint) : Math.round(c * (1 + tint));
|
||||
|
||||
const nr = tintChannel(r);
|
||||
@@ -85,9 +101,11 @@ function applyTintToHex(hex, tint = 0) {
|
||||
/**
|
||||
* Resolve final hex for a cell fill
|
||||
*/
|
||||
function resolveCellColor(cell, themeColors) {
|
||||
function resolveCellColor(cell: Cell, themeColors: ThemeColors | null) {
|
||||
// @ts-ignore
|
||||
if (!cell.fill?.fgColor) return null;
|
||||
|
||||
// @ts-ignore
|
||||
const fg = cell.fill.fgColor;
|
||||
|
||||
if (fg.argb) return `#${fg.argb}`;
|
||||
@@ -100,7 +118,7 @@ function resolveCellColor(cell, themeColors) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export default async function parseV3(downloadedFilePath) {
|
||||
export default async function parseV3(downloadedFilePath: string) {
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.readFile(downloadedFilePath);
|
||||
const themeColors = await getThemeColors(downloadedFilePath);
|
||||
@@ -110,7 +128,7 @@ export default async function parseV3(downloadedFilePath) {
|
||||
const upcoming = getUpcomingSheets(workbook);
|
||||
const resolvedDays = groupSheetsByDate(upcoming);
|
||||
|
||||
const schedule = {};
|
||||
const schedule: any = {};
|
||||
|
||||
for (const { dateKey, sheet } of resolvedDays) {
|
||||
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap, themeColors);
|
||||
@@ -138,13 +156,13 @@ export default async function parseV3(downloadedFilePath) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function getUpcomingSheets(workbook) {
|
||||
function getUpcomingSheets(workbook: ExcelJS.Workbook): ResolvedDay[] {
|
||||
const dateRegex = /^(pondělí|úterý|středa|čtvrtek|pátek|po|út|ut|st|čt|ct|pa|pá)\s+(\d{1,2})\.\s*(\d{1,2})\.\s*(\d{4}|\d{2})/i;
|
||||
|
||||
const today = new Date();
|
||||
const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate());
|
||||
|
||||
const result = [];
|
||||
const result: ResolvedDay[] = [];
|
||||
|
||||
for (const sheet of workbook.worksheets) {
|
||||
const match = sheet.name.toLowerCase().match(dateRegex);
|
||||
@@ -164,8 +182,8 @@ function getUpcomingSheets(workbook) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function groupSheetsByDate(items) {
|
||||
const map = {};
|
||||
function groupSheetsByDate(items: ResolvedDay[]) {
|
||||
const map: Record<string, Worksheet[]> = {};
|
||||
|
||||
for (const item of items) {
|
||||
map[item.dateKey] ??= [];
|
||||
@@ -188,7 +206,7 @@ function groupSheetsByDate(items) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function extractDaySchedule(sheet, teacherMap, themeColors) {
|
||||
function extractDaySchedule(sheet: Worksheet, teacherMap: Record<string, string>, themeColors: ThemeColors | null) {
|
||||
return {
|
||||
changes: extractClassChanges(sheet, themeColors),
|
||||
absence: extractAbsence(sheet, teacherMap),
|
||||
@@ -198,7 +216,7 @@ function extractDaySchedule(sheet, teacherMap, themeColors) {
|
||||
};
|
||||
}
|
||||
|
||||
function isPripravaSheet(name) {
|
||||
function isPripravaSheet(name: string) {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.normalize("NFD")
|
||||
@@ -212,12 +230,12 @@ function isPripravaSheet(name) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function extractClassChanges(sheet, themeColors) {
|
||||
function extractClassChanges(sheet: Worksheet, themeColors: ThemeColors | null) {
|
||||
const classRegex = /[AEC][0-4][a-c]?\s*\/.*/s;
|
||||
const prefixRegex = /[AEC][0-4][a-c]?/;
|
||||
|
||||
const classes = [];
|
||||
const classCells = [];
|
||||
const classes: string[] = [];
|
||||
const classCells: string[] = [];
|
||||
|
||||
sheet.eachRow((row) => {
|
||||
row.eachCell((cell) => {
|
||||
@@ -230,18 +248,18 @@ function extractClassChanges(sheet, themeColors) {
|
||||
});
|
||||
});
|
||||
|
||||
const changes = {};
|
||||
const changes: Record<string, (Lesson | null)[]> = {};
|
||||
|
||||
classCells.forEach((address, index) => {
|
||||
const row = sheet.getRow(sheet.getCell(address).row);
|
||||
const row = sheet.getRow(Number(sheet.getCell(address).row));
|
||||
changes[classes[index]] = buildLessonArray(row, address, themeColors);
|
||||
});
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
function buildLessonArray(row, ignoreAddress, themeColors) {
|
||||
const lessons = [];
|
||||
function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeColors | null) {
|
||||
const lessons: (Lesson | null)[] = [];
|
||||
|
||||
row.eachCell((cell) => {
|
||||
if (cell.address === ignoreAddress) return;
|
||||
@@ -256,11 +274,12 @@ function buildLessonArray(row, ignoreAddress, themeColors) {
|
||||
return normalized.slice(1, 11);
|
||||
}
|
||||
|
||||
function parseLessonCell(cell, themeColors) {
|
||||
function parseLessonCell(cell: Cell, themeColors: ThemeColors | null): Lesson | null {
|
||||
try {
|
||||
const text = (cell.text || "").trim();
|
||||
const cleanupRegex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
|
||||
|
||||
// @ts-ignore
|
||||
if (!text || cleanupRegex.test(text) || !cell.fill?.fgColor) return null;
|
||||
|
||||
const backgroundColor = resolveCellColor(cell, themeColors);
|
||||
@@ -272,6 +291,7 @@ function parseLessonCell(cell, themeColors) {
|
||||
text,
|
||||
backgroundColor,
|
||||
foregroundColor,
|
||||
// @ts-ignore
|
||||
willBeSpecified: cell.fill.fgColor.argb === "FFFFFF00" ? true : undefined,
|
||||
};
|
||||
} catch {
|
||||
@@ -280,7 +300,7 @@ function parseLessonCell(cell, themeColors) {
|
||||
}
|
||||
|
||||
|
||||
function extractTakesPlace(sheet) {
|
||||
function extractTakesPlace(sheet: Worksheet) {
|
||||
const cell = sheet.getCell("B4");
|
||||
|
||||
if (!cell.isMerged) {
|
||||
@@ -296,7 +316,7 @@ function extractTakesPlace(sheet) {
|
||||
|
||||
for (const cellTest of tryCells) {
|
||||
const cellTry = sheet.getCell(`${cellTest}${i}`)
|
||||
const cellValue = cellTry?.value?.trim() || "";
|
||||
const cellValue = (typeof cellTry?.value === 'string' ? cellTry.value.trim() : "") || "";
|
||||
|
||||
if (cellValue.length >= threshold) {
|
||||
str += `\n${cellValue}`;
|
||||
@@ -316,10 +336,10 @@ function extractTakesPlace(sheet) {
|
||||
return str.trim();
|
||||
}
|
||||
|
||||
function extractReservedRooms(sheet) {
|
||||
const result = [];
|
||||
function extractReservedRooms(sheet: Worksheet) {
|
||||
const result: (string | null)[] = [];
|
||||
|
||||
const cells = [];
|
||||
const cells: string[] = [];
|
||||
|
||||
sheet.eachRow((row) => {
|
||||
row.eachCell((cell) => {
|
||||
@@ -331,12 +351,13 @@ function extractReservedRooms(sheet) {
|
||||
});
|
||||
|
||||
cells.forEach((address) => {
|
||||
const row = sheet.getRow(sheet.getCell(address).row);
|
||||
const row = sheet.getRow(Number(sheet.getCell(address).row));
|
||||
|
||||
row.eachCell((cell) => {
|
||||
if (cell.address === address) return;
|
||||
|
||||
result.push(cell.value?.trim().length == 0 ? null : cell.value)
|
||||
const val = cell.value?.toString().trim();
|
||||
result.push(!val || val.length == 0 ? null : val)
|
||||
});
|
||||
});
|
||||
|
||||
@@ -353,8 +374,8 @@ function extractReservedRooms(sheet) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function extractAbsence(sheet, teacherMap) {
|
||||
let absenceAddress = null;
|
||||
function extractAbsence(sheet: Worksheet, teacherMap: Record<string, string>) {
|
||||
let absenceAddress: string | null = null;
|
||||
|
||||
sheet.eachRow((row) => {
|
||||
row.eachCell((cell) => {
|
||||
@@ -366,8 +387,8 @@ function extractAbsence(sheet, teacherMap) {
|
||||
|
||||
if (!absenceAddress) return [];
|
||||
|
||||
const row = sheet.getRow(sheet.getCell(absenceAddress).row);
|
||||
const results = [];
|
||||
const row = sheet.getRow(Number(sheet.getCell(absenceAddress).row));
|
||||
const results: any[] = [];
|
||||
const absenceRange = new Set(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L"])
|
||||
|
||||
row.eachCell((cell) => {
|
||||
@@ -390,7 +411,7 @@ function extractAbsence(sheet, teacherMap) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function letterToNumber(letter) {
|
||||
function letterToNumber(letter: string) {
|
||||
return letter.toLowerCase().charCodeAt(0) - 97;
|
||||
}
|
||||
|
||||
@@ -403,4 +424,4 @@ function formatNowTime() {
|
||||
);
|
||||
}
|
||||
|
||||
//parseV3("db/current.xlsx")
|
||||
parseV3("db/current.xlsx")
|
||||
Reference in New Issue
Block a user