-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(cli): support XLSX text extraction in read tool #10740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@kilocode/cli": patch | ||
| --- | ||
|
|
||
| Support reading XLSX spreadsheets as labelled tabular text |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,15 @@ | ||
| import type { Readable } from "stream" | ||
| import * as Docx from "./read-docx" | ||
| import * as Notebook from "./notebook" | ||
| import * as Xlsx from "./xlsx" | ||
|
|
||
| export function binary(filepath: string) { | ||
| return Docx.accepts(filepath) || Xlsx.is(filepath) | ||
| } | ||
|
|
||
| export async function open(filepath: string): Promise<Readable | undefined> { | ||
| if (Docx.accepts(filepath)) return Docx.open(filepath) | ||
| if (Xlsx.is(filepath)) return Xlsx.open(filepath) | ||
| if (Notebook.isFile(filepath)) return Notebook.open(filepath) | ||
| return undefined | ||
| } |
This file contains hidden or 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,78 @@ | ||
| import path from "path" | ||
| import { Readable } from "stream" | ||
| import { read, utils, type CellObject, type WorkBook } from "xlsx" | ||
|
|
||
| const ROW_LIMIT = 50_000 | ||
| const MAX_SIZE = 50 * 1024 * 1024 | ||
| const MAX_SIZE_LABEL = `${MAX_SIZE / (1024 * 1024)} MB` | ||
|
|
||
| export function is(filepath: string) { | ||
| return path.extname(filepath).toLowerCase() === ".xlsx" | ||
| } | ||
|
|
||
| export async function open(filepath: string) { | ||
| const file = Bun.file(filepath) | ||
| if (file.size > MAX_SIZE) { | ||
| throw new Error(`Cannot read spreadsheet file: ${filepath} exceeds the ${MAX_SIZE_LABEL} size limit`) | ||
| } | ||
| const bytes = new Uint8Array(await file.arrayBuffer()) | ||
| if (bytes[0] !== 0x50 || bytes[1] !== 0x4b) { | ||
| throw new Error(`Cannot read spreadsheet file: ${filepath} is not a valid XLSX workbook`) | ||
| } | ||
|
|
||
| try { | ||
| const book = read(bytes, { type: "array", cellDates: true }) | ||
| return Readable.from(lines(book)) | ||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err) | ||
| throw new Error(`Cannot read spreadsheet file: ${filepath}: ${message}`, { cause: err }) | ||
| } | ||
| } | ||
|
|
||
| function cell(value: CellObject | undefined) { | ||
| if (!value) return "" | ||
| if (value.f) { | ||
| if (value.w !== undefined && value.w !== null) return value.w | ||
| if (value.v !== undefined && value.v !== null) return String(value.v) | ||
| return `[Formula: ${value.f}]` | ||
| } | ||
| if (value.v === undefined || value.v === null) return "" | ||
| if (value.t === "e") return `[Error: ${value.w ?? String(value.v)}]` | ||
| if (value.t === "d") return value.v instanceof Date ? value.v.toISOString().slice(0, 10) : String(value.v) | ||
| if (value.l?.Target) return `${value.w ?? String(value.v)} (${value.l.Target})` | ||
| return value.w ?? String(value.v) | ||
| } | ||
|
|
||
| function* lines(book: WorkBook) { | ||
| const sheets = book.SheetNames.filter((_, index) => { | ||
| const hidden = book.Workbook?.Sheets?.[index]?.Hidden | ||
| return hidden !== 1 && hidden !== 2 | ||
| }) | ||
| for (const [index, name] of sheets.entries()) { | ||
| if (index > 0) yield "\n" | ||
| yield `--- Sheet: ${name} ---\n` | ||
|
|
||
| const sheet = book.Sheets[name] | ||
| if (!sheet?.["!ref"]) continue | ||
| const range = utils.decode_range(sheet["!ref"]) | ||
| const end = Math.min(range.e.r, ROW_LIMIT - 1) | ||
| const rows = new Map<number, Map<number, string>>() | ||
| for (const key of Object.keys(sheet)) { | ||
| if (key.startsWith("!")) continue | ||
| const pos = utils.decode_cell(key) | ||
| if (pos.r < range.s.r || pos.r > end || pos.c < range.s.c || pos.c > range.e.c) continue | ||
| const value = cell(sheet[key]) | ||
| if (!value.trim()) continue | ||
| const row = rows.get(pos.r) ?? new Map<number, string>() | ||
| row.set(pos.c, value) | ||
| rows.set(pos.r, row) | ||
| } | ||
|
|
||
| for (const values of [...rows.entries()].sort((a, b) => a[0] - b[0]).map((entry) => entry[1])) { | ||
| const last = Math.max(...values.keys()) | ||
| const row = Array.from({ length: last - range.s.c + 1 }, (_, col) => values.get(col + range.s.c) ?? "") | ||
| yield row.join("\t") + "\n" | ||
| } | ||
| if (range.e.r > end) yield `[... truncated at row ${ROW_LIMIT} ...]\n` | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.