2017-09-05 05:26:50 +00:00
|
|
|
/* 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 */
|
2018-02-14 20:06:35 +00:00
|
|
|
s = new Scanner(SheetJS.class.getResourceAsStream("/xlsx.full.min.js")).useDelimiter("\\Z").next();
|
2017-09-05 05:26:50 +00:00
|
|
|
//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 */
|
2018-02-14 20:06:35 +00:00
|
|
|
NativeObject q = (NativeObject)this.cx.evaluateString(this.scope, "q = {'type':'binary', 'WTF':1};", "<cmd>", 2, null);
|
2017-09-05 05:26:50 +00:00
|
|
|
|
|
|
|
/* set up function arguments */
|
2018-02-14 20:06:35 +00:00
|
|
|
Object args[] = {d, q};
|
2017-09-05 05:26:50 +00:00
|
|
|
|
|
|
|
/* call read -> wb workbook */
|
|
|
|
Function readfunc = (Function)JSHelper.get_object("XLSX.read",this.scope);
|
2018-02-14 20:06:35 +00:00
|
|
|
NativeObject wb = (NativeObject)readfunc.call(this.cx, this.scope, this.nXLSX, args);
|
2017-09-05 05:26:50 +00:00
|
|
|
|
|
|
|
return new SheetJSFile(wb, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void close() { JSHelper.close(); }
|
|
|
|
}
|
|
|
|
|