|
| 1 | +import coda from "../../coda.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "coda-find-row", |
| 5 | + name: "Find Row", |
| 6 | + description: "Searches for a row in the selected table using a column match search. [See docs](https://coda.io/developers/apis/v1#operation/listRows)", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + coda, |
| 11 | + docId: { |
| 12 | + propDefinition: [ |
| 13 | + coda, |
| 14 | + "docId", |
| 15 | + ], |
| 16 | + }, |
| 17 | + tableId: { |
| 18 | + propDefinition: [ |
| 19 | + coda, |
| 20 | + "tableId", |
| 21 | + (c) => ({ |
| 22 | + docId: c.docId, |
| 23 | + }), |
| 24 | + ], |
| 25 | + }, |
| 26 | + columnId: { |
| 27 | + propDefinition: [ |
| 28 | + coda, |
| 29 | + "columnId", |
| 30 | + (c) => ({ |
| 31 | + docId: c.docId, |
| 32 | + tableId: c.tableId, |
| 33 | + }), |
| 34 | + ], |
| 35 | + description: "ID of the column. This field is required if querying", |
| 36 | + optional: true, |
| 37 | + }, |
| 38 | + query: { |
| 39 | + propDefinition: [ |
| 40 | + coda, |
| 41 | + "query", |
| 42 | + ], |
| 43 | + description: "Query used to filter returned rows", |
| 44 | + optional: true, |
| 45 | + }, |
| 46 | + sortBy: { |
| 47 | + propDefinition: [ |
| 48 | + coda, |
| 49 | + "sortBy", |
| 50 | + ], |
| 51 | + description: "Specifies the sort order of the rows returned. If left unspecified, rows are returned by creation time ascending", |
| 52 | + options: [ |
| 53 | + "createdAt", |
| 54 | + "natural", |
| 55 | + "updatedAt", |
| 56 | + ], |
| 57 | + }, |
| 58 | + visibleOnly: { |
| 59 | + propDefinition: [ |
| 60 | + coda, |
| 61 | + "visibleOnly", |
| 62 | + ], |
| 63 | + }, |
| 64 | + useColumnNames: { |
| 65 | + type: "boolean", |
| 66 | + label: "Use Column Names", |
| 67 | + description: "Use column names instead of column IDs in the returned output", |
| 68 | + optional: true, |
| 69 | + }, |
| 70 | + valueFormat: { |
| 71 | + type: "string", |
| 72 | + label: "Value Format", |
| 73 | + description: "The format that individual cell values are returned as", |
| 74 | + optional: true, |
| 75 | + options: [ |
| 76 | + "simple", |
| 77 | + "simpleWithArrays", |
| 78 | + "rich", |
| 79 | + ], |
| 80 | + }, |
| 81 | + max: { |
| 82 | + propDefinition: [ |
| 83 | + coda, |
| 84 | + "max", |
| 85 | + ], |
| 86 | + }, |
| 87 | + }, |
| 88 | + async run({ $ }) { |
| 89 | + let params = { |
| 90 | + sortBy: this.sortBy, |
| 91 | + visibleOnly: this.visibleOnly, |
| 92 | + useColumnNames: this.useColumnNames, |
| 93 | + valueFormat: this.valueFormat, |
| 94 | + }; |
| 95 | + |
| 96 | + if (this.columnId && this.query) { |
| 97 | + params.query = `${this.columnId}:"${this.query}"`; |
| 98 | + } |
| 99 | + |
| 100 | + let items = []; |
| 101 | + let response; |
| 102 | + do { |
| 103 | + response = await this.coda.findRow( |
| 104 | + $, |
| 105 | + this.docId, |
| 106 | + this.tableId, |
| 107 | + params, |
| 108 | + ); |
| 109 | + items.push(...response.items); |
| 110 | + params.pageToken = response.nextPageToken; |
| 111 | + } while (params.pageToken && items.length < this.max); |
| 112 | + |
| 113 | + if (items.length > this.max) items.length = this.max; |
| 114 | + |
| 115 | + if (items.length > 0) { |
| 116 | + $.export("$summary", `Found ${items.length} rows`); |
| 117 | + } else { |
| 118 | + $.export("$summary", `No rows found with the search query: ${this.query}`); |
| 119 | + } |
| 120 | + return { |
| 121 | + items, |
| 122 | + }; |
| 123 | + }, |
| 124 | +}; |
0 commit comments