-
Notifications
You must be signed in to change notification settings - Fork 2
/
google.ts
219 lines (194 loc) · 7.74 KB
/
google.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const fs = require('fs');
const readline = require('readline');
const { google } = require('googleapis');
import { DecisionTable } from './DecisionTable';
/*
* Decison Vaction Allowance Hit Policy Collect+
Variables Input Input Output Annotation
Variable YearsOfService EmployeeType Vacation Annotation
data type integer string integer
Rules:
1 - "Contract" 0
2 - 'Executive" 4
3 - 'Employee" 2
4 >10 in ['Employee','Executive'] 1 Additional Week for 10 years
5 >20 in ['Employee','Executive'] 1 Another week after 20 years
Tests 5 "Employee"
*/
class CSVReader {
static parseRule(rows) {
const dt = { name: null, hitPolicy: null, conditionVars: [], actionVars: [], rules: [] };
const tests = [];
let varTypes = null;
let c, inputStart, outputStart, annotationStart, hitPolicyPosition;
let r = 1;
let mode = '';
rows.forEach(row => {
// console.log(row)
if (row.length > 0) {
const title = row[0];
// console.log('row ' + r + " " + title + " mode: " + mode);
switch (title) {
case 'Decision':
for (c = 1; c < row.length; c++) {
if (row[c] == 'Hit Policy')
hitPolicyPosition = c;
}
mode = 'Decision';
break;
case 'Variables':
mode = 'VariableTypes';
break;
case 'Rules:':
mode = 'Rules';
break;
case 'Tests:':
mode = 'Tests';
break;
default:
switch (mode) {
case 'Decision':
dt.name = row[0];
dt.hitPolicy = row[hitPolicyPosition];
break;
case 'VariableTypes':
for (c = 1; c < row.length; c++) {
if (row[c] == 'Input')
inputStart = c;
else if (row[c] == 'Output')
outputStart = c;
else if (row[c] == 'Annotation')
annotationStart = c;
}
mode = 'Variables';
break;
case 'Variables':
for (c = inputStart; c < outputStart; c++) {
dt.conditionVars.push({ name: row[c] });
}
for (c = outputStart; c < annotationStart && c < row.length; c++) {
dt.actionVars.push({ name: row[c] });
}
mode = '';
break;
case 'Rules':
dt.rules.push(row);
break;
case 'Tests':
let i;
const record = {};
for (i = 0; i < dt.conditionVars.length; i++) {
const varName = dt.conditionVars[i].name;
record[varName] = CSVReader.trimParam(row[i + 1]);
}
record['__ID'] = r;
tests.push(record);
break;
}
break;
}
}
else
mode = '';
r++;
});
//console.log(dt);
// console.log(dt.tests);
return {decisionTable: dt, tests}
}
static trimParam(param) {
if (param.startsWith('"') && param.endsWith('"'))
return param.substring(1, param.length - 1);
if (param.startsWith("'") && param.endsWith("'"))
return param.substring(1, param.length - 1);
else
return param.trim();
}
}
export class GoogleSheets {
auth=null;
constructor() {
}
async init() {
let content = fs.readFileSync('credentials.json');
// if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Sheets API.
// authorize(JSON.parse(content), listMajors);
// authorize(JSON.parse(content), doIt);
let token = await this.authorizeAsync(JSON.parse(content));
this.auth=token;
}
async getRule(spreadsheetId, range) {
const rows = await this.getData(spreadsheetId, range);
// console.log(rows);
return CSVReader.parseRule(rows);
}
async getData(spreadsheetId, range) {
return new Promise((resolve, reject) => {
const sheets = google.sheets({ version: 'v4', auth: this.auth });
sheets.spreadsheets.values.get({
// spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms',
spreadsheetId: spreadsheetId,
range: range,
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
const rows = res.data.values;
resolve(rows);
let r = 1;
if (rows.length) {
// console.log('Range:' + range);
// Print columns A and E, which correspond to indices 0 and 4.
rows.map((row) => {
// console.log('Row: ', r++, row);
// console.log(`${row[0]}, ${row[4]}`);
});
} else {
console.log('No data found.');
}
});
});
}
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
async authorizeAsync(credentials) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
/*
return new Promise((resolve, reject) => {
...
resolve(data)
})
}); */
return new Promise((resolve, reject) => {
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) {
// return getNewToken(oAuth2Client, callback);
}
oAuth2Client.setCredentials(JSON.parse(token));
// callback(oAuth2Client);
resolve(oAuth2Client)
});
});
}
}
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
/*
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Sheets API.
// authorize(JSON.parse(content), listMajors);
authorize(JSON.parse(content), doIt);
});
*/