forked from SheetJS/sheetjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Math.LOG2E precision issue + new demos [ci skip]
- swift + jsc - java + rhino - XMLHttpRequest and friends
- Loading branch information
1 parent
e34b6e7
commit ad47cb4
Showing
47 changed files
with
822 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
jvm-npm.js | ||
sheetjs.* | ||
*.class | ||
*.jar | ||
rhino | ||
xlsx.swift.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env xcrun swift | ||
|
||
import JavaScriptCore; | ||
|
||
class SheetJS { | ||
var context: JSContext!; | ||
var XLSX: JSValue!; | ||
|
||
enum SJSError: Error { | ||
case badJSContext; | ||
}; | ||
|
||
func init_context() throws -> JSContext { | ||
var context: JSContext! | ||
do { | ||
context = JSContext(); | ||
context.exceptionHandler = { ctx, X in if let e = X { print(e.toString()); }; } | ||
var src = "var global = (function(){ return this; }).call(null);"; | ||
context.evaluateScript(src); | ||
src = try String(contentsOfFile: "xlsx.swift.js"); | ||
context.evaluateScript(src); | ||
if context != nil { return context!; } | ||
} catch { print(error.localizedDescription); } | ||
throw SheetJS.SJSError.badJSContext; | ||
} | ||
|
||
func version() throws -> String { | ||
if let version = XLSX.objectForKeyedSubscript("version") { return version.toString(); } | ||
throw SheetJS.SJSError.badJSContext; | ||
} | ||
|
||
func readFileToCSV(file: String) throws -> String { | ||
let data:String! = try String(contentsOfFile: file, encoding:String.Encoding.ascii); | ||
self.context.setObject(data, forKeyedSubscript:"payload" as (NSCopying & NSObjectProtocol)!); | ||
|
||
let src = [ | ||
"var wb = XLSX.read(payload, {type:'binary'});", | ||
"var ws = wb.Sheets[wb.SheetNames[0]];", | ||
"var result = XLSX.utils.sheet_to_csv(ws);" | ||
].joined(separator: "\n"); | ||
self.context.evaluateScript(src); | ||
|
||
return context.objectForKeyedSubscript("result").toString(); | ||
} | ||
|
||
init() throws { | ||
do { | ||
self.context = try init_context(); | ||
self.XLSX = context.objectForKeyedSubscript("XLSX"); | ||
if self.XLSX == nil { | ||
throw SheetJS.SJSError.badJSContext; | ||
} | ||
} catch { print(error.localizedDescription); } | ||
} | ||
} | ||
|
||
let sheetjs = try SheetJS(); | ||
try print(sheetjs.version()); | ||
try print(sheetjs.readFileToCSV(file:"sheetjs.xlsx")); | ||
try print(sheetjs.readFileToCSV(file:"sheetjs.xlsb")); | ||
try print(sheetjs.readFileToCSV(file:"sheetjs.xls")); | ||
try print(sheetjs.readFileToCSV(file:"sheetjs.xml.xls")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* vim: set ts=2: */ | ||
import com.sheetjs.SheetJS; | ||
import com.sheetjs.SheetJSFile; | ||
import com.sheetjs.SheetJSSheet; | ||
|
||
public class SheetJSRhino { | ||
public static void main(String args[]) throws Exception { | ||
try { | ||
SheetJS sjs = new SheetJS(); | ||
|
||
/* open file */ | ||
SheetJSFile xl = sjs.read_file(args[0]); | ||
|
||
/* get sheetnames */ | ||
String[] sheetnames = xl.get_sheet_names(); | ||
System.err.println(sheetnames[0]); | ||
|
||
/* convert to CSV */ | ||
SheetJSSheet sheet = xl.get_sheet(0); | ||
String csv = sheet.get_csv(); | ||
|
||
System.out.println(csv); | ||
|
||
} catch(Exception e) { | ||
throw e; | ||
} finally { | ||
SheetJS.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* vim: set ts=2: */ | ||
package com.sheetjs; | ||
|
||
import java.lang.Integer; | ||
import java.lang.StringBuilder; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.NativeArray; | ||
import org.mozilla.javascript.NativeObject; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
public class JSHelper { | ||
static String read_file(String file) throws IOException { | ||
byte[] b = Files.readAllBytes(Paths.get(file)); | ||
System.out.println(b.length); | ||
StringBuilder sb = new StringBuilder(); | ||
for(int i = 0; i < b.length; ++i) sb.append(Character.toString((char)b[i])); | ||
return sb.toString(); | ||
} | ||
|
||
static Object get_object(String path, Object base) throws ObjectNotFoundException { | ||
int idx = path.indexOf("."); | ||
Scriptable b = (Scriptable)base; | ||
if(idx == -1) return b.get(path, b); | ||
Object o = b.get(path.substring(0,idx), b); | ||
if(o == Scriptable.NOT_FOUND) throw new ObjectNotFoundException("not found: |" + path.substring(0,idx) + "|" + Integer.toString(idx)); | ||
return get_object(path.substring(idx+1), (NativeObject)o); | ||
} | ||
|
||
static Object[] get_array(String path, Object base) throws ObjectNotFoundException { | ||
NativeArray arr = (NativeArray)get_object(path, base); | ||
Object[] out = new Object[(int)arr.getLength()]; | ||
int idx; | ||
for(Object o : arr.getIds()) out[idx = (Integer)o] = arr.get(idx, arr); | ||
return out; | ||
} | ||
|
||
static String[] get_string_array(String path, Object base) throws ObjectNotFoundException { | ||
NativeArray arr = (NativeArray)get_object(path, base); | ||
String[] out = new String[(int)arr.getLength()]; | ||
int idx; | ||
for(Object o : arr.getIds()) out[idx = (Integer)o] = arr.get(idx, arr).toString(); | ||
return out; | ||
} | ||
|
||
public static void close() { Context.exit(); } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* vim: set ts=2: */ | ||
package com.sheetjs; | ||
|
||
import java.lang.Exception; | ||
|
||
public class ObjectNotFoundException extends Exception { | ||
public ObjectNotFoundException() {} | ||
public ObjectNotFoundException(String message) { super(message); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* vim: set ts=2: */ | ||
package com.sheetjs; | ||
|
||
import java.lang.Integer; | ||
import java.util.Scanner; | ||
import java.io.IOException; | ||
import java.io.File; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.Function; | ||
import org.mozilla.javascript.NativeObject; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
public class SheetJS { | ||
public Scriptable scope; | ||
public Context cx; | ||
public NativeObject nXLSX; | ||
|
||
public SheetJS() throws Exception { | ||
this.cx = Context.enter(); | ||
this.scope = this.cx.initStandardObjects(); | ||
|
||
/* boilerplate */ | ||
cx.setOptimizationLevel(-1); | ||
String s = "var global = (function(){ return this; }).call(null);"; | ||
cx.evaluateString(scope, s, "<cmd>", 1, null); | ||
|
||
/* eval library */ | ||
s = new Scanner(SheetJS.class.getResourceAsStream("/dist/xlsx.full.min.js")).useDelimiter("\\Z").next(); | ||
//s = new Scanner(new File("xlsx.full.min.js")).useDelimiter("\\Z").next(); | ||
cx.evaluateString(scope, s, "<cmd>", 1, null); | ||
|
||
/* grab XLSX variable */ | ||
Object XLSX = scope.get("XLSX", scope); | ||
if(XLSX == Scriptable.NOT_FOUND) throw new Exception("XLSX not found"); | ||
this.nXLSX = (NativeObject)XLSX; | ||
} | ||
|
||
public SheetJSFile read_file(String filename) throws IOException, ObjectNotFoundException { | ||
/* open file */ | ||
String d = JSHelper.read_file(filename); | ||
|
||
/* options argument */ | ||
NativeObject q = (NativeObject)this.cx.evaluateString(this.scope, "q = {'type':'binary'};", "<cmd>", 2, null); | ||
|
||
/* set up function arguments */ | ||
Object functionArgs[] = {d, q}; | ||
|
||
/* call read -> wb workbook */ | ||
Function readfunc = (Function)JSHelper.get_object("XLSX.read",this.scope); | ||
NativeObject wb = (NativeObject)readfunc.call(this.cx, this.scope, this.nXLSX, functionArgs); | ||
|
||
return new SheetJSFile(wb, this); | ||
} | ||
|
||
public static void close() { JSHelper.close(); } | ||
} | ||
|
Oops, something went wrong.