|
| 1 | +import microsoftExcel from "../../microsoft_excel.app.mjs"; |
| 2 | +import { getColumnLetter } from "../../common/utils.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "microsoft_excel-add-row", |
| 6 | + name: "Add Row", |
| 7 | + 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)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + microsoftExcel, |
| 12 | + folderId: { |
| 13 | + propDefinition: [ |
| 14 | + microsoftExcel, |
| 15 | + "folderId", |
| 16 | + ], |
| 17 | + }, |
| 18 | + sheetId: { |
| 19 | + propDefinition: [ |
| 20 | + microsoftExcel, |
| 21 | + "sheetId", |
| 22 | + ({ folderId }) => ({ |
| 23 | + folderId, |
| 24 | + }), |
| 25 | + ], |
| 26 | + }, |
| 27 | + worksheet: { |
| 28 | + propDefinition: [ |
| 29 | + microsoftExcel, |
| 30 | + "worksheet", |
| 31 | + ({ sheetId }) => ({ |
| 32 | + sheetId, |
| 33 | + }), |
| 34 | + ], |
| 35 | + }, |
| 36 | + values: { |
| 37 | + type: "string[]", |
| 38 | + label: "Values", |
| 39 | + description: "An array of values for the new row. Each item in the array represents one cell. E.g. `[1, 2, 3]`", |
| 40 | + }, |
| 41 | + }, |
| 42 | + methods: { |
| 43 | + isArrayString(str) { |
| 44 | + return typeof str === "string" && ((str.startsWith("[") && str.endsWith("]")) || ((str.startsWith("[[") || str.startsWith("[ [")) && (str.endsWith("]]") || str.endsWith("] ]")))); |
| 45 | + }, |
| 46 | + convertStringToArray(str) { |
| 47 | + const arrayString = str.match(/\[\[?(.*?)\]?\]/)[1]; |
| 48 | + return arrayString.split(","); |
| 49 | + }, |
| 50 | + parseValues(columnCount) { |
| 51 | + let values = this.values; |
| 52 | + if (Array.isArray(this.values)) { |
| 53 | + if (Array.isArray(this.values[0])) { |
| 54 | + values = this.values[0]; |
| 55 | + } else if (this.isArrayString(this.values[0])) { |
| 56 | + values = this.convertStringToArray(this.values[0]); |
| 57 | + } |
| 58 | + } else { |
| 59 | + if (this.isArrayString(this.values)) { |
| 60 | + values = this.convertStringToArray(this.values); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (values.length < columnCount) { |
| 65 | + values.length = columnCount; |
| 66 | + } |
| 67 | + return values; |
| 68 | + }, |
| 69 | + }, |
| 70 | + async run({ $ }) { |
| 71 | + const { |
| 72 | + address, columnCount, |
| 73 | + } = await this.microsoftExcel.getUsedRange({ |
| 74 | + $, |
| 75 | + sheetId: this.sheetId, |
| 76 | + worksheet: this.worksheet, |
| 77 | + }); |
| 78 | + |
| 79 | + // get next row range |
| 80 | + const match = address.match(/^(.+!)?([A-Z]+)(\d+):([A-Z]+)(\d+)$/); |
| 81 | + const nextRow = parseInt(match[5], 10) + 1; |
| 82 | + const values = this.parseValues(columnCount); |
| 83 | + const colEnd = getColumnLetter(values.length); |
| 84 | + const range = `A${nextRow}:${colEnd}${nextRow}`; |
| 85 | + |
| 86 | + // insert range |
| 87 | + await this.microsoftExcel.insertRange({ |
| 88 | + $, |
| 89 | + sheetId: this.sheetId, |
| 90 | + worksheet: this.worksheet, |
| 91 | + range, |
| 92 | + data: { |
| 93 | + shift: "Down", |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + // update range |
| 98 | + const response = await this.microsoftExcel.updateRange({ |
| 99 | + $, |
| 100 | + sheetId: this.sheetId, |
| 101 | + worksheet: this.worksheet, |
| 102 | + range, |
| 103 | + data: { |
| 104 | + values: [ |
| 105 | + values, |
| 106 | + ], |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + $.export("$summary", "Successfully added new row"); |
| 111 | + return response; |
| 112 | + }, |
| 113 | +}; |
0 commit comments