From db181706e4bc742ba93e572e4c565e729a6c0764 Mon Sep 17 00:00:00 2001 From: Dmytro2V <107959955+Dmytro2V@users.noreply.github.com> Date: Thu, 19 Oct 2023 00:33:58 +0300 Subject: [PATCH] Update index.js Added patchRows and patchRow methods --- dist/index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dist/index.js b/dist/index.js index c6412ca..6376230 100644 --- a/dist/index.js +++ b/dist/index.js @@ -190,6 +190,33 @@ class SheetQueryBuilder { }); this.clearCache(); return this; + } + patchRows(updateFn) { + const rows = this.getRows(); + for (let i = 0; i < rows.length; i++) { + this.patchRow(rows[i], updateFn); + } + this.clearCache(); + return this; + } + /** + * Patch single row + */ + patchRow(row, updateFn) { + const rowMeta = row.__meta; + delete row.__meta; + const sourceRow = {...row}; // shallow copy + const updatedRow = updateFn(row) || row; + const headings = this.getHeadings(); + // Write those cells only where values were changed + headings.forEach((heading, col) => { + const newValue = updatedRow[heading]; + if (newValue !== sourceRow[heading]){ + const updateRowCell = this.getSheet().getRange(rowMeta.row, col + 1) + updateRowCell.setValues([[newValue]]); + } + }); + return this; } /** * Update matched rows in spreadsheet with provided function