-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
readExcel
function with sheetName
param
- Loading branch information
Showing
7 changed files
with
165 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pl from "polars"; | ||
import ExcelJS from "@tinkie101/exceljs-wrapper"; | ||
import type { RowData } from "./types.ts"; | ||
|
||
export async function readExcel( | ||
filePath: string, | ||
sheetName?: string, | ||
): Promise<pl.DataFrame> { | ||
const workbook = new ExcelJS.Workbook(); | ||
await workbook.xlsx.readFile(filePath); | ||
|
||
const worksheet = sheetName | ||
? workbook.getWorksheet(sheetName) | ||
: workbook.worksheets[0]; | ||
|
||
if (!worksheet) { | ||
throw new Error( | ||
`Worksheet ${sheetName || "Sheet1"} not found in the Excel file.`, | ||
); | ||
} | ||
|
||
const jsonData: RowData[] = []; | ||
const headers: string[] = []; | ||
|
||
worksheet.eachRow((row, rowNumber) => { | ||
const rowData: RowData = {}; | ||
|
||
row.eachCell((cell, colNumber) => { | ||
const cellValue = cell.value as string | number | boolean | null; | ||
|
||
if (rowNumber === 1) { | ||
headers[colNumber - 1] = cellValue?.toString() || `Column${colNumber}`; | ||
} else { | ||
rowData[headers[colNumber - 1]] = cellValue; | ||
} | ||
}); | ||
|
||
if (rowNumber > 1) { | ||
jsonData.push(rowData); | ||
} | ||
}); | ||
|
||
return pl.DataFrame(jsonData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { assertEquals, assertRejects } from "@std/assert"; | ||
import { readExcel } from "./read_excel.ts"; | ||
import { createTestExcelFile, removeTestFile } from "./test_utils.ts"; | ||
import ExcelJS from "@tinkie101/exceljs-wrapper"; | ||
|
||
Deno.test({ | ||
name: "readExcel - Reads data from a valid Excel file", | ||
async fn() { | ||
const filePath = "./test-read-valid.xlsx"; | ||
const testData = { | ||
headers: ["Name", "Age", "Country"], | ||
rows: [ | ||
["Alice", 30, "USA"], | ||
["Bob", 25, "Canada"], | ||
], | ||
}; | ||
await createTestExcelFile(filePath, testData); | ||
|
||
const df = await readExcel(filePath); | ||
|
||
const expected = [ | ||
{ Name: "Alice", Age: 30, Country: "USA" }, | ||
{ Name: "Bob", Age: 25, Country: "Canada" }, | ||
]; | ||
assertEquals(df.toRecords(), expected); | ||
|
||
await removeTestFile(filePath); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "readExcel - Throws error for missing sheet", | ||
async fn() { | ||
const filePath = "./test-read-missing-sheet.xlsx"; | ||
const testData = { | ||
headers: ["Name", "Age"], | ||
rows: [["Alice", 30]], | ||
}; | ||
await createTestExcelFile(filePath, testData); | ||
|
||
await assertRejects( | ||
async () => { | ||
await readExcel(filePath, "NonExistentSheet"); | ||
}, | ||
Error, | ||
"Worksheet NonExistentSheet not found in the Excel file.", | ||
); | ||
await removeTestFile(filePath); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "readExcel - Handles empty Excel file", | ||
async fn() { | ||
const filePath = "./test-empty.xlsx"; | ||
const workbook = new ExcelJS.Workbook(); | ||
workbook.addWorksheet("Sheet1"); | ||
await workbook.xlsx.writeFile(filePath); | ||
|
||
const df = await readExcel(filePath); | ||
|
||
assertEquals(df.shape, { height: 0, width: 0 }); | ||
|
||
await removeTestFile(filePath); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type RowData = Record< | ||
string, | ||
string | number | boolean | null | undefined | ||
>; |