UNPKG

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