-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathadd-row.mjs
90 lines (85 loc) · 2.11 KB
/
add-row.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
import microsoftExcel from "../../microsoft_excel.app.mjs";
import {
parseObject, getColumnLetter,
} from "../../common/utils.mjs";
export default {
key: "microsoft_excel-add-row",
name: "Add Row",
description: "Insert a new row into a specified Excel worksheet. [See the documentation](https://learn.microsoft.com/en-us/graph/api/range-insert?view=graph-rest-1.0&tabs=http)",
version: "0.0.1",
type: "action",
props: {
microsoftExcel,
folderId: {
propDefinition: [
microsoftExcel,
"folderId",
],
},
sheetId: {
propDefinition: [
microsoftExcel,
"sheetId",
({ folderId }) => ({
folderId,
}),
],
},
worksheet: {
propDefinition: [
microsoftExcel,
"worksheet",
({ sheetId }) => ({
sheetId,
}),
],
},
values: {
type: "string[]",
label: "Values",
description: "An array of values for the new row. Each item in the array represents one cell. E.g. `[1, 2, 3]`",
},
},
async run({ $ }) {
const {
address, columnCount,
} = await this.microsoftExcel.getUsedRange({
$,
sheetId: this.sheetId,
worksheet: this.worksheet,
});
// get next row range
const match = address.match(/^(.+!)?([A-Z]+)(\d+):([A-Z]+)(\d+)$/);
const nextRow = parseInt(match[5], 10) + 1;
const values = parseObject(this.values);
if (values.length < columnCount) {
values.length = columnCount;
}
const colEnd = getColumnLetter(values.length);
const range = `A${nextRow}:${colEnd}${nextRow}`;
// insert range
await this.microsoftExcel.insertRange({
$,
sheetId: this.sheetId,
worksheet: this.worksheet,
range,
data: {
shift: "Down",
},
});
// update range
const response = await this.microsoftExcel.updateRange({
$,
sheetId: this.sheetId,
worksheet: this.worksheet,
range,
data: {
values: [
values,
],
},
});
$.export("$summary", "Successfully added new row");
return response;
},
};