Skip to content

Commit

Permalink
demo refresh [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed Jan 27, 2018
1 parent c2ec755 commit edf7150
Show file tree
Hide file tree
Showing 30 changed files with 1,218 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .spelling
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ CommonJS
Ethercalc
ExtendScript
FileSaver
IndexedDB
JavaScriptCore
LocalStorage
NPM
Nuxt.js
Redis
RequireJS
Rollup
SessionStorage
SQLite
SystemJS
VueJS
WebSQL
iOS
nodejs
npm
Expand All @@ -57,10 +63,12 @@ ArrayBuffer
Base64
Booleans
JS
NoSQL
README
UTF-16
XHR
XMLHttpRequest
bundler
bundlers
cleanroom
config
Expand Down Expand Up @@ -95,6 +103,15 @@ ui-grid
- demos/angular2/README.md
angular-cli

- demos/database/README.md
LowDB
MariaDB
MySQL
PostgreSQL
schemaless
schemas
storages

- demos/extendscript/README.md
Photoshop
minifier
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ The [`demos` directory](demos/) includes sample projects for:
- [`vue 2.x and weex`](demos/vue/)
- [`XMLHttpRequest and fetch`](demos/xhr/)
- [`nodejs server`](demos/server/)
- [`databases and key/value stores`](demos/database/)

**Bundlers and Tooling**
- [`browserify`](demos/browserify/)
- [`fusebox`](demos/fusebox/)
- [`parcel`](demos/parcel/)
- [`requirejs`](demos/requirejs/)
- [`rollup`](demos/rollup/)
- [`systemjs`](demos/systemjs/)
Expand Down
3 changes: 3 additions & 0 deletions demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ can be installed with Bash on Windows or with `cygwin`.
- [`vue 2.x and weex`](vue/)
- [`XMLHttpRequest and fetch`](xhr/)
- [`nodejs server`](server/)
- [`databases and key/value stores`](database/)

**Bundlers and Tooling**
- [`browserify`](browserify/)
- [`fusebox`](fusebox/)
- [`parcel`](parcel/)
- [`requirejs`](requirejs/)
- [`rollup`](rollup/)
- [`systemjs`](systemjs/)
Expand Down
7 changes: 7 additions & 0 deletions demos/database/.eslintrc
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" ]
}
1 change: 1 addition & 0 deletions demos/database/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
59 changes: 59 additions & 0 deletions demos/database/LocalForage.html
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>
57 changes: 57 additions & 0 deletions demos/database/LocalStorage.html
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>
23 changes: 23 additions & 0 deletions demos/database/LowDBTest.js
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');
16 changes: 16 additions & 0 deletions demos/database/Makefile
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
70 changes: 70 additions & 0 deletions demos/database/MySQLTest.js
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);
})();
59 changes: 59 additions & 0 deletions demos/database/ObjUtils.js
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;
Loading

0 comments on commit edf7150

Please sign in to comment.