-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-data.js
67 lines (56 loc) · 1.67 KB
/
post-data.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
import { google } from "googleapis";
import fs from "fs";
import path from "path";
const addRows = async (parsedList) => {
const filePath = path.join(process.cwd(), "secret", "key.json");
const serviceAccount = JSON.parse(fs.readFileSync(filePath));
// Define the scopes for the Google Sheets API
const scopes = ["https://www.googleapis.com/auth/spreadsheets"];
// Authenticate the service account
const auth = new google.auth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
scopes
);
const service = google.sheets({ version: "v4", auth });
// Spreadsheet ID - from the URL of your Google Sheets document
const spreadsheetId = process.env.SPREADSHEET_ID;
// Sheet name
const sheet = process.env.SHEET_NAME;
// Input option - let's say we want the input to be parsed
const valueInputOption = "USER_ENTERED";
// Values to be inserted - a 2D array of values
// Convert parsedList to a 2D array of values
const values = parsedList.map((item) => [
item.name,
item.username,
item.verified,
item.profile_link,
item.location,
item.followers_count,
item.friends_count,
item.profile_image_url,
item.description,
item.created_at,
item.media_count,
item.statuses_count,
]);
const resource = {
values,
};
try {
const result = await service.spreadsheets.values.append({
spreadsheetId,
range: `${sheet}!A3`,
valueInputOption,
resource,
});
if (result.statusText === "OK") return true;
} catch (err) {
// TODO (Developer) - Handle exception
console.log("Error in posting data to google api: ", err);
}
return false;
};
export { addRows };