|
2 | 2 | import JavaScriptCore; |
3 | 3 |
|
4 | 4 | enum SJSError: Error { |
5 | | - case badJSContext; |
6 | | - case badJSWorkbook; |
7 | | - case badJSWorksheet; |
| 5 | + case badJSContext; |
| 6 | + case badJSWorkbook; |
| 7 | + case badJSWorksheet; |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | class SJSWorksheet { |
11 | | - var context: JSContext!; |
12 | | - var wb: JSValue!; var ws: JSValue!; |
13 | | - var idx: Int32; |
| 11 | + var context: JSContext!; |
| 12 | + var wb: JSValue!; var ws: JSValue!; |
| 13 | + var idx: Int32; |
14 | 14 |
|
15 | | - func toCSV() throws -> String { |
16 | | - let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); |
17 | | - let utils: JSValue! = XLSX.objectForKeyedSubscript("utils"); |
18 | | - let sheet_to_csv: JSValue! = utils.objectForKeyedSubscript("sheet_to_csv"); |
19 | | - return sheet_to_csv.call(withArguments: [ws]).toString(); |
20 | | - } |
| 15 | + func toCSV() throws -> String { |
| 16 | + let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); |
| 17 | + let utils: JSValue! = XLSX.objectForKeyedSubscript("utils"); |
| 18 | + let sheet_to_csv: JSValue! = utils.objectForKeyedSubscript("sheet_to_csv"); |
| 19 | + return sheet_to_csv.call(withArguments: [ws]).toString(); |
| 20 | + } |
21 | 21 |
|
22 | | - init(ctx: JSContext, workbook: JSValue, worksheet: JSValue, idx: Int32) throws { |
23 | | - self.context = ctx; self.wb = workbook; self.ws = worksheet; self.idx = idx; |
24 | | - } |
| 22 | + init(ctx: JSContext, workbook: JSValue, worksheet: JSValue, idx: Int32) throws { |
| 23 | + self.context = ctx; self.wb = workbook; self.ws = worksheet; self.idx = idx; |
| 24 | + } |
25 | 25 | } |
26 | 26 |
|
27 | 27 | class SJSWorkbook { |
28 | | - var context: JSContext!; |
29 | | - var wb: JSValue!; var SheetNames: JSValue!; var Sheets: JSValue!; |
| 28 | + var context: JSContext!; |
| 29 | + var wb: JSValue!; var SheetNames: JSValue!; var Sheets: JSValue!; |
30 | 30 |
|
31 | | - func getSheetAtIndex(idx: Int32) throws -> SJSWorksheet { |
32 | | - let SheetName: String = SheetNames.atIndex(Int(idx)).toString(); |
33 | | - let ws: JSValue! = Sheets.objectForKeyedSubscript(SheetName); |
34 | | - return try SJSWorksheet(ctx: context, workbook: wb, worksheet: ws, idx: idx); |
35 | | - } |
| 31 | + func getSheetAtIndex(idx: Int32) throws -> SJSWorksheet { |
| 32 | + let SheetName: String = SheetNames.atIndex(Int(idx)).toString(); |
| 33 | + let ws: JSValue! = Sheets.objectForKeyedSubscript(SheetName); |
| 34 | + return try SJSWorksheet(ctx: context, workbook: wb, worksheet: ws, idx: idx); |
| 35 | + } |
36 | 36 |
|
37 | | - func writeBStr(bookType: String = "xlsx") throws -> String { |
38 | | - let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); |
39 | | - context.evaluateScript(String(format: "var writeopts = {type:'binary', bookType:'%@'}", bookType)); |
40 | | - let writeopts: JSValue! = context.objectForKeyedSubscript("writeopts"); |
41 | | - let writefunc: JSValue! = XLSX.objectForKeyedSubscript("write"); |
42 | | - return writefunc.call(withArguments: [wb, writeopts]).toString(); |
43 | | - } |
| 37 | + func writeBStr(bookType: String = "xlsx") throws -> String { |
| 38 | + let XLSX: JSValue! = context.objectForKeyedSubscript("XLSX"); |
| 39 | + context.evaluateScript(String(format: "var writeopts = {type:'binary', bookType:'%@'}", bookType)); |
| 40 | + let writeopts: JSValue! = context.objectForKeyedSubscript("writeopts"); |
| 41 | + let writefunc: JSValue! = XLSX.objectForKeyedSubscript("write"); |
| 42 | + return writefunc.call(withArguments: [wb, writeopts]).toString(); |
| 43 | + } |
44 | 44 |
|
45 | | - init(ctx: JSContext, wb: JSValue) throws { |
46 | | - self.context = ctx; |
47 | | - self.wb = wb; |
48 | | - self.SheetNames = wb.objectForKeyedSubscript("SheetNames"); |
49 | | - self.Sheets = wb.objectForKeyedSubscript("Sheets"); |
50 | | - } |
| 45 | + init(ctx: JSContext, wb: JSValue) throws { |
| 46 | + self.context = ctx; |
| 47 | + self.wb = wb; |
| 48 | + self.SheetNames = wb.objectForKeyedSubscript("SheetNames"); |
| 49 | + self.Sheets = wb.objectForKeyedSubscript("Sheets"); |
| 50 | + } |
51 | 51 | } |
52 | 52 |
|
53 | 53 | class SheetJSCore { |
54 | | - var context: JSContext!; |
55 | | - var XLSX: JSValue!; |
| 54 | + var context: JSContext!; |
| 55 | + var XLSX: JSValue!; |
56 | 56 |
|
57 | | - func init_context() throws -> JSContext { |
58 | | - var context: JSContext! |
59 | | - do { |
60 | | - context = JSContext(); |
61 | | - context.exceptionHandler = { ctx, X in if let e = X { print(e.toString()); }; }; |
62 | | - context.evaluateScript("var global = (function(){ return this; }).call(null);"); |
63 | | - context.evaluateScript("if(typeof wbs == 'undefined') wbs = [];"); |
64 | | - let src = try String(contentsOfFile: "xlsx.full.min.js"); |
65 | | - context.evaluateScript(src); |
66 | | - if context != nil { return context!; } |
67 | | - } catch { print(error.localizedDescription); } |
68 | | - throw SJSError.badJSContext; |
69 | | - } |
| 57 | + func init_context() throws -> JSContext { |
| 58 | + var context: JSContext! |
| 59 | + do { |
| 60 | + context = JSContext(); |
| 61 | + context.exceptionHandler = { ctx, X in if let e = X { print(e.toString()); }; }; |
| 62 | + context.evaluateScript("var global = (function(){ return this; }).call(null);"); |
| 63 | + context.evaluateScript("if(typeof wbs == 'undefined') wbs = [];"); |
| 64 | + let src = try String(contentsOfFile: "xlsx.full.min.js"); |
| 65 | + context.evaluateScript(src); |
| 66 | + if context != nil { return context!; } |
| 67 | + } catch { print(error.localizedDescription); } |
| 68 | + throw SJSError.badJSContext; |
| 69 | + } |
70 | 70 |
|
71 | | - func version() throws -> String { |
72 | | - if let version = XLSX.objectForKeyedSubscript("version") { return version.toString(); } |
73 | | - throw SJSError.badJSContext; |
74 | | - } |
| 71 | + func version() throws -> String { |
| 72 | + if let version = XLSX.objectForKeyedSubscript("version") { return version.toString(); } |
| 73 | + throw SJSError.badJSContext; |
| 74 | + } |
75 | 75 |
|
76 | | - func readFile(file: String) throws -> SJSWorkbook { |
77 | | - let data: String! = try String(contentsOfFile: file, encoding: String.Encoding.isoLatin1); |
78 | | - return try readBStr(data: data); |
79 | | - } |
| 76 | + func readFile(file: String) throws -> SJSWorkbook { |
| 77 | + let data: String! = try String(contentsOfFile: file, encoding: String.Encoding.isoLatin1); |
| 78 | + return try readBStr(data: data); |
| 79 | + } |
80 | 80 |
|
81 | | - func readBStr(data: String) throws -> SJSWorkbook { |
82 | | - context.setObject(data, forKeyedSubscript: "payload" as (NSCopying & NSObjectProtocol)!); |
83 | | - context.evaluateScript("var wb = XLSX.read(payload, {type:'binary'});"); |
84 | | - let wb: JSValue! = context.objectForKeyedSubscript("wb"); |
85 | | - if wb == nil { throw SJSError.badJSWorkbook; } |
86 | | - return try SJSWorkbook(ctx: context, wb: wb); |
87 | | - } |
| 81 | + func readBStr(data: String) throws -> SJSWorkbook { |
| 82 | + context.setObject(data, forKeyedSubscript: "payload" as (NSCopying & NSObjectProtocol)!); |
| 83 | + context.evaluateScript("var wb = XLSX.read(payload, {type:'binary'});"); |
| 84 | + let wb: JSValue! = context.objectForKeyedSubscript("wb"); |
| 85 | + if wb == nil { throw SJSError.badJSWorkbook; } |
| 86 | + return try SJSWorkbook(ctx: context, wb: wb); |
| 87 | + } |
88 | 88 |
|
89 | | - init() throws { |
90 | | - do { |
91 | | - self.context = try init_context(); |
92 | | - self.XLSX = self.context.objectForKeyedSubscript("XLSX"); |
93 | | - if self.XLSX == nil { throw SJSError.badJSContext; } |
94 | | - } catch { print(error.localizedDescription); } |
95 | | - } |
| 89 | + init() throws { |
| 90 | + do { |
| 91 | + self.context = try init_context(); |
| 92 | + self.XLSX = self.context.objectForKeyedSubscript("XLSX"); |
| 93 | + if self.XLSX == nil { throw SJSError.badJSContext; } |
| 94 | + } catch { print(error.localizedDescription); } |
| 95 | + } |
96 | 96 | } |
0 commit comments