-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet_operations.js
More file actions
481 lines (406 loc) · 15.5 KB
/
sheet_operations.js
File metadata and controls
481 lines (406 loc) · 15.5 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* @fileoverview Sheet Operations Module - Incremental Update Strategies
* Provides different strategies for updating sheets efficiently during incremental audits
* Supports append-only, merge-update, and selective refresh modes
*
* @version 1.0
* @date January 2026
*/
// =================================================================
// CONSTANTS
// =================================================================
const SHEET_OPS_BATCH_SIZE = 500; // Max rows to write in single batch
const SHEET_OPS_SLEEP_INTERVAL = 100; // Sleep between batches (ms)
/**
* Append-only sheet update strategy.
* Use for resources that are rarely deleted (GA4 Properties, GTM Containers, etc.)
* Simply appends new rows at the end without checking for duplicates.
*
* @param {string} sheetName - Name of sheet to update
* @param {Array<Array>} newRecords - Array of record arrays to append
* @param {Object} options - Options object
* @returns {Object} Update result with record count and status
*/
function appendNewRecords(sheetName, newRecords, options = {}) {
try {
if (!newRecords || newRecords.length === 0) {
logEvent('SHEET_OPS', `No new records to append for ${sheetName}, skipping update`);
return { status: 'SKIPPED', recordsAppended: 0, sheetName: sheetName };
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(sheetName);
// Create sheet if it doesn't exist
if (!sheet) {
// Get headers from options or create default
const headers = options.headers || getDefaultHeaders(sheetName);
sheet = ss.insertSheet(sheetName);
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
logEvent('SHEET_OPS', `Created sheet: ${sheetName}`);
}
// Get last row
const lastRow = sheet.getLastRow();
const colCount = newRecords[0].length;
// Batch write to avoid hitting timeout limits
let recordsWritten = 0;
for (let i = 0; i < newRecords.length; i += SHEET_OPS_BATCH_SIZE) {
const batch = newRecords.slice(i, i + SHEET_OPS_BATCH_SIZE);
const insertRow = lastRow + 1 + i;
// Write batch
sheet.getRange(insertRow, 1, batch.length, colCount).setValues(batch);
recordsWritten += batch.length;
// Sleep between batches to avoid quota issues
if (i + SHEET_OPS_BATCH_SIZE < newRecords.length) {
Utilities.sleep(SHEET_OPS_SLEEP_INTERVAL);
}
}
logEvent('SHEET_OPS', `Appended ${recordsWritten} records to ${sheetName}`);
return {
status: 'SUCCESS',
recordsAppended: recordsWritten,
sheetName: sheetName,
totalRowsInSheet: lastRow + recordsWritten
};
} catch (error) {
logError('SHEET_OPS', `Failed to append records to ${sheetName}: ${error.message}`);
return { status: 'ERROR', error: error.message, sheetName: sheetName };
}
}
/**
* Merge-update sheet strategy.
* Updates existing rows by primary key, appends new rows.
* Use for resources that can be modified (BigQuery Tables, GTM Tags, etc.)
*
* @param {string} sheetName - Name of sheet to update
* @param {Array<Array>} newRecords - Array of record arrays to merge
* @param {number} primaryKeyColumnIndex - Column index of primary key (0-indexed)
* @param {Object} options - Options object
* @returns {Object} Update result with counts
*/
function mergeUpdateRecords(sheetName, newRecords, primaryKeyColumnIndex = 0, options = {}) {
try {
if (!newRecords || newRecords.length === 0) {
logEvent('SHEET_OPS', `No records to merge for ${sheetName}, skipping update`);
return { status: 'SKIPPED', recordsMerged: 0, sheetName: sheetName };
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(sheetName);
// Create sheet if it doesn't exist
if (!sheet) {
const headers = options.headers || getDefaultHeaders(sheetName);
sheet = ss.insertSheet(sheetName);
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
logEvent('SHEET_OPS', `Created sheet: ${sheetName}`);
}
// Get all existing data
const allData = sheet.getDataRange().getValues();
const colCount = newRecords[0].length;
// Build map of existing records by primary key
// Key format: row number (1-indexed in sheet, so we need to adjust)
const existingByKey = {};
for (let i = 1; i < allData.length; i++) {
const key = allData[i][primaryKeyColumnIndex];
if (key) {
existingByKey[key] = i + 1; // Sheet row number (1-indexed)
}
}
// Process new records
const toAppend = [];
let updatedCount = 0;
for (const newRecord of newRecords) {
const key = newRecord[primaryKeyColumnIndex];
if (!key) {
logWarning('SHEET_OPS', `Record missing primary key in ${sheetName}, appending as new`);
toAppend.push(newRecord);
continue;
}
if (existingByKey[key]) {
// Update existing row
const rowNum = existingByKey[key];
sheet.getRange(rowNum, 1, 1, colCount).setValues([newRecord]);
updatedCount++;
} else {
// Collect new records for batch append
toAppend.push(newRecord);
}
}
// Batch append new records
let appendedCount = 0;
if (toAppend.length > 0) {
const lastRow = sheet.getLastRow();
for (let i = 0; i < toAppend.length; i += SHEET_OPS_BATCH_SIZE) {
const batch = toAppend.slice(i, i + SHEET_OPS_BATCH_SIZE);
const insertRow = lastRow + 1 + i;
sheet.getRange(insertRow, 1, batch.length, colCount).setValues(batch);
appendedCount += batch.length;
if (i + SHEET_OPS_BATCH_SIZE < toAppend.length) {
Utilities.sleep(SHEET_OPS_SLEEP_INTERVAL);
}
}
}
logEvent('SHEET_OPS', `Merged ${newRecords.length} records into ${sheetName}: ${updatedCount} updated, ${appendedCount} appended`);
return {
status: 'SUCCESS',
recordsProcessed: newRecords.length,
recordsUpdated: updatedCount,
recordsAppended: appendedCount,
sheetName: sheetName,
totalRowsInSheet: sheet.getLastRow()
};
} catch (error) {
logError('SHEET_OPS', `Failed to merge records in ${sheetName}: ${error.message}`);
return { status: 'ERROR', error: error.message, sheetName: sheetName };
}
}
/**
* Selective refresh strategy.
* Compares records by hash, only updates rows that actually changed.
* Most efficient for large datasets with few changes.
* Use for frequently modified resources (GTM Tags, etc.)
*
* @param {string} sheetName - Name of sheet to update
* @param {Array<Array>} newRecords - Array of record arrays to process
* @param {number} primaryKeyColumnIndex - Column index of primary key (0-indexed)
* @param {Object} options - Options object with skipHashCheck flag
* @returns {Object} Update result with detailed counts
*/
function selectiveRefreshRecords(sheetName, newRecords, primaryKeyColumnIndex = 0, options = {}) {
try {
if (!newRecords || newRecords.length === 0) {
logEvent('SHEET_OPS', `No records to process for ${sheetName}, skipping update`);
return { status: 'SKIPPED', recordsProcessed: 0, sheetName: sheetName };
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(sheetName);
// Create sheet if it doesn't exist
if (!sheet) {
const headers = options.headers || getDefaultHeaders(sheetName);
sheet = ss.insertSheet(sheetName);
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
logEvent('SHEET_OPS', `Created sheet: ${sheetName}`);
}
// Get all existing data
const allData = sheet.getDataRange().getValues();
const colCount = newRecords[0].length;
// Build hash map of existing records
const existingHashes = {};
for (let i = 1; i < allData.length; i++) {
const key = allData[i][primaryKeyColumnIndex];
if (key) {
existingHashes[key] = {
rowNum: i + 1,
hash: hashArray(allData[i])
};
}
}
// Find changed and new records
const changed = [];
const newOnly = [];
let unchangedCount = 0;
for (const newRecord of newRecords) {
const key = newRecord[primaryKeyColumnIndex];
if (!key) {
logWarning('SHEET_OPS', `Record missing primary key in ${sheetName}`);
newOnly.push(newRecord);
continue;
}
if (existingHashes[key]) {
// Compare hashes
const newHash = hashArray(newRecord);
if (newHash !== existingHashes[key].hash) {
changed.push({ key: key, record: newRecord, rowNum: existingHashes[key].rowNum });
} else {
unchangedCount++;
}
} else {
// New record
newOnly.push(newRecord);
}
}
// Update changed rows
let updatedCount = 0;
for (const { rowNum, record } of changed) {
sheet.getRange(rowNum, 1, 1, colCount).setValues([record]);
updatedCount++;
}
// Append new rows
let appendedCount = 0;
if (newOnly.length > 0) {
const lastRow = sheet.getLastRow();
for (let i = 0; i < newOnly.length; i += SHEET_OPS_BATCH_SIZE) {
const batch = newOnly.slice(i, i + SHEET_OPS_BATCH_SIZE);
const insertRow = lastRow + 1 + i;
sheet.getRange(insertRow, 1, batch.length, colCount).setValues(batch);
appendedCount += batch.length;
if (i + SHEET_OPS_BATCH_SIZE < newOnly.length) {
Utilities.sleep(SHEET_OPS_SLEEP_INTERVAL);
}
}
}
logEvent('SHEET_OPS', `Selectively refreshed ${sheetName}: ${updatedCount} updated, ${appendedCount} appended, ${unchangedCount} unchanged`);
return {
status: 'SUCCESS',
recordsProcessed: newRecords.length,
recordsChanged: updatedCount,
recordsAppended: appendedCount,
recordsUnchanged: unchangedCount,
sheetName: sheetName,
totalRowsInSheet: sheet.getLastRow()
};
} catch (error) {
logError('SHEET_OPS', `Failed to selectively refresh ${sheetName}: ${error.message}`);
return { status: 'ERROR', error: error.message, sheetName: sheetName };
}
}
/**
* Creates or clears and writes a sheet completely (fallback to legacy behavior).
* Use only when you need to completely reset a sheet.
* NOT recommended for incremental audits - use append/merge instead.
*
* @param {string} sheetName - Name of sheet
* @param {Array<string>} headers - Column headers
* @param {Array<Array>} data - Data rows
* @returns {Object} Write result
*/
function writeDataToSheetIncremental(sheetName, headers, data) {
try {
if (!headers || headers.length === 0) {
throw new Error('Headers are required');
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(sheetName);
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet(sheetName);
logEvent('SHEET_OPS', `Created sheet: ${sheetName}`);
} else {
// Clear all existing content
sheet.clearContents();
}
// Write headers
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
// Write data
if (data && data.length > 0) {
// Batch write data
for (let i = 0; i < data.length; i += SHEET_OPS_BATCH_SIZE) {
const batch = data.slice(i, i + SHEET_OPS_BATCH_SIZE);
const startRow = 2 + i;
sheet.getRange(startRow, 1, batch.length, headers.length).setValues(batch);
if (i + SHEET_OPS_BATCH_SIZE < data.length) {
Utilities.sleep(SHEET_OPS_SLEEP_INTERVAL);
}
}
}
// Format headers
const headerRange = sheet.getRange(1, 1, 1, headers.length);
headerRange.setFontWeight('bold');
headerRange.setBackground('#f3f3f3');
// Auto-resize columns
for (let i = 1; i <= headers.length; i++) {
sheet.autoResizeColumn(i);
}
logEvent('SHEET_OPS', `Wrote ${data ? data.length : 0} records to ${sheetName}`);
return {
status: 'SUCCESS',
sheetName: sheetName,
recordsWritten: data ? data.length : 0
};
} catch (error) {
logError('SHEET_OPS', `Failed to write data to ${sheetName}: ${error.message}`);
return { status: 'ERROR', error: error.message, sheetName: sheetName };
}
}
// =================================================================
// UTILITY FUNCTIONS
// =================================================================
/**
* Computes a simple hash of an array for change detection.
* Used for comparison in selective refresh operations.
*
* @private
* @param {Array} arr - Array to hash
* @returns {string} Hash of the array
*/
function hashArray(arr) {
try {
// Convert array to JSON string and compute hash
const str = JSON.stringify(arr);
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return hash.toString();
} catch (error) {
logWarning('SHEET_OPS', `Hash computation failed: ${error.message}`);
return '';
}
}
/**
* Gets default headers for a sheet based on its name.
* Used when creating sheets if headers aren't provided.
*
* @private
* @param {string} sheetName - Name of the sheet
* @returns {Array<string>} Default headers for this sheet
*/
function getDefaultHeaders(sheetName) {
const headerMap = {
'GA4_PROPERTIES': ['Property ID', 'Display Name', 'Create Time', 'Industry Category', 'Time Zone', 'Sync Date'],
'GA4_CUSTOM_DIMENSIONS': ['Property ID', 'Dimension Name', 'Scope', 'Description', 'Sync Date'],
'GA4_CUSTOM_METRICS': ['Property ID', 'Metric Name', 'Scope', 'Unit', 'Description', 'Sync Date'],
'GTM_CONTAINERS': ['Account ID', 'Container ID', 'Name', 'Usage Context', 'Created By', 'Sync Date'],
'GTM_TAGS': ['Container ID', 'Tag ID', 'Name', 'Type', 'Fire On', 'Workspace', 'Sync Date'],
'BQ_DATASETS': ['Project ID', 'Dataset ID', 'Location', 'Created Date', 'Last Modified', 'Sync Date'],
'BQ_TABLES': ['Project ID', 'Dataset ID', 'Table ID', 'Row Count', 'Size (GB)', 'Created Date', 'Sync Date'],
'_AUDIT_METADATA': ['Service', 'Resource Type', 'Last Sync Timestamp', 'Record Count', 'Status', 'Sync Mode']
};
return headerMap[sheetName] || ['ID', 'Name', 'Value', 'Sync Date'];
}
/**
* Gets count of rows (excluding header) in a sheet.
*
* @param {string} sheetName - Name of sheet
* @returns {number} Number of data rows
*/
function getSheetRecordCount(sheetName) {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
if (!sheet) {
return 0;
}
const lastRow = sheet.getLastRow();
return lastRow > 1 ? lastRow - 1 : 0; // Subtract 1 for header row
} catch (error) {
logWarning('SHEET_OPS', `Could not get record count for ${sheetName}: ${error.message}`);
return 0;
}
}
/**
* Clears all data from a sheet (but keeps headers if they exist).
*
* @param {string} sheetName - Name of sheet
* @returns {boolean} True if successful
*/
function clearSheetData(sheetName) {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
if (!sheet) {
return false;
}
// Get last row/column
const lastRow = sheet.getLastRow();
const lastCol = sheet.getLastColumn();
// Keep header row (row 1), delete everything else
if (lastRow > 1) {
sheet.deleteRows(2, lastRow - 1);
}
logEvent('SHEET_OPS', `Cleared data from ${sheetName} (kept header row)`);
return true;
} catch (error) {
logError('SHEET_OPS', `Failed to clear sheet data: ${error.message}`);
return false;
}
}