-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-spreadsheet-control-runner.js
More file actions
93 lines (86 loc) · 3.84 KB
/
Copy pathgoogle-spreadsheet-control-runner.js
File metadata and controls
93 lines (86 loc) · 3.84 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
const async = require('async');
const GoogleSpreadsheet = require('google-spreadsheet');
function isRowProcessable(row) {
return row.progress === 'NOT_STARTED';
}
function fallbackRowProcessor(row) {
return (step) => {
console.log(`Skipping row: ${row.id}`);
if (row.progress === 'NOT_STARTED') {
row.progress = 'SKIPPED';
row.save();
}
step(null, `Skipped row: ${row.id}`);
}
}
function getControlSteps(spreadsheetUUID, GoogleCreds, startingRow, numberOfRows, providedRowProcessor) {
const doc = new GoogleSpreadsheet(spreadsheetUUID);
return [
(step) => doc.useServiceAccountAuth(GoogleCreds, step),
(step) => doc.getInfo((error, info) => {
console.log(`Loaded doc: ${info.title}`);
console.log(`Number of worksheets: ${info.worksheets.length}. Only processing first worksheet.`);
const sheet = info.worksheets[0];
console.log(`Sheet 1: ${sheet.title} ${sheet.rowCount}x${sheet.colCount}`);
step(null, sheet);
}),
(sheet, step) => {
sheet.getRows({
offset: startingRow,
limit: numberOfRows
}, (error, rows) => {
console.log(`Processing ${rows.length} rows`);
async.series(rows.map(row => isRowProcessable(row) ? providedRowProcessor(row) : fallbackRowProcessor(row)), step);
});
},
(results, step) => {
console.log(`Processed ${results.length} rows`);
step(null, results);
}
];
}
module.exports = (spreadsheetUUID, rowProcessor, errorResultsCallback, startingRow, numberOfRows, googleCredsLocation) => {
let allGood = true;
console.log('...INITIALISING GOOGLE SPREADSHEET CONTROL RUNNER...');
if (!spreadsheetUUID) {
console.error("Please provide a Google Spreadsheet ID as the first argument. The first worksheet must have a column " +
"labelled 'progress'. For a row to be processed, the 'progress' cell must have a value of 'NOT_STARTED'");
allGood = false;
}
if (!rowProcessor) {
console.error("Please provide a row processing curried function as the second argument. It should take a row and return an async.js function to be executed in time which actually processes the row.");
allGood = false;
}
if (!errorResultsCallback) {
console.error("Please provide a callback function (error, results) as the third argument to receive notification when the processing is complete.");
allGood = false;
}
if (!startingRow) {
console.warn("Starting at row 1. To start at a different row, please provide a positive integer as the fourth argument.");
startingRow = 1;
}
if (!numberOfRows) {
console.warn("Will process 1 row only. To process more rows, please provide a positive integer as the fifth argument.");
numberOfRows = 1;
}
if (!googleCredsLocation) {
console.warn("Reading Google credentials from: './google-creds.json'. Please provide an alternative path as the sixth and final argument.");
googleCredsLocation = './google-creds.json';
}
const GoogleCreds = require(googleCredsLocation);
if (!GoogleCreds) {
allGood = false;
}
if (allGood) {
console.log('...PROCESSING GOOGLE SPREADSHEET...');
const GoogleCreds = require(googleCredsLocation);
const steps = getControlSteps(spreadsheetUUID, GoogleCreds, startingRow, numberOfRows, rowProcessor);
async.waterfall(steps, (error, results) => {
console.log('...PROCESSING COMPLETE. CALLING BACK...');
errorResultsCallback(error, results);
});
console.log('...INTIALISATION COMPLETE. PLEASE WAIT FOR CALLBACK.');
} else {
console.log('GOOGLE SPREADSHEET CONTROL RUNNER FINISHED.');
}
}