-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (64 loc) · 1.81 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
import * as dotenv from "dotenv";
dotenv.config();
import { info, getInput } from "@actions/core";
import { JWT } from "google-auth-library";
import { google } from "googleapis";
import { readFileSync } from "fs";
const sheets = google.sheets("v4");
const user = getInput("GOOGLE_USER_MAIL", {
required: true,
});
const key = getInput("GOOGLE_USER_KEY", {
required: true,
});
const googleAuth = new JWT(user, null, key, [
"https://www.googleapis.com/auth/spreadsheets",
]);
const spreadsheetId = getInput("SPREADSHEET", {
required: true,
});
const tableStartCell = getInput("TABLE_START_CELL") || "A1";
const dataEnvironmentVariableName = getInput("DATA_ENV_NAME");
const dataFileName = getInput('DATA_FILE_PATH');
if (!dataEnvironmentVariableName && !dataFileName) {
throw new Error("Either 'DATA_ENV_NAME' or 'DATA_FILE_PATH' are required.")
}
let rawData;
if (dataFileName) {
rawData = readFileSync(dataFileName, { encoding: 'utf-8' })
} else {
rawData = process.env[dataEnvironmentVariableName]
}
const data = rawData
.split("\n")
.map((dataRow) => dataRow.split(","));
const update = getInput("UPDATE") || "false";
if (update.toLowerCase() === "true") {
info("Updating data...");
await sheets.spreadsheets.values.update({
auth: googleAuth,
spreadsheetId: spreadsheetId,
range: tableStartCell,
valueInputOption: "USER_ENTERED",
requestBody: {
majorDimension: "ROWS",
range: tableStartCell,
values: data,
},
});
} else {
info("Appending data...");
await sheets.spreadsheets.values.append({
auth: googleAuth,
spreadsheetId: spreadsheetId,
range: tableStartCell,
valueInputOption: "USER_ENTERED",
insertDataOption: "INSERT_ROWS",
requestBody: {
majorDimension: "ROWS",
range: tableStartCell,
values: data,
},
});
}
info("Done.");