mirror of
https://github.com/canna71/obsidian-sheets.git
synced 2026-07-22 08:30:27 +00:00
lading and saving from vault
This commit is contained in:
parent
54fd3c1b69
commit
afcd7f8d7e
3 changed files with 42 additions and 10 deletions
BIN
Book1.xlsx
Normal file
BIN
Book1.xlsx
Normal file
Binary file not shown.
BIN
SampleData.xlsx
BIN
SampleData.xlsx
Binary file not shown.
|
|
@ -1,13 +1,34 @@
|
|||
// import * as React from "react";
|
||||
// import { createRoot } from "react-dom/client";
|
||||
import { MarkdownPostProcessorContext } from "obsidian";
|
||||
import { MarkdownPostProcessorContext, debounce } from "obsidian";
|
||||
import { SheetjsSettings } from "src/Settings";
|
||||
import Spreadsheet from "x-data-spreadsheet";
|
||||
// import "x-data-spreadsheet/dist/xspreadsheet.css";
|
||||
import * as fs from "fs/promises"
|
||||
import * as path from "path"
|
||||
|
||||
import * as XLSX from "xlsx"
|
||||
import { stox, xtos } from "../utils/xlsxpread"
|
||||
|
||||
function resolve_book_type(fileName: string):XLSX.BookType {
|
||||
const _BT:any = {
|
||||
"xls": "biff8",
|
||||
"htm": "html",
|
||||
"slk": "sylk",
|
||||
"socialcalc": "eth",
|
||||
"Sh33tJS": "WTF"
|
||||
};
|
||||
let bookType = "xlsx";
|
||||
const ext = fileName.slice(fileName.lastIndexOf(".")).toLowerCase();
|
||||
if(ext.match(/^\.[a-z]+$/)) {
|
||||
bookType = ext.slice(1)
|
||||
}
|
||||
bookType = _BT[bookType] || bookType;
|
||||
return bookType as XLSX.BookType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function processCodeBlock(source: string, el: HTMLElement, settings: SheetjsSettings, ctx: MarkdownPostProcessorContext) {
|
||||
|
||||
|
||||
|
|
@ -29,6 +50,10 @@ export function processCodeBlock(source: string, el: HTMLElement, settings: Shee
|
|||
|
||||
// data validation
|
||||
|
||||
// const filename = `/Users/gcannata/Documents/Obsidian Vault/Dev Vault/Dev/.obsidian/plugins/obsidian-sheetjs/SampleData.xlsx`;
|
||||
|
||||
const filename = `/stuff/Book1.xlsx`;
|
||||
|
||||
const container = el.createDiv()
|
||||
// container.style.width = "100%";
|
||||
// container.style.height = "800px";
|
||||
|
|
@ -73,23 +98,30 @@ export function processCodeBlock(source: string, el: HTMLElement, settings: Shee
|
|||
.loadData({
|
||||
|
||||
})
|
||||
.change(data => {
|
||||
.change(debounce(data => {
|
||||
// save data
|
||||
console.log(data)
|
||||
XLSX.writeFile(xtos(data) as any, `C:\\Users\\hh7gabcannat\\Projects\\Personal\\obsidian-dev\\DEV\\.obsidian\\plugins\\obsidian-sheetjs\\SampleData.xlsx`);
|
||||
});
|
||||
const wb = xtos(s.getData() as any[]) as XLSX.WorkBook;
|
||||
const bookType = resolve_book_type(filename);
|
||||
const bytes = XLSX.write(wb,{
|
||||
bookType: bookType,
|
||||
type: "buffer"
|
||||
});
|
||||
// fs.writeFile(filename,bytes);
|
||||
app.vault.adapter.writeBinary(filename,bytes)
|
||||
// XLSX.writeFile(xtos(s.getData(data)) as any, filename);
|
||||
},1000));
|
||||
|
||||
(async () => {
|
||||
// const ab = await (await fetch("https://sheetjs.com/pres.numbers")).arrayBuffer();
|
||||
//ctx.sourcePath
|
||||
// TODO: take relative path
|
||||
const data = await fs.readFile(`C:\\Users\\hh7gabcannat\\Projects\\Personal\\obsidian-dev\\DEV\\.obsidian\\plugins\\obsidian-sheetjs\\SampleData.xlsx`)
|
||||
|
||||
const data = await app.vault.adapter.readBinary(filename)
|
||||
s.loadData(stox(XLSX.read(data)));
|
||||
})();
|
||||
|
||||
// see https://docs.sheetjs.com/docs/demos/grid/xs
|
||||
// https://docs.sheetjs.com/xspreadsheet/
|
||||
// https://github.com/myliang/x-spreadsheet
|
||||
|
||||
console.log(`spreadsheet`, s)
|
||||
// TODO: support other formats, not only binary
|
||||
// TODO: save into code block?
|
||||
// TODO: support $A$2
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue