-
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: add read and write xl functions
- Loading branch information
Showing
6 changed files
with
150 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.xlsx |
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 |
---|---|---|
@@ -1 +1 @@ | ||
# pl2xl | ||
# pl2xl |
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,12 @@ | ||
{ | ||
"name": "@jackfiszr/pl2xl", | ||
"version": "0.0.1", | ||
"exports": "./mod.ts", | ||
"tasks": { | ||
"dev": "deno run --watch main.ts" | ||
}, | ||
"imports": { | ||
"polars": "npm:nodejs-polars@0.16.0", | ||
"xlsx": "npm:xlsx@0.18.5" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pl from "polars"; | ||
import xlsx from "xlsx"; | ||
|
||
function readExcel(filePath: string): pl.DataFrame { | ||
const workbook = xlsx.readFile(filePath); | ||
const sheet = workbook.Sheets[workbook.SheetNames[0]]; | ||
const jsonData = xlsx.utils.sheet_to_json(sheet); | ||
return pl.DataFrame(jsonData); | ||
} | ||
|
||
function writeExcel(df: pl.DataFrame, filePath: string): void { | ||
const rows = df.toRecords(); | ||
const newWorkbook = xlsx.utils.book_new(); | ||
const newSheet = xlsx.utils.json_to_sheet(rows); | ||
xlsx.utils.book_append_sheet(newWorkbook, newSheet, "Sheet1"); | ||
xlsx.writeFile(newWorkbook, filePath); | ||
} | ||
|
||
export { readExcel, writeExcel }; |
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,22 @@ | ||
import { readExcel, writeExcel } from "./mod.ts"; | ||
import pl from "polars"; | ||
|
||
const inputDf = pl.DataFrame({ | ||
"Name": ["Alice", "Bob", "Charlie"], | ||
"Age": [25, 30, 35], | ||
"City": ["New York", "Los Angeles", "Chicago"], | ||
}); | ||
|
||
writeExcel(inputDf, "input.xlsx"); | ||
|
||
const df = readExcel("input.xlsx"); | ||
console.log("Read DataFrame:", df); | ||
|
||
const modifiedDf = df.withColumn( | ||
pl.col("Age").add(1).alias("Age"), | ||
); | ||
|
||
console.log("Modified DataFrame:", modifiedDf); | ||
|
||
writeExcel(modifiedDf, "output.xlsx"); | ||
console.log("Modified DataFrame written to output.xlsx"); |