This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.95 KB
/
index.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
#!/usr/local/bin/node
'use strict';
const google_login = require('./lib/google-auth');
const GoogleSheetsHelper = require('./lib/GoogleSheetsClient');
const QuizletClient = require('./lib/QuizletClient');
const quizletClient = new QuizletClient('quizlet_credentials.json');
const Config = require('./lib/config');
const config = new Config('config.json');
async function copySheetToQuizlet(sheet, quizlet_set_id, sheetsClient) {
const rows = await sheetsClient.getRows(config.getSheetsDocumentId(), sheet, config.getColumns());
let wordList = mapWordList(rows);
if (wordList.length < 3) {
throw Error(`wordList must have at least 3 elements. Rows: ${rows}`);
}
let newData = {
definitions: wordList.map(i => i.definition),
terms: wordList.map(i => i.term),
};
return await quizletClient.createOrUpdateSet(quizlet_set_id,
config.getPrefix() + sheet.properties.title,
newData, config.getQuizletLangTerms(), config.getQuizletLangDefinitions(), config.getQuizletVisibility());
}
function mapWordList(rows) {
if (config.getSkipFirstRow()) {
rows.shift();
}
return rows.filter(row => row[0] && row[1])
.map(row => {
return {
term: row[0],
definition: row[1]
}
});
}
async function main() {
try {
const sheetsClient = new GoogleSheetsHelper(await google_login('google_client_secret.json'));
let sheetsList = await sheetsClient.getTabs(config.getSheetsDocumentId());
// TODO: run all sheets, but first it must be possible to save quizlet_set_id for each sheet
const firstSheet = sheetsList[0];
const new_quizlet_set_id = await copySheetToQuizlet(firstSheet, config.getQuizletSetId(), sheetsClient);
await config.setQuizletSetId(new_quizlet_set_id);
process.exit(0);
} catch
(reason) {
console.log('error', reason);
process.exit(1);
}
}
main();