|
1 | | -import httpBase from "../common/http-based/sheet.mjs"; |
2 | | -import newRowAdded from "../common/new-row-added.mjs"; |
3 | 1 | import sampleEmit from "./test-event.mjs"; |
4 | 2 |
|
5 | 3 | export default { |
6 | | - ...httpBase, |
7 | | - ...newRowAdded, |
8 | 4 | key: "google_sheets-new-row-added", |
9 | | - name: "New Row Added (Instant)", |
10 | | - description: "Emit new event each time a row or rows are added to the bottom of a spreadsheet.", |
11 | | - version: "0.2.3", |
12 | | - dedupe: "unique", |
13 | 5 | type: "source", |
| 6 | + name: "New Row Added (Polling)", |
| 7 | + description: |
| 8 | + "Emit new event each time a row or rows are added to the bottom of a spreadsheet.", |
| 9 | + version: "0.3.0", |
| 10 | + dedupe: "unique", |
14 | 11 | props: { |
15 | | - ...httpBase.props, |
16 | | - ...newRowAdded.props, |
| 12 | + db: "$.service.db", |
| 13 | + http: "$.interface.http", |
| 14 | + google_sheets: { |
| 15 | + type: "app", |
| 16 | + app: "google_sheets", |
| 17 | + label: "Google Sheets", |
| 18 | + description: "Google Sheets account used to access the Sheets API", |
| 19 | + }, |
| 20 | + spreadsheet_id: { |
| 21 | + type: "string", |
| 22 | + label: "Spreadsheet ID", |
| 23 | + description: "The Google Sheets spreadsheet ID", |
| 24 | + }, |
| 25 | + sheet_name: { |
| 26 | + type: "string", |
| 27 | + label: "Sheet Name", |
| 28 | + description: "The name of the sheet to monitor (e.g., 'Sheet1')", |
| 29 | + }, |
17 | 30 | }, |
18 | 31 | methods: { |
19 | | - ...httpBase.methods, |
20 | | - ...newRowAdded.methods, |
| 32 | + async getSheetData() { |
| 33 | + const { |
| 34 | + google_sheets, |
| 35 | + spreadsheet_id, |
| 36 | + sheet_name, |
| 37 | + } = this; |
| 38 | + |
| 39 | + const response = await google_sheets.request({ |
| 40 | + url: `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheet_id}/values/${encodeURIComponent(sheet_name)}`, |
| 41 | + method: "GET", |
| 42 | + }); |
| 43 | + |
| 44 | + if (response && response.values) { |
| 45 | + return response.values; |
| 46 | + } |
| 47 | + return []; |
| 48 | + }, |
| 49 | + |
| 50 | + async getHeaderRow() { |
| 51 | + const data = await this.getSheetData(); |
| 52 | + if (data.length > 0) { |
| 53 | + return data[0]; |
| 54 | + } |
| 55 | + return []; |
| 56 | + }, |
| 57 | + |
| 58 | + rowToObject(headers, rowData) { |
| 59 | + const obj = {}; |
| 60 | + headers.forEach((header, idx) => { |
| 61 | + obj[header] = rowData[idx] || ""; |
| 62 | + }); |
| 63 | + return obj; |
| 64 | + }, |
| 65 | + |
| 66 | + getStoredRowCount() { |
| 67 | + const { |
| 68 | + spreadsheet_id, |
| 69 | + sheet_name, |
| 70 | + db, |
| 71 | + } = this; |
| 72 | + const stateKey = `row-count-${spreadsheet_id}-${sheet_name}`; |
| 73 | + return db.get(stateKey) || 0; |
| 74 | + }, |
| 75 | + |
| 76 | + updateStoredRowCount(count) { |
| 77 | + const { |
| 78 | + spreadsheet_id, |
| 79 | + sheet_name, |
| 80 | + db, |
| 81 | + } = this; |
| 82 | + const stateKey = `row-count-${spreadsheet_id}-${sheet_name}`; |
| 83 | + db.set(stateKey, count); |
| 84 | + }, |
| 85 | + |
| 86 | + getStoredRows() { |
| 87 | + const { |
| 88 | + spreadsheet_id, |
| 89 | + sheet_name, |
| 90 | + db, |
| 91 | + } = this; |
| 92 | + const stateKey = `rows-${spreadsheet_id}-${sheet_name}`; |
| 93 | + return db.get(stateKey) || []; |
| 94 | + }, |
| 95 | + |
| 96 | + updateStoredRows(rows) { |
| 97 | + const { |
| 98 | + spreadsheet_id, |
| 99 | + sheet_name, |
| 100 | + db, |
| 101 | + } = this; |
| 102 | + const stateKey = `rows-${spreadsheet_id}-${sheet_name}`; |
| 103 | + db.set(stateKey, rows); |
| 104 | + }, |
| 105 | + }, |
| 106 | + async run() { |
| 107 | + const { |
| 108 | + spreadsheet_id, |
| 109 | + sheet_name, |
| 110 | + } = this; |
| 111 | + |
| 112 | + if (!spreadsheet_id || !sheet_name) { |
| 113 | + throw new Error("Please provide both Spreadsheet ID and Sheet Name"); |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + const allData = await this.getSheetData(); |
| 118 | + |
| 119 | + // Sheet must have at least a header row |
| 120 | + if (allData.length === 0) { |
| 121 | + this.updateStoredRowCount(0); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const headers = allData[0]; |
| 126 | + const currentRows = allData.slice(1); // Exclude header |
| 127 | + const storedRowCount = this.getStoredRowCount(); |
| 128 | + |
| 129 | + // Detect new rows added to the bottom |
| 130 | + if (currentRows.length > storedRowCount) { |
| 131 | + const newRows = currentRows.slice(storedRowCount); |
| 132 | + |
| 133 | + // Emit event for new rows |
| 134 | + newRows.forEach((rowData, idx) => { |
| 135 | + const rowNumber = storedRowCount + idx + 2; // +2: 1 for header, 1 for 1-indexing |
| 136 | + const rowObject = this.rowToObject(headers, rowData); |
| 137 | + |
| 138 | + this.$emit( |
| 139 | + { |
| 140 | + row_number: rowNumber, |
| 141 | + values: rowData, |
| 142 | + object: rowObject, |
| 143 | + spreadsheet_id, |
| 144 | + sheet_name, |
| 145 | + timestamp: new Date().toISOString(), |
| 146 | + }, |
| 147 | + { |
| 148 | + id: `${spreadsheet_id}-${sheet_name}-row-${rowNumber}`, |
| 149 | + summary: `New row added at row ${rowNumber}`, |
| 150 | + ts: Date.now(), |
| 151 | + }, |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + // Update stored row count |
| 156 | + this.updateStoredRowCount(currentRows.length); |
| 157 | + } else if (storedRowCount === 0) { |
| 158 | + // First run: store current row count without emitting |
| 159 | + this.updateStoredRowCount(currentRows.length); |
| 160 | + } |
| 161 | + } catch (error) { |
| 162 | + console.error(`Error fetching sheet data: ${error.message}`); |
| 163 | + throw error; |
| 164 | + } |
21 | 165 | }, |
22 | 166 | sampleEmit, |
23 | 167 | }; |
0 commit comments