| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2022-06-01 22:59:29 +00:00
										 |  |  | sidebar_position: 4 | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Workbook Object
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 10:00:35 +00:00
										 |  |  | For a given workbook object `wb`: | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 10:00:35 +00:00
										 |  |  | `wb.SheetNames` is an ordered list of the sheets in the workbook. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Sheets` is an object whose keys are worksheet names (from `SheetNames`) and | 
					
						
							|  |  |  | whose values are worksheet objects. | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | `wb.Props` is an object storing the standard properties.  `wb.Custprops` stores | 
					
						
							|  |  |  | custom properties.  Since the XLS standard properties deviate from the XLSX | 
					
						
							|  |  |  | standard, XLS parsing stores core properties in both places. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Workbook` stores [workbook-level attributes](#workbook-level-attributes). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 10:00:35 +00:00
										 |  |  | When reading a file, `wb.bookType` is the determined book type. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | ## File Properties
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The various file formats use different internal names for file properties.  The | 
					
						
							|  |  |  | workbook `Props` object normalizes the names: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-01 22:59:29 +00:00
										 |  |  | <details open><summary><b>File Properties</b> (click to hide)</summary> | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | | JS Name       | Excel Description              | | 
					
						
							|  |  |  | |:--------------|:-------------------------------| | 
					
						
							|  |  |  | | `Title`       | Summary tab "Title"            | | 
					
						
							|  |  |  | | `Subject`     | Summary tab "Subject"          | | 
					
						
							|  |  |  | | `Author`      | Summary tab "Author"           | | 
					
						
							|  |  |  | | `Manager`     | Summary tab "Manager"          | | 
					
						
							|  |  |  | | `Company`     | Summary tab "Company"          | | 
					
						
							|  |  |  | | `Category`    | Summary tab "Category"         | | 
					
						
							|  |  |  | | `Keywords`    | Summary tab "Keywords"         | | 
					
						
							|  |  |  | | `Comments`    | Summary tab "Comments"         | | 
					
						
							|  |  |  | | `LastAuthor`  | Statistics tab "Last saved by" | | 
					
						
							|  |  |  | | `CreatedDate` | Statistics tab "Created"       | | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </details> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example, to set the workbook title property: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | if(!wb.Props) wb.Props = {}; | 
					
						
							|  |  |  | wb.Props.Title = "Insert Title Here"; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Custom properties are added in the workbook `Custprops` object: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | if(!wb.Custprops) wb.Custprops = {}; | 
					
						
							|  |  |  | wb.Custprops["Custom Property"] = "Custom Value"; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Writers will process the `Props` key of the options object: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | /* force the Author to be "SheetJS" */ | 
					
						
							| 
									
										
										
										
											2023-04-01 20:13:16 +00:00
										 |  |  | XLSX.write(wb, { Props: { Author: "SheetJS" } }); | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Workbook-Level Attributes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Workbook` stores workbook-level attributes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Defined Names
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <details> | 
					
						
							|  |  |  |   <summary><b>Format Support</b> (click to show)</summary> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-30 05:04:05 +00:00
										 |  |  | **Simple Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML, ODS, SYLK | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-30 05:04:05 +00:00
										 |  |  | **Unicode Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML, ODS | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | **Defined Name Comment**: XLSX/M, XLSB, BIFF8 XLS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </details> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Workbook.Names` is an array of defined name objects which have the keys: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | | Key       | Description                                                      | | 
					
						
							|  |  |  | |:----------|:-----------------------------------------------------------------| | 
					
						
							|  |  |  | | `Sheet`   | Name scope.  Sheet Index (0 = first sheet) or `null` (Workbook)  | | 
					
						
							|  |  |  | | `Name`    | Case-sensitive name.  Standard rules apply **                    | | 
					
						
							| 
									
										
										
										
											2022-08-23 03:20:02 +00:00
										 |  |  | | `Ref`     | A1-Style Reference (`"Sheet1!$A$1:$D$20"`)                       | | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | | `Comment` | Comment (only applicable for XLS/XLSX/XLSB)                      | | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Excel allows two sheet-scoped defined names to share the same name.  However, a | 
					
						
							|  |  |  | sheet-scoped name cannot collide with a workbook-scope name.  Workbook writers | 
					
						
							|  |  |  | may not enforce this constraint. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 20:13:16 +00:00
										 |  |  | The following snippet creates a worksheet-level defined name `"Global"` and a | 
					
						
							|  |  |  | local defined name `"Local"` with distinct values for first and second sheets: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | /* ensure the workbook structure exists */ | 
					
						
							|  |  |  | if(!wb.Workbook) wb.Workbook = {}; | 
					
						
							|  |  |  | if(!wb.Workbook.Names) wb.Workbook.Names = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* "Global" workbook-level -> Sheet1 A1:A2 */ | 
					
						
							|  |  |  | wb.Workbook.Names.push({ Name: "Global", Ref: "Sheet1!$A$1:$A$2" }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* "Local" scoped to the first worksheet -> Sheet1 B1:B2 */ | 
					
						
							|  |  |  | wb.Workbook.Names.push({ Name: "Local",  Ref: "Sheet1!$B$1:$B$2", Sheet: 0 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* "Local" scoped to the second worksheet -> Sheet1 C1:C2 */ | 
					
						
							|  |  |  | wb.Workbook.Names.push({ Name: "Local",  Ref: "Sheet1!$C$1:$C$2", Sheet: 1 }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <details><summary><b>Live Example</b> (click to show)</summary> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```jsx live | 
					
						
							|  |  |  | /* The live editor requires this function wrapper */ | 
					
						
							|  |  |  | function DefinedNameExport() { return ( <button onClick={() => { | 
					
						
							|  |  |  |   /* Create empty workbook */ | 
					
						
							|  |  |  |   var wb = XLSX.utils.book_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Create worksheet Sheet1 */ | 
					
						
							|  |  |  |   var ws1 = XLSX.utils.aoa_to_sheet([[1,2,3],[4,5,6],["Global",0],["Local",0]]); | 
					
						
							|  |  |  |   XLSX.utils.book_append_sheet(wb, ws1, "Sheet1"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Create worksheet Sheet2 */ | 
					
						
							|  |  |  |   var ws2 = XLSX.utils.aoa_to_sheet([["Global",0],["Local",0]]); | 
					
						
							|  |  |  |   XLSX.utils.book_append_sheet(wb, ws2, "Sheet2"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Create defined names */ | 
					
						
							|  |  |  |   if(!wb.Workbook) wb.Workbook = {}; | 
					
						
							|  |  |  |   if(!wb.Workbook.Names) wb.Workbook.Names = []; | 
					
						
							|  |  |  |   /* "Global" workbook-level -> Sheet1 A1:A2 */ | 
					
						
							|  |  |  |   wb.Workbook.Names.push({ Name: "Global", Ref: "Sheet1!$A$1:$A$2" }); | 
					
						
							|  |  |  |   /* "Local" scoped to the first worksheet -> Sheet1 B1:B2 */ | 
					
						
							|  |  |  |   wb.Workbook.Names.push({ Name: "Local", Sheet: 0, Ref: "Sheet1!$B$1:$B$2" }); | 
					
						
							|  |  |  |   /* "Local" scoped to the second worksheet -> Sheet1 C1:C2 */ | 
					
						
							|  |  |  |   wb.Workbook.Names.push({ Name: "Local", Sheet: 1, Ref: "Sheet1!$C$1:$C$2" }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Create formulae */ | 
					
						
							|  |  |  |   ws1["B3"].f = "SUM(Global)"; // Sheet1 B3 =SUM(Global)  1 + 4 = 5 | 
					
						
							|  |  |  |   ws1["B4"].f = "SUM(Local)";  // Sheet1 B4 =SUM(Local)   2 + 5 = 7 | 
					
						
							|  |  |  |   ws2["B1"].f = "SUM(Global)"; // Sheet2 B1 =SUM(Global)  1 + 4 = 5 | 
					
						
							|  |  |  |   ws2["B2"].f = "SUM(Local)";  // Sheet2 B2 =SUM(Local)   3 + 6 = 9 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Export to file (start a download) */ | 
					
						
							|  |  |  |   XLSX.writeFile(wb, "SheetJSDNExport.xlsx"); | 
					
						
							|  |  |  | }}><b>Export XLSX!</b></button> ); } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </details> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-16 03:26:04 +00:00
										 |  |  | ### Workbook Views
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Workbook.Views` is an array of workbook view objects which have the keys: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | | Key             | Description                                         | | 
					
						
							|  |  |  | |:----------------|:----------------------------------------------------| | 
					
						
							|  |  |  | | `RTL`           | If true, display right-to-left                      | | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Miscellaneous Workbook Properties
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `wb.Workbook.WBProps` holds other workbook properties: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 05:45:37 +00:00
										 |  |  | | Key             | Description                                                | | 
					
						
							|  |  |  | |:----------------|:-----------------------------------------------------------| | 
					
						
							|  |  |  | | `CodeName`      | [VBA Workbook Name](/docs/csf/features#vba-and-macros)     | | 
					
						
							|  |  |  | | `date1904`      | epoch: 0/false for 1900 system, 1/true for 1904            | | 
					
						
							|  |  |  | | `filterPrivacy` | Warn or strip personally identifying info on save          | |