forked from SheetJS/sheetjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDBTest.js
54 lines (46 loc) · 1.57 KB
/
MongoDBTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* eslint-env node */
/* global Promise */
const XLSX = require('xlsx');
const assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/sheetjs';
const db_name = 'sheetjs';
/* make new workbook object from CSV */
const wb = XLSX.read('a,b,c\n1,2,3', {type:"binary", raw:true});
let P = Promise.resolve("sheetjs");
/* Connect to mongodb server and initialize collection */
P = P.then(async () => {
const client = await MongoClient.connect(url);
const db = client.db(db_name);
try { await db.collection('wb').drop(); } catch(e) {}
const coll = db.collection('wb');
return [client, coll];
});
/* Insert entire workbook object as a document */
P = P.then(async ([client, coll]) => {
const res = await coll.insertOne(wb);
assert.equal(res.insertedCount, 1);
return [client, coll];
});
/* Change cell A1 of Sheet1 to "J" and change A2 to 5 */
P = P.then(async ([client, coll]) => {
const res = await coll.updateOne({}, { $set: {
"Sheets.Sheet1.A1": {"t": "s", "v": "J"},
"Sheets.Sheet1.A2": {"t": "n", "v": 5},
}});
assert.equal(res.matchedCount, 1);
assert.equal(res.modifiedCount, 1);
return [client, coll];
});
/* Write to file */
P = P.then(async ([client, coll]) => {
const res = await coll.find({}).toArray();
const wb = res[0];
XLSX.writeFile(wb, "mongo.xlsx");
const ws = XLSX.readFile("mongo.xlsx").Sheets.Sheet1;
console.log(XLSX.utils.sheet_to_csv(ws));
return [client, coll];
});
/* Close connection */
P.then(async ([client]) => { client.close(); });