Skip to content

XLSX file attachments #215

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
"ecmaVersion": 9
},
"env": {
"browser": true,
Expand Down
5 changes: 5 additions & 0 deletions src/fileAttachment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {require as requireDefault} from "d3-require";
import sqlite, {SQLiteDatabaseClient} from "./sqlite.js";
import xlsx, {XlsxWorkbook} from "./xlsx.js";

async function remote_fetch(file) {
const response = await fetch(await file.url());
Expand Down Expand Up @@ -62,6 +63,10 @@ class FileAttachment {
const db = new SQL.Database(new Uint8Array(buffer));
return new SQLiteDatabaseClient(db);
}
async xlsx(options) {
const [XLSX, buffer] = await Promise.all([xlsx(requireDefault), this.arrayBuffer()]);
return new XlsxWorkbook(await XLSX.read(buffer, {cellDates: true, ...options, type: "buffer"}));
}
}

export function NoFileAttachments(name) {
Expand Down
20 changes: 20 additions & 0 deletions src/xlsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let XLSX; // set lazily

export default async function xslx(require) {
return XLSX = await require("[email protected]/dist/xlsx.mini.min.js");
}

export class XlsxWorkbook {
constructor(workbook) {
Object.defineProperties(this, {
_: {value: workbook}
Copy link
Member

@visnup visnup May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to expose the XLSX object or XLSX.utils somewhere so people can easily use those functions like sheet_to_html or sheet_to_formulae. Or for modifying and writing back out to xlsx.

Copy link
Member Author

@mbostock mbostock May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I commented on why I didn’t do this: there are two versions of XLSX (mini and full), and we only want the mini one to parse XLSX files, but I think we’d want the full one (or both, somehow) in standard library.

I suppose I could hide it in the XlsxWorkbook class I made, but I figured it would be better to expose a minimal API that is easier for us to improve over time. (In my opinion, the design of SheetJs is not a little weird… I’m not sure why it favors global static functions over methods.)

I guess I could add both XLSX (mini) and XLS (full) to the standard library, but I think I’d prefer to wrap SheetJs rather than expose it directly so we can design a more elegant interface. That said once we have standard library versioning we can always change our minds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, let's start conservatively and see if we need to add more. there's also always the workaround of notebook-space requiring xlsx/sheetjs explicitly and using it if you need to.

});
}
sheetNames() {
return this._.SheetNames.slice();
}
sheet(name, options) {
if (!Object.prototype.hasOwnProperty.call(this._.Sheets, name)) throw new Error("unknown sheet");
return XLSX.utils.sheet_to_json(this._.Sheets[name], options);
}
}