UNPKG

20.2 kBTypeScriptView Raw
1/* index.d.ts (C) 2015-present SheetJS and contributors */
2// TypeScript Version: 2.2
3import * as CFB from "cfb";
4import * as SSF from "ssf";
5
6/** Version string */
7export const version: string;
8
9/** SSF Formatter Library */
10export { SSF };
11
12/** CFB Library */
13export { CFB };
14
15/** NODE ONLY! Attempts to read filename and parse */
16export function readFile(filename: string, opts?: ParsingOptions): WorkBook;
17/** Attempts to parse data */
18export function read(data: any, opts?: ParsingOptions): WorkBook;
19/** Attempts to write or download workbook data to file */
20export function writeFile(data: WorkBook, filename: string, opts?: WritingOptions): any;
21/** Attempts to write the workbook data */
22export function write(data: WorkBook, opts?: WritingOptions): any;
23
24/** Utility Functions */
25export const utils: XLSX$Utils;
26/** Stream Utility Functions */
27export const stream: StreamUtils;
28
29/** Number Format (either a string or an index to the format table) */
30export type NumberFormat = string | number;
31
32/** Basic File Properties */
33export interface Properties {
34 /** Summary tab "Title" */
35 Title?: string;
36 /** Summary tab "Subject" */
37 Subject?: string;
38 /** Summary tab "Author" */
39 Author?: string;
40 /** Summary tab "Manager" */
41 Manager?: string;
42 /** Summary tab "Company" */
43 Company?: string;
44 /** Summary tab "Category" */
45 Category?: string;
46 /** Summary tab "Keywords" */
47 Keywords?: string;
48 /** Summary tab "Comments" */
49 Comments?: string;
50 /** Statistics tab "Last saved by" */
51 LastAuthor?: string;
52 /** Statistics tab "Created" */
53 CreatedDate?: Date;
54}
55
56/** Other supported properties */
57export interface FullProperties extends Properties {
58 ModifiedDate?: Date;
59 Application?: string;
60 AppVersion?: string;
61 DocSecurity?: string;
62 HyperlinksChanged?: boolean;
63 SharedDoc?: boolean;
64 LinksUpToDate?: boolean;
65 ScaleCrop?: boolean;
66 Worksheets?: number;
67 SheetNames?: string[];
68 ContentStatus?: string;
69 LastPrinted?: string;
70 Revision?: string | number;
71 Version?: string;
72 Identifier?: string;
73 Language?: string;
74}
75
76export interface CommonOptions {
77 /**
78 * If true, throw errors when features are not understood
79 * @default false
80 */
81 WTF?: boolean;
82
83 /**
84 * When reading a file with VBA macros, expose CFB blob to `vbaraw` field
85 * When writing BIFF8/XLSB/XLSM, reseat `vbaraw` and export to file
86 * @default false
87 */
88 bookVBA?: boolean;
89
90 /**
91 * When reading a file, store dates as type d (default is n)
92 * When writing XLSX/XLSM file, use native date (default uses date codes)
93 * @default false
94 */
95 cellDates?: boolean;
96
97 /**
98 * When reading a file, save style/theme info to the .s field
99 * When writing a file, export style/theme info
100 * @default false
101 */
102 cellStyles?: boolean;
103}
104
105export interface DateNFOption {
106 /** Use specified date format */
107 dateNF?: NumberFormat;
108}
109
110/** Options for read and readFile */
111export interface ParsingOptions extends CommonOptions {
112 /** Input data encoding */
113 type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array' | 'string';
114
115 /** Default codepage */
116 codepage?: number;
117
118 /**
119 * Save formulae to the .f field
120 * @default true
121 */
122 cellFormula?: boolean;
123
124 /**
125 * Parse rich text and save HTML to the .h field
126 * @default true
127 */
128 cellHTML?: boolean;
129
130 /**
131 * Save number format string to the .z field
132 * @default false
133 */
134 cellNF?: boolean;
135
136 /**
137 * Generate formatted text to the .w field
138 * @default true
139 */
140 cellText?: boolean;
141
142 /** Override default date format (code 14) */
143 dateNF?: string;
144
145 /**
146 * Create cell objects for stub cells
147 * @default false
148 */
149 sheetStubs?: boolean;
150
151 /**
152 * If >0, read the first sheetRows rows
153 * @default 0
154 */
155 sheetRows?: number;
156
157 /**
158 * If true, parse calculation chains
159 * @default false
160 */
161 bookDeps?: boolean;
162
163 /**
164 * If true, add raw files to book object
165 * @default false
166 */
167 bookFiles?: boolean;
168
169 /**
170 * If true, only parse enough to get book metadata
171 * @default false
172 */
173 bookProps?: boolean;
174
175 /**
176 * If true, only parse enough to get the sheet names
177 * @default false
178 */
179 bookSheets?: boolean;
180
181 /**
182 * If defined and file is encrypted, use password
183 * @default ''
184 */
185 password?: string;
186
187 /* If true, plaintext parsing will not parse values */
188 raw?: boolean;
189
190 dense?: boolean;
191}
192
193/** Options for write and writeFile */
194export interface WritingOptions extends CommonOptions {
195 /** Output data encoding */
196 type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array' | 'string';
197
198 /**
199 * Generate Shared String Table
200 * @default false
201 */
202 bookSST?: boolean;
203
204 /**
205 * File format of generated workbook
206 * @default 'xlsx'
207 */
208 bookType?: BookType;
209
210 /**
211 * Name of Worksheet (for single-sheet formats)
212 * @default ''
213 */
214 sheet?: string;
215
216 /**
217 * Use ZIP compression for ZIP-based formats
218 * @default false
219 */
220 compression?: boolean;
221
222 /**
223 * Suppress "number stored as text" errors in generated files
224 * @default true
225 */
226 ignoreEC?: boolean;
227
228 /** Override workbook properties on save */
229 Props?: Properties;
230}
231
232/** Workbook Object */
233export interface WorkBook {
234 /**
235 * A dictionary of the worksheets in the workbook.
236 * Use SheetNames to reference these.
237 */
238 Sheets: { [sheet: string]: WorkSheet };
239
240 /** Ordered list of the sheet names in the workbook */
241 SheetNames: string[];
242
243 /** Standard workbook Properties */
244 Props?: FullProperties;
245
246 /** Custom workbook Properties */
247 Custprops?: object;
248
249 Workbook?: WBProps;
250
251 vbaraw?: any;
252}
253
254export interface SheetProps {
255 /** Sheet Visibility (0=Visible 1=Hidden 2=VeryHidden) */
256 Hidden?: 0 | 1 | 2;
257
258 /** Name of Document Module in associated VBA Project */
259 CodeName?: string;
260}
261
262/** Defined Name Object */
263export interface DefinedName {
264 /** Name */
265 Name: string;
266
267 /** Reference */
268 Ref: string;
269
270 /** Scope (undefined for workbook scope) */
271 Sheet?: number;
272
273 /** Name comment */
274 Comment?: string;
275}
276
277/** Workbook-Level Attributes */
278export interface WBProps {
279 /** Sheet Properties */
280 Sheets?: SheetProps[];
281
282 /** Defined Names */
283 Names?: DefinedName[];
284
285 /** Workbook Views */
286 Views?: WBView[];
287
288 /** Other Workbook Properties */
289 WBProps?: WorkbookProperties;
290}
291
292/** Workbook View */
293export interface WBView {
294 /** Right-to-left mode */
295 RTL?: boolean;
296}
297
298/** Other Workbook Properties */
299export interface WorkbookProperties {
300 /** Worksheet Epoch (1904 if true, 1900 if false) */
301 date1904?: boolean;
302
303 /** Warn or strip personally identifying info on save */
304 filterPrivacy?: boolean;
305
306 /** Name of Document Module in associated VBA Project */
307 CodeName?: string;
308}
309
310/** Column Properties Object */
311export interface ColInfo {
312 /* --- visibility --- */
313
314 /** if true, the column is hidden */
315 hidden?: boolean;
316
317 /* --- column width --- */
318
319 /** width in Excel's "Max Digit Width", width*256 is integral */
320 width?: number;
321
322 /** width in screen pixels */
323 wpx?: number;
324
325 /** width in "characters" */
326 wch?: number;
327
328 /** Excel's "Max Digit Width" unit, always integral */
329 MDW?: number;
330}
331
332/** Row Properties Object */
333export interface RowInfo {
334 /* --- visibility --- */
335
336 /** if true, the column is hidden */
337 hidden?: boolean;
338
339 /* --- row height --- */
340
341 /** height in screen pixels */
342 hpx?: number;
343
344 /** height in points */
345 hpt?: number;
346
347 /** outline / group level */
348 level?: number;
349}
350
351/**
352 * Write sheet protection properties.
353 */
354export interface ProtectInfo {
355 /**
356 * The password for formats that support password-protected sheets
357 * (XLSX/XLSB/XLS). The writer uses the XOR obfuscation method.
358 */
359 password?: string;
360 /**
361 * Select locked cells
362 * @default: true
363 */
364 selectLockedCells?: boolean;
365 /**
366 * Select unlocked cells
367 * @default: true
368 */
369 selectUnlockedCells?: boolean;
370 /**
371 * Format cells
372 * @default: false
373 */
374 formatCells?: boolean;
375 /**
376 * Format columns
377 * @default: false
378 */
379 formatColumns?: boolean;
380 /**
381 * Format rows
382 * @default: false
383 */
384 formatRows?: boolean;
385 /**
386 * Insert columns
387 * @default: false
388 */
389 insertColumns?: boolean;
390 /**
391 * Insert rows
392 * @default: false
393 */
394 insertRows?: boolean;
395 /**
396 * Insert hyperlinks
397 * @default: false
398 */
399 insertHyperlinks?: boolean;
400 /**
401 * Delete columns
402 * @default: false
403 */
404 deleteColumns?: boolean;
405 /**
406 * Delete rows
407 * @default: false
408 */
409 deleteRows?: boolean;
410 /**
411 * Sort
412 * @default: false
413 */
414 sort?: boolean;
415 /**
416 * Filter
417 * @default: false
418 */
419 autoFilter?: boolean;
420 /**
421 * Use PivotTable reports
422 * @default: false
423 */
424 pivotTables?: boolean;
425 /**
426 * Edit objects
427 * @default: true
428 */
429 objects?: boolean;
430 /**
431 * Edit scenarios
432 * @default: true
433 */
434 scenarios?: boolean;
435}
436
437/** Page Margins -- see Excel Page Setup .. Margins diagram for explanation */
438export interface MarginInfo {
439 /** Left side margin (inches) */
440 left?: number;
441 /** Right side margin (inches) */
442 right?: number;
443 /** Top side margin (inches) */
444 top?: number;
445 /** Bottom side margin (inches) */
446 bottom?: number;
447 /** Header top margin (inches) */
448 header?: number;
449 /** Footer bottom height (inches) */
450 footer?: number;
451}
452export type SheetType = 'sheet' | 'chart';
453export type SheetKeys = string | MarginInfo | SheetType;
454/** General object representing a Sheet (worksheet or chartsheet) */
455export interface Sheet {
456 /**
457 * Indexing with a cell address string maps to a cell object
458 * Special keys start with '!'
459 */
460 [cell: string]: CellObject | SheetKeys | any;
461
462 /** Sheet type */
463 '!type'?: SheetType;
464
465 /** Sheet Range */
466 '!ref'?: string;
467
468 /** Page Margins */
469 '!margins'?: MarginInfo;
470}
471
472/** AutoFilter properties */
473export interface AutoFilterInfo {
474 /** Range of the AutoFilter table */
475 ref: string;
476}
477export type WSKeys = SheetKeys | ColInfo[] | RowInfo[] | Range[] | ProtectInfo | AutoFilterInfo;
478
479/** Worksheet Object */
480export interface WorkSheet extends Sheet {
481 /**
482 * Indexing with a cell address string maps to a cell object
483 * Special keys start with '!'
484 */
485 [cell: string]: CellObject | WSKeys | any;
486
487 /** Column Info */
488 '!cols'?: ColInfo[];
489
490 /** Row Info */
491 '!rows'?: RowInfo[];
492
493 /** Merge Ranges */
494 '!merges'?: Range[];
495
496 /** Worksheet Protection info */
497 '!protect'?: ProtectInfo;
498
499 /** AutoFilter info */
500 '!autofilter'?: AutoFilterInfo;
501}
502
503/**
504 * The Excel data type for a cell.
505 * b Boolean, n Number, e error, s String, d Date, z Stub
506 */
507export type ExcelDataType = 'b' | 'n' | 'e' | 's' | 'd' | 'z';
508
509/**
510 * Type of generated workbook
511 * @default 'xlsx'
512 */
513export type BookType = 'xlsx' | 'xlsm' | 'xlsb' | 'xls' | 'xla' | 'biff8' | 'biff5' | 'biff2' | 'xlml' | 'ods' | 'fods' | 'csv' | 'txt' | 'sylk' | 'html' | 'dif' | 'rtf' | 'prn' | 'eth';
514
515/** Comment element */
516export interface Comment {
517 /** Author of the comment block */
518 a?: string;
519
520 /** Plaintext of the comment */
521 t: string;
522}
523
524/** Link object */
525export interface Hyperlink {
526 /** Target of the link (HREF) */
527 Target: string;
528
529 /** Plaintext tooltip to display when mouse is over cell */
530 Tooltip?: string;
531}
532
533/** Worksheet Cell Object */
534export interface CellObject {
535 /** The raw value of the cell. Can be omitted if a formula is specified */
536 v?: string | number | boolean | Date;
537
538 /** Formatted text (if applicable) */
539 w?: string;
540
541 /**
542 * The Excel Data Type of the cell.
543 * b Boolean, n Number, e Error, s String, d Date, z Empty
544 */
545 t: ExcelDataType;
546
547 /** Cell formula (if applicable) */
548 f?: string;
549
550 /** Range of enclosing array if formula is array formula (if applicable) */
551 F?: string;
552
553 /** Rich text encoding (if applicable) */
554 r?: any;
555
556 /** HTML rendering of the rich text (if applicable) */
557 h?: string;
558
559 /** Comments associated with the cell */
560 c?: Comment[];
561
562 /** Number format string associated with the cell (if requested) */
563 z?: NumberFormat;
564
565 /** Cell hyperlink object (.Target holds link, .tooltip is tooltip) */
566 l?: Hyperlink;
567
568 /** The style/theme of the cell (if applicable) */
569 s?: any;
570}
571
572/** Simple Cell Address */
573export interface CellAddress {
574 /** Column number */
575 c: number;
576 /** Row number */
577 r: number;
578}
579
580/**
581 * Range object (representing ranges like "A1:B2")
582 */
583export interface Range {
584 /** Starting cell */
585 s: CellAddress;
586 /** Ending cell */
587 e: CellAddress;
588}
589
590export interface Sheet2CSVOpts extends DateNFOption {
591 /** Field Separator ("delimiter") */
592 FS?: string;
593
594 /** Record Separator ("row separator") */
595 RS?: string;
596
597 /** Remove trailing field separators in each record */
598 strip?: boolean;
599
600 /** Include blank lines in the CSV output */
601 blankrows?: boolean;
602
603 /** Skip hidden rows and columns in the CSV output */
604 skipHidden?: boolean;
605}
606
607export interface OriginOption {
608 /** Top-Left cell for operation (CellAddress or A1 string or row) */
609 origin?: number | string | CellAddress;
610}
611
612export interface Sheet2HTMLOpts {
613 /** TABLE element id attribute */
614 id?: string;
615
616 /** Add contenteditable to every cell */
617 editable?: boolean;
618
619 /** Header HTML */
620 header?: string;
621
622 /** Footer HTML */
623 footer?: string;
624}
625
626export interface Sheet2JSONOpts extends DateNFOption {
627 /** Output format */
628 header?: "A"|number|string[];
629
630 /** Override worksheet range */
631 range?: any;
632
633 /** Include or omit blank lines in the output */
634 blankrows?: boolean;
635
636 /** Default value for null/undefined values */
637 defval?: any;
638
639 /** if true, return raw data; if false, return formatted text */
640 raw?: boolean;
641}
642
643export interface AOA2SheetOpts extends CommonOptions, DateNFOption {
644 /**
645 * Create cell objects for stub cells
646 * @default false
647 */
648 sheetStubs?: boolean;
649}
650
651export interface SheetAOAOpts extends AOA2SheetOpts, OriginOption {}
652
653export interface JSON2SheetOpts extends CommonOptions, DateNFOption {
654 /** Use specified column order */
655 header?: string[];
656
657 /** Skip header row in generated sheet */
658 skipHeader?: boolean;
659}
660
661export interface SheetJSONOpts extends JSON2SheetOpts, OriginOption {}
662
663export interface Table2SheetOpts extends CommonOptions, DateNFOption {
664 /* If true, plaintext parsing will not parse values */
665 raw?: boolean;
666
667 /**
668 * If >0, read the first sheetRows rows
669 * @default 0
670 */
671 sheetRows?: number;
672
673 /** If true, hidden rows and cells will not be parsed */
674 display?: boolean;
675}
676
677/** General utilities */
678export interface XLSX$Utils {
679 /* --- Import Functions --- */
680
681 /** Converts an array of arrays of JS data to a worksheet. */
682 aoa_to_sheet<T>(data: T[][], opts?: AOA2SheetOpts): WorkSheet;
683 aoa_to_sheet(data: any[][], opts?: AOA2SheetOpts): WorkSheet;
684
685 /** Converts an array of JS objects to a worksheet. */
686 json_to_sheet<T>(data: T[], opts?: JSON2SheetOpts): WorkSheet;
687 json_to_sheet(data: any[], opts?: JSON2SheetOpts): WorkSheet;
688
689 /** BROWSER ONLY! Converts a TABLE DOM element to a worksheet. */
690 table_to_sheet(data: any, opts?: Table2SheetOpts): WorkSheet;
691 table_to_book(data: any, opts?: Table2SheetOpts): WorkBook;
692
693 /* --- Export Functions --- */
694
695 /** Converts a worksheet object to an array of JSON objects */
696 sheet_to_json<T>(worksheet: WorkSheet, opts?: Sheet2JSONOpts): T[];
697 sheet_to_json(worksheet: WorkSheet, opts?: Sheet2JSONOpts): any[][];
698 sheet_to_json(worksheet: WorkSheet, opts?: Sheet2JSONOpts): any[];
699
700 /** Generates delimiter-separated-values output */
701 sheet_to_csv(worksheet: WorkSheet, options?: Sheet2CSVOpts): string;
702
703 /** Generates UTF16 Formatted Text */
704 sheet_to_txt(worksheet: WorkSheet, options?: Sheet2CSVOpts): string;
705
706 /** Generates HTML */
707 sheet_to_html(worksheet: WorkSheet, options?: Sheet2HTMLOpts): string;
708
709 /** Generates a list of the formulae (with value fallbacks) */
710 sheet_to_formulae(worksheet: WorkSheet): string[];
711
712 /** Generates DIF */
713 sheet_to_dif(worksheet: WorkSheet, options?: Sheet2HTMLOpts): string;
714
715 /** Generates SYLK (Symbolic Link) */
716 sheet_to_slk(worksheet: WorkSheet, options?: Sheet2HTMLOpts): string;
717
718 /** Generates ETH */
719 sheet_to_eth(worksheet: WorkSheet, options?: Sheet2HTMLOpts): string;
720
721 /* --- Cell Address Utilities --- */
722
723 /** Converts 0-indexed cell address to A1 form */
724 encode_cell(cell: CellAddress): string;
725
726 /** Converts 0-indexed row to A1 form */
727 encode_row(row: number): string;
728
729 /** Converts 0-indexed column to A1 form */
730 encode_col(col: number): string;
731
732 /** Converts 0-indexed range to A1 form */
733 encode_range(s: CellAddress, e: CellAddress): string;
734 encode_range(r: Range): string;
735
736 /** Converts A1 cell address to 0-indexed form */
737 decode_cell(address: string): CellAddress;
738
739 /** Converts A1 row to 0-indexed form */
740 decode_row(row: string): number;
741
742 /** Converts A1 column to 0-indexed form */
743 decode_col(col: string): number;
744
745 /** Converts A1 range to 0-indexed form */
746 decode_range(range: string): Range;
747
748 /** Format cell */
749 format_cell(cell: CellObject, v?: any, opts?: any): string;
750
751 /* --- General Utilities --- */
752
753 /** Creates a new workbook */
754 book_new(): WorkBook;
755
756 /** Append a worksheet to a workbook */
757 book_append_sheet(workbook: WorkBook, worksheet: WorkSheet, name?: string): void;
758
759 /** Set sheet visibility (visible/hidden/very hidden) */
760 book_set_sheet_visibility(workbook: WorkBook, sheet: number|string, visibility: number): void;
761
762 /** Set number format for a cell */
763 cell_set_number_format(cell: CellObject, fmt: string|number): CellObject;
764
765 /** Set hyperlink for a cell */
766 cell_set_hyperlink(cell: CellObject, target: string, tooltip?: string): CellObject;
767
768 /** Set internal link for a cell */
769 cell_set_internal_link(cell: CellObject, target: string, tooltip?: string): CellObject;
770
771 /** Add comment to a cell */
772 cell_add_comment(cell: CellObject, text: string, author?: string): void;
773
774 /** Assign an Array Formula to a range */
775 sheet_set_array_formula(ws: WorkSheet, range: Range|string, formula: string): WorkSheet;
776
777 /** Add an array of arrays of JS data to a worksheet */
778 sheet_add_aoa<T>(ws: WorkSheet, data: T[][], opts?: SheetAOAOpts): WorkSheet;
779 sheet_add_aoa(ws: WorkSheet, data: any[][], opts?: SheetAOAOpts): WorkSheet;
780
781 /** Add an array of JS objects to a worksheet */
782 sheet_add_json(ws: WorkSheet, data: any[], opts?: SheetJSONOpts): WorkSheet;
783 sheet_add_json<T>(ws: WorkSheet, data: T[], opts?: SheetJSONOpts): WorkSheet;
784
785
786 consts: XLSX$Consts;
787}
788
789export interface XLSX$Consts {
790 /* --- Sheet Visibility --- */
791
792 /** Visibility: Visible */
793 SHEET_VISIBLE: 0;
794
795 /** Visibility: Hidden */
796 SHEET_HIDDEN: 1;
797
798 /** Visibility: Very Hidden */
799 SHEET_VERYHIDDEN: 2;
800}
801
802/** NODE ONLY! these return Readable Streams */
803export interface StreamUtils {
804 /** CSV output stream, generate one line at a time */
805 to_csv(sheet: WorkSheet, opts?: Sheet2CSVOpts): any;
806 /** HTML output stream, generate one line at a time */
807 to_html(sheet: WorkSheet, opts?: Sheet2HTMLOpts): any;
808 /** JSON object stream, generate one row at a time */
809 to_json(sheet: WorkSheet, opts?: Sheet2JSONOpts): any;
810}