-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
add-multiple-rows.mjs
118 lines (111 loc) · 3.39 KB
/
add-multiple-rows.mjs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import common from "../common/worksheet.mjs";
import { ConfigurationError } from "@pipedream/platform";
import {
parseArray, getWorksheetHeaders,
} from "../../common/utils.mjs";
const { googleSheets } = common.props;
export default {
...common,
key: "google_sheets-add-multiple-rows",
name: "Add Multiple Rows",
description: "Add multiple rows of data to a Google Sheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append)",
version: "0.2.8",
type: "action",
props: {
googleSheets,
drive: {
propDefinition: [
googleSheets,
"watchedDrive",
],
},
sheetId: {
propDefinition: [
googleSheets,
"sheetID",
(c) => ({
driveId: googleSheets.methods.getDriveId(c.drive),
}),
],
},
worksheetId: {
propDefinition: [
googleSheets,
"worksheetIDs",
(c) => ({
sheetId: c.sheetId,
}),
],
type: "string",
label: "Worksheet Id",
reloadProps: true,
},
headersDisplay: {
propDefinition: [
googleSheets,
"headersDisplay",
],
},
rows: {
propDefinition: [
googleSheets,
"rows",
],
},
rowsDescription: {
propDefinition: [
googleSheets,
"rowsDescription",
],
},
resetRowFormat: {
type: "boolean",
label: "Reset Row Format",
description: "Reset the formatting of the rows that were added (line style to none, background to white, foreground color to black, font size to 10, no bold, no italic, no strikethrough, horizontalAlignment to left). This is useful if you want to add rows to a formatted table in Google Sheets.",
optional: true,
},
},
async additionalProps() {
const props = {};
if (!this.sheetId || !this.worksheetId) {
return props;
}
const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId);
const rowHeaders = await getWorksheetHeaders(this, this.sheetId, worksheet?.properties?.title);
if (rowHeaders.length) {
return {
headersDisplay: {
type: "alert",
alertType: "info",
content: `Possible Row Headers: **\`${rowHeaders.join(", ")}\`**`,
hidden: false,
},
};
}
},
async run() {
let inputValidated = true;
const rows = parseArray(this.rows);
if (!rows) {
inputValidated = false;
} else {
rows.forEach((row) => { if (!Array.isArray(row)) { inputValidated = false; } });
}
// Throw an error if input validation failed
if (!inputValidated) {
console.error("Data Submitted:");
console.error(rows);
throw new ConfigurationError("Rows data is not an array of arrays. Please enter an array of arrays in the `Rows` parameter above. If you're trying to send a single rows to Google Sheets, search for the action to add a single row to Sheets or try modifying the code for this step.");
}
const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId);
const addRowsResponse = await this.googleSheets.addRowsToSheet({
spreadsheetId: this.sheetId,
range: worksheet?.properties?.title,
rows,
});
if (this.resetRowFormat) {
await this.googleSheets.resetRowFormat(this.sheetId, addRowsResponse.updatedRange);
}
return addRowsResponse;
},
};