-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
55 lines (43 loc) · 1.33 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
const express = require("express");
const { google } = require("googleapis");
const app = express();
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.render("index");
});
app.post("/", async (req, res) => {
const { request, name } = req.body;
const auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
// Create client instance for auth
const client = await auth.getClient();
// Instance of Google Sheets API
const googleSheets = google.sheets({ version: "v4", auth: client });
const spreadsheetId = "1J5OesnSTJCgLTTA0hQ_QSk_UPVK1nwRTEkvvRHHrEqM";
// Get metadata about spreadsheet
const metaData = await googleSheets.spreadsheets.get({
auth,
spreadsheetId,
});
// Read rows from spreadsheet
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: "Sheet1!A:A",
});
// Write row(s) to spreadsheet
await googleSheets.spreadsheets.values.append({
auth,
spreadsheetId,
range: "Sheet1!A:B",
valueInputOption: "USER_ENTERED",
resource: {
values: [[request, name]],
},
});
res.send("Successfully submitted! Thank you!");
});
app.listen(1337, (req, res) => console.log("running on 1337"));