deno.land / x / lume@v2.1.4 / plugins / sheets.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { ParsingOptions, read, utils } from "../deps/sheetjs.ts";import { merge } from "../core/utils/object.ts";import loadFile from "../core/loaders/binary.ts";
import type Site from "../core/site.ts";import type { RawData } from "../core/file.ts";
export interface Options { /** Extensions processed by this plugin */ extensions?: string[];
/** Return the first sheet only or all sheets if the document have more */ sheets?: "first" | "auto";
/** Options passed to Sheetjs */ options?: ParsingOptions;}
export const defaults: Options = { extensions: [".xlsx", ".numbers", ".csv"], sheets: "auto", options: {},};
/** Register the plugin to add support for sheets documents to load data */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
async function loader(path: string): Promise<RawData> { const { content } = await loadFile(path); const wb = read(content, options.options);
// Return only the first sheet if (options.sheets === "first" || wb.SheetNames.length === 1) { const sheet = wb.Sheets[wb.SheetNames[0]]; return utils.sheet_to_json(sheet) as unknown as RawData; }
// Return all sheets by name const sheets: Record<string, unknown[]> = {}; wb.SheetNames.forEach((name) => { sheets[name] = utils.sheet_to_json(wb.Sheets[name]); });
return { content: sheets }; }
return (site: Site) => { site.loadData(options.extensions, loader);
// Ignore temp files generated by Numbers (https://github.com/lumeland/lume/issues/245) if (options.extensions.includes(".numbers")) { site.options.watcher.ignore.push((path) => !!path.match(/\.numbers\.[\w-]+$/) ); } };}
lume

Version Info

Tagged at
6 months ago