-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.ts
40 lines (34 loc) · 1.21 KB
/
test_utils.ts
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
import ExcelJS from "jsr:@tinkie101/exceljs-wrapper@^1.0.2";
import { exists } from "@std/fs";
export async function createTestExcelFile(
filePath: string,
data: { headers: string[]; rows: (string | number | boolean)[][] },
): Promise<void> {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("Sheet1");
worksheet.addRow(data.headers);
data.rows.forEach((row) => worksheet.addRow(row));
await workbook.xlsx.writeFile(filePath);
}
export async function removeTestFile(filePath: string): Promise<void> {
if (await exists(filePath)) {
await Deno.remove(filePath);
}
}
/**
* Extracts rows from an ExcelJS worksheet and removes the first row and column.
*
* @param worksheet - The ExcelJS worksheet to extract rows from.
* @returns A 2D array with the first row and first column removed.
* @throws If the worksheet is undefined.
*/
export function getRows(
worksheet: ExcelJS.Worksheet | undefined,
): ExcelJS.CellValue[][] {
if (!worksheet) {
throw new Error("The worksheet is undefined.");
}
return worksheet.getSheetValues()
.slice(1) // Exclude the header row
.map((row) => (Array.isArray(row) ? row.slice(1) : [])); // Exclude the empty first column
}