| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | # canvas-datagrid
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `sheet_to_json` utility function generates output arrays suitable for use | 
					
						
							|  |  |  | with other JS libraries such as data grids for previewing data.  After extensive | 
					
						
							|  |  |  | testing, [`canvas-datagrid`](https://tonygermaneri.github.io/canvas-datagrid/) | 
					
						
							|  |  |  | stood out as a very high-performance grid with an incredibly simple API. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 06:01:53 +00:00
										 |  |  | This demo is available at <http://oss.sheetjs.com/sheetjs/datagrid.html> | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | ## Obtaining the Library
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The [`canvas-datagrid` npm nodule](http://npm.im/canvas-datagrid) includes a | 
					
						
							|  |  |  | minified script `dist/canvas-datagrid.js` that can be directly inserted as a | 
					
						
							|  |  |  | script tag.  The unpkg CDN also exposes the latest version: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <script src="https://unpkg.com/canvas-datagrid/dist/canvas-datagrid.js"></script> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Previewing Data
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The HTML document needs a container element: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <div id="gridctr"></div> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Grid initialization is a one-liner: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | var grid = canvasDatagrid({ | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  |   parentNode: document.getElementById('gridctr'), | 
					
						
							|  |  |  |   data: [] | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 22:35:11 +00:00
										 |  |  | For large data sets, it's necessary to constrain the size of the grid. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | grid.style.height = '100%'; | 
					
						
							|  |  |  | grid.style.width = '100%'; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | Once the workbook is read and the worksheet is selected, assigning the data | 
					
						
							|  |  |  | variable automatically updates the view: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | grid.data = XLSX.utils.sheet_to_json(ws, {header:1}); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:46:49 +00:00
										 |  |  | This demo previews the first worksheet. | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Editing
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | `canvas-datagrid` handles the entire edit cycle.  No intervention is necessary. | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Saving Data
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | `grid.data` is immediately readable and can be converted back to a worksheet. | 
					
						
							|  |  |  | Some versions return an array-like object without the length, so a little bit of | 
					
						
							|  |  |  | preparation may be needed: | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | /* converts an array of array-like objects into an array of arrays */ | 
					
						
							|  |  |  | function prep(arr) { | 
					
						
							|  |  |  |   var out = []; | 
					
						
							|  |  |  |   for(var i = 0; i < arr.length; ++i) { | 
					
						
							|  |  |  |     if(!arr[i]) continue; | 
					
						
							|  |  |  |     if(Array.isArray(arr[i])) { out[i] = arr[i]; continue }; | 
					
						
							|  |  |  |     var o = new Array(); | 
					
						
							|  |  |  |     Object.keys(arr[i]).forEach(function(k) { o[+k] = arr[i][k] }); | 
					
						
							|  |  |  |     out[i] = o; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | /* build worksheet from the grid data */ | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | var ws = XLSX.utils.aoa_to_sheet(prep(grid.data)); | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* build up workbook */ | 
					
						
							|  |  |  | var wb = XLSX.utils.book_new(); | 
					
						
							|  |  |  | XLSX.utils.book_append_sheet(wb, ws, 'SheetJS'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 20:46:32 +00:00
										 |  |  | /* generate download */ | 
					
						
							|  |  |  | XLSX.writeFile(wb, "SheetJS.xlsx"); | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Additional Features
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This demo barely scratches the surface.  The underlying grid component includes | 
					
						
							|  |  |  | many additional features including massive data streaming, sorting and styling. | 
					
						
							| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | [](https://github.com/SheetJS/js-xlsx) |