-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (41 loc) · 1.41 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
const express = require("express");
const { google } = require("googleapis");
const getPrice = require("./utilities/GetPrice");
const app = express();
app.get("/", async (req, res) => {
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 for Google-Sheet API
const googleSheets = google.sheets({ version: "v4", auth: client });
const spreadsheetId = "1XkfAqpgaW7iuBYepjxMKGOTRdlzqSqtOnUuV_1U_FCc";
//Get metadata about SpreadSheet
const metadata = await googleSheets.spreadsheets.get({ auth, spreadsheetId });
//Read rows from spraedSheet
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: "Sheet1!A2:A",
});
//get an Array of new prices
const priceList = await getPrice(getRows.data.values);
//Write rows to spreadSheet
await googleSheets.spreadsheets.values.update({
auth,
spreadsheetId,
range: "Sheet1!B2:B",
valueInputOption: "USER_ENTERED",
requestBody: { values: priceList },
});
//Read rows from updated spraedSheet
const getUpdatedData = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: "Sheet1!A2:B10",
});
res.send(getUpdatedData.data);
});
app.listen(1024, (req, res) => console.log("running on 1024...."));