-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (59 loc) · 1.85 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
63
64
const functions = require('@google-cloud/functions-framework');
const {google} = require("googleapis");
const gdoctableapp = require('./table_api');
/**
* Responds to an HTTP request using data from the request body parsed according
* to the "content-type" header.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
functions.http('createContentPlanDoc', async (req, res) => {
const auth = await google.auth.getClient({
scopes: [
"https://www.googleapis.com/auth/documents"
]
});
const table = {
auth: auth,
documentId: req.body.tableDocID,
tableIndex: 0
};
gdoctableapp.GetValues(table, function(err, result) {
if (err) {
console.log(err);
return res.status(500).send(err);
}
console.log(result.values);
const regex = /{{(.*?)}}/;
const newValues = result.values.map(v => {
const match = v[1].match(regex);
console.log(match);
if(!match) {
return v;
}
const incoming = req.body[match[1]];
console.log(incoming);
if(!incoming) {
return [v[0], v[1].replace(regex, "")];
}
return [v[0], v[1].replace(regex, incoming)];
})
const resource = {
auth: auth,
documentId: req.body.outlineDocID,
rows: result.values.length,
columns: 2,
append: true,
values: newValues
};
gdoctableapp.CreateTable(resource, function(err, result2) {
if (err) {
console.log(err);
return res.status(500).send(err);
}
console.log(result2);
res.send(result2);
});
});
});