| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | /* writing feature test -- look for TEST: in comments */ | 
					
						
							|  |  |  | /* vim: set ts=2 ft=javascript: */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* original data */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | let data: any[][] = [ | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 	[1, 2, 3], | 
					
						
							|  |  |  | 	[true, false, null, "sheetjs"], | 
					
						
							|  |  |  | 	["foo", "bar", new Date("2014-02-19T14:30Z"), "0.3"], | 
					
						
							|  |  |  | 	["baz", null, "qux", 3.14159], | 
					
						
							|  |  |  | 	["hidden"], | 
					
						
							|  |  |  | 	["visible"] | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ws_name = "SheetJS"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | let wscols: XLSX.ColInfo[] = [ | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 	{wch: 6}, // "characters"
 | 
					
						
							|  |  |  | 	{wpx: 50}, // "pixels"
 | 
					
						
							|  |  |  | 	, | 
					
						
							|  |  |  | 	{hidden: true} // hide column
 | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* At 96 PPI, 1 pt = 1 px */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | let wsrows: XLSX.RowInfo[] = [ | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 	{hpt: 12}, // "points"
 | 
					
						
							|  |  |  | 	{hpx: 16}, // "pixels"
 | 
					
						
							|  |  |  | 	, | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	{hpx: 24, level:3}, | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 	{hidden: true}, // hide row
 | 
					
						
							|  |  |  | 	{hidden: false} | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log("Sheet Name: " + ws_name); | 
					
						
							|  |  |  | console.log("Data: "); | 
					
						
							|  |  |  | let i = 0; | 
					
						
							|  |  |  | for(i = 0; i !== data.length; ++i) console.log(data[i]); | 
					
						
							|  |  |  | console.log("Columns :"); | 
					
						
							|  |  |  | for(i = 0; i !== wscols.length; ++i) console.log(wscols[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* require XLSX */ | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | import * as XLSX from 'xlsx'; | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* blank workbook constructor */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let wb: XLSX.WorkBook = { SheetNames: <string[]>[], Sheets: {} }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | wb = XLSX.utils.book_new(); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* convert an array of arrays in JS to a CSF spreadsheet */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | let ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(data, {cellDates:true}); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: add worksheet to workbook */ | 
					
						
							|  |  |  | wb.SheetNames.push(ws_name); | 
					
						
							|  |  |  | wb.Sheets[ws_name] = ws; | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.book_append_sheet(wb, ws, ws_name); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: simple formula */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | (<XLSX.CellObject>ws['C1']).f = "A1+B1"; | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | ws['C2'] = {t:'n', f:"A1+B1"}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: single-cell array formula */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ws['D1'] = {t:'n', f:"SUM(A1:C1*A1:C1)", F:"D1:D1"}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.sheet_set_array_formula(ws, 'D1:D1', "SUM(A1:C1*A1:C1)"); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: multi-cell array formula */ | 
					
						
							|  |  |  | ws['E1'] = {t:'n', f:"TRANSPOSE(A1:D1)", F:"E1:E4"}; | 
					
						
							|  |  |  | ws['E2'] = {t:'n', F:"E1:E4"}; | 
					
						
							|  |  |  | ws['E3'] = {t:'n', F:"E1:E4"}; | 
					
						
							|  |  |  | ws['E4'] = {t:'n', F:"E1:E4"}; | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.sheet_set_array_formula(ws, 'E1:E4', "TRANSPOSE(A1:D1)"); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | ws["!ref"] = "A1:E6"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: column props */ | 
					
						
							|  |  |  | ws['!cols'] = wscols; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: row props */ | 
					
						
							|  |  |  | ws['!rows'] = wsrows; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: hyperlink note: Excel does not automatically style hyperlinks */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | (<XLSX.CellObject>ws['A3']).l = { Target: "http://sheetjs.com", Tooltip: "Visit us <SheetJS.com!>" }; | 
					
						
							| 
									
										
										
										
											2017-08-18 18:10:18 +00:00
										 |  |  | XLSX.utils.cell_set_hyperlink(ws['A3'], "http://sheetjs.com", "Visit us <SheetJS.com!>"); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: built-in format */ | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | (<XLSX.CellObject>ws['B1']).z = "0%"; // Format Code 9
 | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.cell_set_number_format(ws['B1'], "0%"); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: custom format */ | 
					
						
							|  |  |  | const custfmt = "\"This is \"\\ 0.0"; | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | (<XLSX.CellObject>ws['C2']).z = custfmt; | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.cell_set_number_format(ws['C2'], custfmt); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: page margins */ | 
					
						
							|  |  |  | ws['!margins'] =  { left:1.0, right:1.0, top:1.0, bottom:1.0, header:0.5, footer:0.5 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log("JSON Data:"); | 
					
						
							|  |  |  | console.log(XLSX.utils.sheet_to_json(ws, {header:1})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: hidden sheets */ | 
					
						
							|  |  |  | wb.SheetNames.push("Hidden"); | 
					
						
							|  |  |  | wb.Sheets["Hidden"] = XLSX.utils.aoa_to_sheet(["Hidden".split(""), [1,2,3]]); | 
					
						
							|  |  |  | wb.Workbook = {Sheets:[]}; | 
					
						
							|  |  |  | wb.Workbook.Sheets[1] = {Hidden:1}; | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | const data_2 = ["Hidden".split(""), [1,2,3]]; | 
					
						
							|  |  |  | XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet(data_2), "Hidden"); | 
					
						
							|  |  |  | XLSX.utils.book_set_sheet_visibility(wb, "Hidden", XLSX.utils.consts.SHEET_HIDDEN); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: properties */ | 
					
						
							|  |  |  | wb.Props = { | 
					
						
							|  |  |  | 	Title: "SheetJS Test", | 
					
						
							|  |  |  | 	Subject: "Tests", | 
					
						
							|  |  |  | 	Author: "Devs at SheetJS", | 
					
						
							|  |  |  | 	Manager: "Sheet Manager", | 
					
						
							|  |  |  | 	Company: "SheetJS", | 
					
						
							|  |  |  | 	Category: "Experimentation", | 
					
						
							|  |  |  | 	Keywords: "Test", | 
					
						
							|  |  |  | 	Comments: "Nothing to say here", | 
					
						
							|  |  |  | 	LastAuthor: "Not SheetJS", | 
					
						
							|  |  |  | 	CreatedDate: new Date(2017,1,19) | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* TEST: comments */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | (<XLSX.CellObject>ws['A4']).c = []; | 
					
						
							|  |  |  | (<XLSX.CellObject>ws['A4']).c.push({a:"SheetJS",t:"I'm a little comment, short and stout!\n\nWell, Stout may be the wrong word"}); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 07:14:14 +00:00
										 |  |  | XLSX.utils.cell_add_comment(ws['A4'], "I'm a little comment, short and stout!\n\nWell, Stout may be the wrong word", "SheetJS"); | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* TEST: sheet protection */ | 
					
						
							|  |  |  | ws['!protect'] = { | 
					
						
							|  |  |  | 	password:"password", | 
					
						
							|  |  |  | 	/* enable formatting rows and columns */ | 
					
						
							|  |  |  | 	formatRows:false, | 
					
						
							|  |  |  | 	formatColumns:false, | 
					
						
							|  |  |  | 	/* disable editing objects and scenarios */ | 
					
						
							|  |  |  | 	objects:true, | 
					
						
							|  |  |  | 	scenarios:true | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-03 07:19:09 +00:00
										 |  |  | /* TEST: Workbook Properties */ | 
					
						
							|  |  |  | if(!wb.Workbook) wb.Workbook = {Sheets:[], WBProps:{}}; | 
					
						
							|  |  |  | if(!wb.Workbook.WBProps) wb.Workbook.WBProps = {}; | 
					
						
							|  |  |  | wb.Workbook.WBProps.filterPrivacy = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | console.log("Worksheet Model:"); | 
					
						
							|  |  |  | console.log(ws); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const filenames: Array<[string]|[string, XLSX.WritingOptions]> = [ | 
					
						
							|  |  |  | 	['sheetjs.xlsx', {bookSST:true}], | 
					
						
							|  |  |  | 	['sheetjs.xlsm'], | 
					
						
							|  |  |  | 	['sheetjs.xlsb'], | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	['sheetjs.xls', {bookType:'xls'}], | 
					
						
							|  |  |  | 	['sheetjs.biff2', {bookType:'biff2'}], | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | 	['sheetjs.xml.xls', {bookType:'xlml'}], | 
					
						
							|  |  |  | 	['sheetjs.ods'], | 
					
						
							|  |  |  | 	['sheetjs.fods'], | 
					
						
							|  |  |  | 	['sheetjs.csv'], | 
					
						
							|  |  |  | 	['sheetjs.txt'], | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	['sheetjs.slk'], | 
					
						
							|  |  |  | 	['sheetjs.htm'], | 
					
						
							|  |  |  | 	['sheetjs.dif'], | 
					
						
							|  |  |  | 	['sheetjs.prn'] | 
					
						
							| 
									
										
										
										
											2017-05-15 15:48:44 +00:00
										 |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | filenames.forEach((r) => { | 
					
						
							|  |  |  | 		/* write file */ | 
					
						
							|  |  |  | 		XLSX.writeFile(wb, r[0], r[1]); | 
					
						
							|  |  |  | 		/* test by reading back files */ | 
					
						
							|  |  |  | 		XLSX.readFile(r[0]); | 
					
						
							|  |  |  | }); |