-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsheets.js
84 lines (62 loc) · 2.32 KB
/
sheets.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
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
const GoogleSpreadsheet = require('google-spreadsheet');
const { sheets: { sheetID, credentials } } = require('./credentials.json');
// flat returns an array after flattening all the
const flat = arr => arr.reduce((res, it) => res.concat(Array.isArray(it) ? flat(it) : it), []);
// sumUsing calculates sums up the values prodvided by transform function applied
// each element of the given array
const sumUsing = (arr, fn) => arr.reduce((sum, it) => sum + fn(it), 0);
// associate Returns a Map containing key-value pairs provided by
// transform function applied to elements of the given array.
const associate = (arr, fn) => arr.reduce((m, it) => m.set(fn(it), it), new Map());
// numberOfHighLights counts the total number of highlights in an array of books
const getNumberOfHighLights = books => sumUsing(books, it => it.highlights.length);
// createRowColumnIndexGrid takes an array of cells and return a map of each cell
// mapped agaist the its rowNumber-columnNumber
const createRowColumnIndexGrid = cells => associate(cells, it => `${it.row}-${it.col}`);
function insertHighlights(sheet, books) {
const numberOfHighLights = getNumberOfHighLights(books);
sheet.getCells({
'min-row': 2,
'max-row': 1 + numberOfHighLights,
'min-col': 1,
'max-col': 3,
'return-empty': true,
}, (err, cells) => {
if (err) {
console.log(err);
return;
}
const cellIndex = createRowColumnIndexGrid(cells, numberOfHighLights, 3);
let curPos = 0;
for (let i = 0; i < books.length; i += 1) {
const book = books[i];
for (let j = 0; j < book.highlights.length; j += 1) {
cellIndex.get(`${curPos + 2}-1`).value = book.title;
cellIndex.get(`${curPos + 2}-2`).value = book.author;
cellIndex.get(`${curPos + 2}-3`).value = book.highlights[j];
curPos += 1;
}
}
sheet.bulkUpdateCells(Array.from(cellIndex.values()), (err) => {
if (err) {
console.log(err);
}
});
});
}
module.exports = function insertToSheet(books) {
const doc = new GoogleSpreadsheet(sheetID);
doc.useServiceAccountAuth(credentials, (err) => {
if (err) {
console.log(err);
return;
}
doc.getInfo((err, info) => {
if (err) {
console.log(err);
return;
}
insertHighlights(info.worksheets[0], books);
});
});
};