-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (70 loc) · 2.03 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
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
const express = require("express");
const req = require("express/lib/request");
const { google } = require("googleapis");
const app = express();
app.use(express.json());
async function getAuthSheets() {
const auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
const client = await auth.getClient();
const googleSheets = google.sheets({
version: "v4",
auth: client,
});
const spreadsheetId = "";
return {
auth,
client,
googleSheets,
spreadsheetId,
};
}
app.get("/metadata", async (req, res) => {
const { googleSheets, auth, spreadsheetId } = await getAuthSheets();
const metadata = await googleSheets.spreadsheets.get({
auth,
spreadsheetId,
});
res.send(metadata.data);
});
app.get("/getRows", async (req, res) => {
const { googleSheets, auth, spreadsheetId } = await getAuthSheets();
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: "Página1",
valueRenderOption: "UNFORMATTED_VALUE",
dateTimeRenderOption: "FORMATTED_STRING",
});
res.send(getRows.data);
});
app.post("/addRow", async (req, res) => {
const { googleSheets, auth, spreadsheetId } = await getAuthSheets();
const { values } = req.body;
const row = await googleSheets.spreadsheets.values.append({
auth,
spreadsheetId,
range: "Página1",
valueInputOption: "USER_ENTERED",
resource: {
values: values,
},
});
res.send(row.data);
});
app.post("/updateValue", async (req, res) => {
const { googleSheets, auth, spreadsheetId } = await getAuthSheets();
const { values } = req.body;
const updateValue = await googleSheets.spreadsheets.values.update({
spreadsheetId,
range: "Página1!A2:C2",
valueInputOption: "USER_ENTERED",
resource: {
values: values,
},
});
res.send(updateValue.data);
});
app.listen(3001, () => console.log("Rodando na porta: 3001"));