| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | /* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | 
					
						
							|  |  |  | /* vim: set ts=2: */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  | import { Component } from '@angular/core'; | 
					
						
							|  |  |  | import { encoding } from '@nativescript/core/text'; | 
					
						
							|  |  |  | import { File, Folder, knownFolders, path } from '@nativescript/core/file-system'; | 
					
						
							|  |  |  | import { Dialogs } from '@nativescript/core'; | 
					
						
							|  |  |  | import { Page, GridLayout, WebView, DockLayout, Button } from '@nativescript/core'; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  | import * as XLSX from './xlsx.full.min'; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |   selector: 'ns-app', | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |   template: `
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |   <Page> | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |     <GridLayout rows="auto, *, auto"> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       <!-- data converted to HTML and rendered in web view --> | 
					
						
							|  |  |  |       <WebView row="1" src="{{html}}"></WebView> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       <DockLayout row="2" dock="bottom" stretchLastChild="false"> | 
					
						
							|  |  |  |         <Button text="Import File" (tap)="import()" style="padding: 10px"></Button> | 
					
						
							|  |  |  |         <Button text="Export File" (tap)="export()" style="padding: 10px"></Button> | 
					
						
							|  |  |  |       </DockLayout> | 
					
						
							|  |  |  |     </GridLayout> | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |   </Page> | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |   `
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class AppComponent { | 
					
						
							|  |  |  |   html: string = ""; | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     const ws = XLSX.utils.aoa_to_sheet([[1,2],[3,4]]); | 
					
						
							|  |  |  |     this.html = XLSX.utils.sheet_to_html(ws); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Import button */ | 
					
						
							|  |  |  |   async import() { | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     const filename: string = "SheetJSNS.csv"; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* find appropriate path */ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     const target: Folder = knownFolders.documents() || knownFolders.ios.sharedPublic(); | 
					
						
							|  |  |  |     const url: string = path.normalize(target.path + "///" + filename); | 
					
						
							|  |  |  |     const file: File = File.fromPath(url); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       /* get binary string */ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |       const bstr: string = await file.readText(encoding.ISO_8859_1); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /* read workbook */ | 
					
						
							|  |  |  |       const wb = XLSX.read(bstr, { type: "binary" }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* grab first sheet */ | 
					
						
							|  |  |  |       const wsname: string = wb.SheetNames[0]; | 
					
						
							|  |  |  |       const ws = wb.Sheets[wsname]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* update table */ | 
					
						
							|  |  |  |       this.html = XLSX.utils.sheet_to_html(ws); | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |       Dialogs.alert(`Attempting to read to ${filename} in ${url}`); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |     } catch(e) { | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |       Dialogs.alert(e.message); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Export button */ | 
					
						
							|  |  |  |   async export() { | 
					
						
							|  |  |  |     const wb = XLSX.read(this.html, { type: "string" }); | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     const filename: string = "SheetJSNS.csv"; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* generate binary string */ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     const wbout: string = XLSX.write(wb, { bookType: 'csv', type: 'binary' }); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* find appropriate path */ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     const target: Folder = knownFolders.documents() || knownFolders.ios.sharedPublic(); | 
					
						
							|  |  |  |     const url: string = path.normalize(target.path + "///" + filename); | 
					
						
							|  |  |  |     const file: File = File.fromPath(url); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* attempt to save binary string to file */ | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     await file.writeText(wbout, encoding.ISO_8859_1); | 
					
						
							|  |  |  |     Dialogs.alert(`Wrote to ${filename} in ${url}`); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:31:36 +00:00
										 |  |  |   }; | 
					
						
							|  |  |  | } |