Skip to content

Commit

Permalink
docs: add readme and jsdoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfiszr committed Oct 26, 2024
1 parent 4a7b442 commit c07473d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# .github/workflows/publish.yml

name: Publish

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
# pl2xl

A lightweight library for reading and writing Excel files as Polars DataFrames.
`pl2xl` enables seamless integration between Polars and Excel, allowing you to
import data from Excel files directly into a Polars DataFrame and export
DataFrames back to Excel.

## Installation

This library can be imported using the `jsr` import specifier and relies on the
`nodejs-polars` package.

### Importing the library in Deno

```typescript
import { readExcel, writeExcel } from "jsr:@jackfiszr/pl2xl@0.0.1";
import pl from "npm:nodejs-polars";
```

### Using the library in Node.js

Install the library in your Node project using `npx jsr`:

```bash
npx jsr add @jackfiszr/pl2xl
```

Then import and use it as follows:

```typescript
import { readExcel, writeExcel } from "@jackfiszr/pl2xl";
import pl from "nodejs-polars";

// Create a sample DataFrame
const inputDf = pl.DataFrame({
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "Los Angeles", "Chicago"],
});

// Write the DataFrame to an Excel file
writeExcel(inputDf, "input.xlsx");

// Read the DataFrame back from the Excel file
const df = readExcel("input.xlsx");
console.log("Read DataFrame:", df);

// Modify the DataFrame by increasing the "Age" column by 1
const modifiedDf = df.withColumn(
pl.col("Age").add(1).alias("Age"),
);

console.log("Modified DataFrame:", modifiedDf);

// Write the modified DataFrame to a new Excel file
writeExcel(modifiedDf, "output.xlsx");
console.log("Modified DataFrame written to output.xlsx");
```

## API

### `readExcel(filePath: string): pl.DataFrame`

Reads data from the first sheet of an Excel file and returns it as a Polars
DataFrame.

- `filePath`: The path to the Excel file to be read.

**Returns**: A `pl.DataFrame` containing the data from the Excel file.

### `writeExcel(df: pl.DataFrame, filePath: string): void`

Writes a Polars DataFrame to an Excel file.

- `df`: The Polars DataFrame to write to the file.
- `filePath`: The path to save the Excel file.

## Requirements

- Deno (for Deno usage) or Node.js (for Node usage)
- `nodejs-polars` and `xlsx` packages

## License

MIT License
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jackfiszr/pl2xl",
"version": "0.0.1",
"version": "0.0.2",
"exports": "./mod.ts",
"tasks": {
"dev": "deno run --watch main.ts"
Expand Down
16 changes: 16 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import pl from "polars";
import xlsx from "xlsx";

/**
* Reads an Excel file and returns its content as a Polars DataFrame.
* This function takes the first sheet of the workbook and converts it to JSON
* before loading it into a DataFrame.
*
* @param filePath - The path to the Excel file to be read.
* @returns A Polars DataFrame containing the data from the first sheet of the Excel file.
*/
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);
}

/**
* Writes a Polars DataFrame to an Excel file.
* This function converts the DataFrame to JSON records and writes them to the
* specified file, with the data stored in a sheet named "Sheet1".
*
* @param df - The Polars DataFrame to write to an Excel file.
* @param filePath - The path to save the Excel file.
*/
function writeExcel(df: pl.DataFrame, filePath: string): void {
const rows = df.toRecords();
const newWorkbook = xlsx.utils.book_new();
Expand Down

0 comments on commit c07473d

Please sign in to comment.