This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (126 loc) · 4.03 KB
/
index.js
File metadata and controls
143 lines (126 loc) · 4.03 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const GoogleSpreadsheet = require("google-spreadsheet");
const Promise = require("bluebird");
let uncaughtExceptionCb = null;
// get a list of props
const getPropList = function getPropList(idcolumn, sheet, cells) {
const props = [];
for (let i = 0; i < sheet.colCount; i += 1) {
props.push(cells[i].value);
}
return props;
};
// get a list of row ids
const getIdList = function getIdList(idcolumn, props, sheet, cells) {
const ids = [];
// get the index of the id column
const idColNum = props.indexOf(idcolumn);
// get id list
for (let i = 1; i < sheet.rowCount; i += 1) {
ids.push(cells[i * sheet.colCount + idColNum].value);
}
return ids;
};
// cell grabber factory
const makeCellGrabber = function makeCellGrabber(idcolumn, sheet, cells) {
const props = getPropList(idcolumn, sheet, cells);
const ids = getIdList(idcolumn, props, sheet, cells);
// find any cell by (id, col)
return function getCell(id, prop) {
return cells[props.indexOf(prop) + (ids.indexOf(id) + 1) * sheet.colCount];
};
};
const DriveSpreadSheetSync = function constructor(data) {
Object.assign(
this,
{
id_column: "rid"
},
data
);
if (!this.spreadsheet) {
throw new Error("No Drive spreadsheet id provided");
}
this.doc = new GoogleSpreadsheet(this.spreadsheet);
};
DriveSpreadSheetSync.prototype.getSheetAndCells = async function getSheetAndCells() {
// auth
await Promise.promisify(this.doc.useServiceAccountAuth)(
this.service_account_credentials
);
// get info
this.doc.info = await Promise.promisify(this.doc.getInfo)();
// get sheet
const sheet = this.sheet
? this.doc.info.worksheets.find(s => s.title === this.sheet)
: this.doc.info.worksheets.sheet[0];
if (!sheet) {
throw new Error("No sheet found");
}
// get cells
const cells = await Promise.promisify(sheet.getCells)({
"min-row": 1,
"max-row": sheet.rowCount,
"return-empty": true
});
return { sheet, cells };
};
DriveSpreadSheetSync.prototype.read = function read(callback) {
return new Promise(async (resolve, reject) => {
uncaughtExceptionCb = callback || reject;
try {
const { sheet, cells } = await this.getSheetAndCells();
const props = getPropList(this.id_column, sheet, cells);
const res = cells.reduce((data, cell) => {
if (!cell.value || !(cell.row - 1) || !props[cell.col - 1]) {
return data;
}
const newdata = data.slice();
newdata[cell.row - 2] = newdata[cell.row - 2] || {};
newdata[cell.row - 2][props[cell.col - 1]] = cell.value;
return newdata;
}, []);
if (callback) callback(null, res);
resolve(res);
} catch (e) {
reject(e);
}
}).catch(e => (callback ? callback(e) : e));
};
DriveSpreadSheetSync.prototype.save = function save(data, callback) {
return new Promise(async (resolve, reject) => {
uncaughtExceptionCb = callback || reject;
try {
const { sheet, cells } = await this.getSheetAndCells();
const cellGrabber = makeCellGrabber(this.id_column, sheet, cells);
await Promise.map(data, row => {
const rowId = row[this.id_column];
const rowNumToUpdate =
cellGrabber(rowId, this.id_column) &&
cellGrabber(rowId, this.id_column).value === rowId &&
cellGrabber(rowId, this.id_column).row;
if (!rowNumToUpdate) {
return Promise.promisify(sheet.addRow)(row);
}
return Object.keys(row).map(key => {
const cellToUpdate = cellGrabber(rowId, key);
if (cellToUpdate) {
cellToUpdate.value = row[key];
}
return true;
});
})
.then(() => Promise.promisify(sheet.bulkUpdateCells)(cells))
.then(res => {
if (callback) callback(null, res);
resolve(res);
});
} catch (e) {
reject(e);
}
}).catch(e => (callback ? callback(e) : e));
};
process.on(
"uncaughtException",
error => (uncaughtExceptionCb ? uncaughtExceptionCb(error) : null)
);
module.exports = DriveSpreadSheetSync;