-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2ec755
commit edf7150
Showing
30 changed files
with
1,218 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"env": { "shared-node-browser":true }, | ||
"parserOptions": { | ||
"ecmaVersion": 2017 | ||
}, | ||
"plugins": [ "html", "json" ] | ||
} |
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 @@ | ||
*.db |
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,59 @@ | ||
<!DOCTYPE html> | ||
<!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com --> | ||
<!-- vim: set ts=2: --> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
<title>SheetJS Live Demo</title> | ||
<style> | ||
a { text-decoration: none } | ||
</style> | ||
</head> | ||
<body> | ||
<pre> | ||
<b><a href="http://sheetjs.com">SheetJS LocalStorage Demo</a></b> | ||
<pre id="data_"> | ||
Original Data: | ||
</pre> | ||
<pre id="out"> | ||
Output: | ||
|
||
</pre> | ||
<script src="xlsx.full.min.js"></script> | ||
<script src="ObjUtils.js"></script> | ||
<script src="https://unpkg.com/localforage/dist/localforage.min.js"></script> | ||
<script src="SheetJSForage.js"></script> | ||
<script> | ||
/* eslint-env browser */ | ||
/*global XLSX, localforage */ | ||
var data = { | ||
"title": "SheetDB", | ||
"metadata": { | ||
"author": "SheetJS", | ||
"code": 7262 | ||
}, | ||
"data": [ | ||
{ "Name": "Barack Obama", "Index": 44 }, | ||
{ "Name": "Donald Trump", "Index": 45 }, | ||
] | ||
}; | ||
document.getElementById("data_").innerText += JSON.stringify(data, 2, 2); | ||
|
||
localforage.setDriver(localforage.INDEXEDDB); | ||
(async function() { | ||
await localforage.clear(); | ||
|
||
await localforage.load(data); | ||
var wb = await localforage.dump(); | ||
console.log(wb); | ||
|
||
var OUT = document.getElementById("out"); | ||
wb.SheetNames.forEach(function(n, i) { | ||
OUT.innerText += "Sheet " + i + " (" + n + ")\n"; | ||
OUT.innerText += XLSX.utils.sheet_to_csv(wb.Sheets[n]); | ||
OUT.innerText += "\n"; | ||
}); | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
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,57 @@ | ||
<!DOCTYPE html> | ||
<!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com --> | ||
<!-- vim: set ts=2: --> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
<title>SheetJS Live Demo</title> | ||
<style> | ||
a { text-decoration: none } | ||
</style> | ||
</head> | ||
<body> | ||
<pre> | ||
<b><a href="http://sheetjs.com">SheetJS LocalStorage Demo</a></b> | ||
<pre id="data_"> | ||
Original Data: | ||
</pre> | ||
<pre id="out"> | ||
Output: | ||
|
||
</pre> | ||
<script src="xlsx.full.min.js"></script> | ||
<script src="ObjUtils.js"></script> | ||
<script src="SheetJSStorage.js"></script> | ||
<script> | ||
/* eslint-env browser */ | ||
/*global XLSX */ | ||
var data = { | ||
"title": "SheetDB", | ||
"metadata": { | ||
"author": "SheetJS", | ||
"code": 7262 | ||
}, | ||
"data": [ | ||
{ "Name": "Barack Obama", "Index": 44 }, | ||
{ "Name": "Donald Trump", "Index": 45 }, | ||
] | ||
}; | ||
document.getElementById("data_").innerText += JSON.stringify(data, 2, 2); | ||
|
||
(function() { | ||
localStorage.clear(); | ||
|
||
localStorage.load(data); | ||
var wb = localStorage.dump(); | ||
console.log(wb); | ||
|
||
var OUT = document.getElementById("out"); | ||
wb.SheetNames.forEach(function(n, i) { | ||
OUT.innerText += "Sheet " + i + " (" + n + ")\n"; | ||
OUT.innerText += XLSX.utils.sheet_to_csv(wb.Sheets[n]); | ||
OUT.innerText += "\n"; | ||
}); | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
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,23 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* eslint-env node */ | ||
var low = require('lowdb'); | ||
var SheetJSAdapter = require('./SheetJSLowDB'); | ||
var adapter = new SheetJSAdapter(); | ||
var db = low(adapter); | ||
|
||
db.defaults({ posts: [], user: {}, count: 0 }).write(); | ||
db.get('posts').push({ id: 1, title: 'lowdb is awesome'}).write(); | ||
db.set('user.name', 'typicode').write(); | ||
db.update('count', function(n) { return n + 1; }).write(); | ||
|
||
adapter.dumpFile('ldb1.xlsx'); | ||
|
||
var adapter2 = new SheetJSAdapter(); | ||
adapter2.loadFile('ldb1.xlsx'); | ||
var db2 = low(adapter2); | ||
|
||
db2.get('posts').push({ id: 2, title: 'mongodb is not'}).write(); | ||
db2.set('user.name', 'sheetjs').write(); | ||
db2.update('count', function(n) { return n + 1; }).write(); | ||
|
||
adapter2.dumpFile('ldb2.xlsx'); |
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,16 @@ | ||
.PHONY: init | ||
init: | ||
rm -f node_modules/xlsx | ||
mkdir -p node_modules | ||
cd node_modules; ln -s ../../../ xlsx; cd - | ||
rm -f xlsx.full.min.js | ||
ln -s ../../dist/xlsx.full.min.js | ||
|
||
FILES=$(filter-out xlsx.full.min.js,$(wildcard *.js)) $(wildcard *.html) | ||
.PHONY: lint | ||
lint: $(FILES) | ||
eslint $(FILES) | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f *.db *.xlsx |
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,70 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/* eslint-env node */ | ||
var XLSX = require('xlsx'); | ||
var assert = require('assert'); | ||
var SheetJSSQL = require('./SheetJSSQL'); | ||
var mysql = require('mysql2/promise'); | ||
|
||
/* Connection options (requires two databases sheetjs and sheetj5) */ | ||
var opts = { | ||
host : 'localhost', | ||
user : 'SheetJS', | ||
password : 'SheetJS', | ||
}; | ||
|
||
/* Sample data table */ | ||
var init = [ | ||
"DROP TABLE IF EXISTS pres", | ||
"CREATE TABLE pres (name TEXT, idx TINYINT)", | ||
"INSERT INTO pres VALUES ('Barack Obama', 44)", | ||
"INSERT INTO pres VALUES ('Donald Trump', 45)", | ||
"DROP TABLE IF EXISTS fmts", | ||
"CREATE TABLE fmts (ext TEXT, ctr TEXT, multi TINYINT)", | ||
"INSERT INTO fmts VALUES ('XLSB', 'ZIP', 1)", | ||
"INSERT INTO fmts VALUES ('XLS', 'CFB', 1)", | ||
"INSERT INTO fmts VALUES ('XLML', '', 1)", | ||
"INSERT INTO fmts VALUES ('CSV', '', 0)", | ||
]; | ||
|
||
(async () => { | ||
const conn1 = await mysql.createConnection({...opts, database: "sheetjs"}); | ||
for(var i = 0; i < init.length; ++i) await conn1.query(init[i]); | ||
|
||
/* Export table to XLSX */ | ||
var wb = XLSX.utils.book_new(); | ||
|
||
async function book_append_table(wb, name) { | ||
var r_f = await conn1.query('SELECT * FROM ' + name); | ||
var r = r_f[0]; | ||
var ws = XLSX.utils.json_to_sheet(r); | ||
XLSX.utils.book_append_sheet(wb, ws, name); | ||
} | ||
|
||
await book_append_table(wb, "pres"); | ||
await book_append_table(wb, "fmts"); | ||
XLSX.writeFile(wb, "mysql.xlsx"); | ||
|
||
/* Capture first database info and close */ | ||
var P1 = (await conn1.query("SELECT * FROM pres"))[0]; | ||
var F1 = (await conn1.query("SELECT * FROM fmts"))[0]; | ||
await conn1.close(); | ||
|
||
/* Import XLSX to table */ | ||
const conn2 = await mysql.createConnection({...opts, database: "sheetj5"}); | ||
var wb2 = XLSX.readFile("mysql.xlsx"); | ||
var queries = SheetJSSQL.book_to_sql(wb2, "MYSQL"); | ||
for(i = 0; i < queries.length; ++i) await conn2.query(queries[i]); | ||
|
||
/* Capture first database info and close */ | ||
var P2 = (await conn2.query("SELECT * FROM pres"))[0]; | ||
var F2 = (await conn2.query("SELECT * FROM fmts"))[0]; | ||
await conn2.close(); | ||
|
||
/* Compare results */ | ||
assert.deepEqual(P1, P2); | ||
assert.deepEqual(F1, F2); | ||
|
||
/* Display results */ | ||
console.log(P2); | ||
console.log(F2); | ||
})(); |
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,59 @@ | ||
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ | ||
/*global XLSX, module, require */ | ||
var ObjUtils = (function() { | ||
|
||
var X; | ||
if(typeof XLSX !== "undefined") X = XLSX; | ||
else if(typeof require !== 'undefined') X = require('xlsx'); | ||
else throw new Error("Could not find XLSX"); | ||
|
||
function walk(obj, key, arr) { | ||
if(Array.isArray(obj)) return; | ||
if(typeof obj != "object" || obj instanceof Date) { arr.push({path:key, value:obj}); return; } | ||
Object.keys(obj).forEach(function(k) { | ||
walk(obj[k], key ? key + "." + k : k, arr); | ||
}); | ||
} | ||
|
||
function object_to_workbook(obj) { | ||
var wb = X.utils.book_new(); | ||
|
||
var base = []; walk(obj, "", base); | ||
var ws = X.utils.json_to_sheet(base, {header:["path", "value"]}); | ||
X.utils.book_append_sheet(wb, ws, "_keys"); | ||
|
||
Object.keys(obj).forEach(function(k) { | ||
if(!Array.isArray(obj[k])) return; | ||
X.utils.book_append_sheet(wb, X.utils.json_to_sheet(obj[k]), k); | ||
}); | ||
|
||
return wb; | ||
} | ||
|
||
function deepset(obj, path, value) { | ||
if(path.indexOf(".") == -1) return obj[path] = value; | ||
var parts = path.split("."); | ||
if(!obj[parts[0]]) obj[parts[0]] = {}; | ||
return deepset(obj[parts[0]], parts.slice(1).join("."), value); | ||
} | ||
function workbook_set_object(obj, wb) { | ||
var ws = wb.Sheets["_keys"]; if(ws) { | ||
var data = X.utils.sheet_to_json(ws, {raw:true}); | ||
data.forEach(function(r) { deepset(obj, r.path, r.value); }); | ||
} | ||
wb.SheetNames.forEach(function(n) { | ||
if(n == "_keys") return; | ||
obj[n] = X.utils.sheet_to_json(wb.Sheets[n], {raw:true}); | ||
}); | ||
} | ||
|
||
function workbook_to_object(wb) { var obj = {}; workbook_set_object(obj, wb); return obj; } | ||
|
||
return { | ||
workbook_to_object: workbook_to_object, | ||
object_to_workbook: object_to_workbook, | ||
workbook_set_object: workbook_set_object | ||
}; | ||
})(); | ||
|
||
if(typeof module !== 'undefined') module.exports = ObjUtils; |
Oops, something went wrong.