| 
									
										
										
										
											2017-09-24 23:40:09 +00:00
										 |  |  | /* xlsx.js (C) 2013-present  SheetJS -- http://sheetjs.com */ | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | import JavaScriptCore; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | enum SJSError: Error { | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   case badJSContext; | 
					
						
							|  |  |  |   case badJSWorkbook; | 
					
						
							|  |  |  |   case badJSWorksheet; | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SJSWorksheet { | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   var context: JSContext!; | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |   var wb: JSValue; var ws: JSValue; | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   var idx: Int32; | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func toCSV() throws -> String { | 
					
						
							|  |  |  |     let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); | 
					
						
							|  |  |  |     let utils: JSValue! = XLSX.objectForKeyedSubscript("utils"); | 
					
						
							|  |  |  |     let sheet_to_csv: JSValue! = utils.objectForKeyedSubscript("sheet_to_csv"); | 
					
						
							|  |  |  |     return sheet_to_csv.call(withArguments: [ws]).toString(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   init(ctx: JSContext, workbook: JSValue, worksheet: JSValue, idx: Int32) throws { | 
					
						
							|  |  |  |     self.context = ctx; self.wb = workbook; self.ws = worksheet; self.idx = idx; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SJSWorkbook { | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   var context: JSContext!; | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |   var wb: JSValue; var SheetNames: JSValue; var Sheets: JSValue; | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func getSheetAtIndex(idx: Int32) throws -> SJSWorksheet { | 
					
						
							|  |  |  |     let SheetName: String = SheetNames.atIndex(Int(idx)).toString(); | 
					
						
							|  |  |  |     let ws: JSValue! = Sheets.objectForKeyedSubscript(SheetName); | 
					
						
							|  |  |  |     return try SJSWorksheet(ctx: context, workbook: wb, worksheet: ws, idx: idx); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func writeBStr(bookType: String = "xlsx") throws -> String { | 
					
						
							|  |  |  |     let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); | 
					
						
							|  |  |  |     context.evaluateScript(String(format: "var writeopts = {type:'binary', bookType:'%@'}", bookType)); | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     let writeopts: JSValue = context.objectForKeyedSubscript("writeopts"); | 
					
						
							|  |  |  |     let writefunc: JSValue = XLSX.objectForKeyedSubscript("write"); | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |     return writefunc.call(withArguments: [wb, writeopts]).toString(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   init(ctx: JSContext, wb: JSValue) throws { | 
					
						
							|  |  |  |     self.context = ctx; | 
					
						
							|  |  |  |     self.wb = wb; | 
					
						
							|  |  |  |     self.SheetNames = wb.objectForKeyedSubscript("SheetNames"); | 
					
						
							|  |  |  |     self.Sheets = wb.objectForKeyedSubscript("Sheets"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-14 20:06:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SheetJSCore { | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   var context: JSContext!; | 
					
						
							|  |  |  |   var XLSX: JSValue!; | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func init_context() throws -> JSContext { | 
					
						
							|  |  |  |     var context: JSContext! | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |       context = JSContext(); | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |       context.exceptionHandler = { _, X in if let e = X { print(e.toString()!); }; }; | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |       context.evaluateScript("var global = (function(){ return this; }).call(null);"); | 
					
						
							|  |  |  |       context.evaluateScript("if(typeof wbs == 'undefined') wbs = [];"); | 
					
						
							|  |  |  |       let src = try String(contentsOfFile: "xlsx.full.min.js"); | 
					
						
							|  |  |  |       context.evaluateScript(src); | 
					
						
							|  |  |  |       if context != nil { return context!; } | 
					
						
							|  |  |  |     } catch { print(error.localizedDescription); } | 
					
						
							|  |  |  |     throw SJSError.badJSContext; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func version() throws -> String { | 
					
						
							|  |  |  |     if let version = XLSX.objectForKeyedSubscript("version") { return version.toString(); } | 
					
						
							|  |  |  |     throw SJSError.badJSContext; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func readFile(file: String) throws -> SJSWorkbook { | 
					
						
							|  |  |  |     let data: String! = try String(contentsOfFile: file, encoding: String.Encoding.isoLatin1); | 
					
						
							|  |  |  |     return try readBStr(data: data); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   func readBStr(data: String) throws -> SJSWorkbook { | 
					
						
							| 
									
										
										
										
											2021-10-03 01:41:36 +00:00
										 |  |  |     context.setObject(data, forKeyedSubscript: "payload" as (NSCopying & NSObjectProtocol)); | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |     context.evaluateScript("var wb = XLSX.read(payload, {type:'binary'});"); | 
					
						
							|  |  |  |     let wb: JSValue! = context.objectForKeyedSubscript("wb"); | 
					
						
							|  |  |  |     if wb == nil { throw SJSError.badJSWorkbook; } | 
					
						
							|  |  |  |     return try SJSWorkbook(ctx: context, wb: wb); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 02:51:54 +00:00
										 |  |  |   init() throws { | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |       self.context = try init_context(); | 
					
						
							|  |  |  |       self.XLSX = self.context.objectForKeyedSubscript("XLSX"); | 
					
						
							|  |  |  |       if self.XLSX == nil { throw SJSError.badJSContext; } | 
					
						
							|  |  |  |     } catch { print(error.localizedDescription); } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-05 05:26:50 +00:00
										 |  |  | } |