diff --git a/snippets/sheets/delete_move_rows_by_conditional/appsscript.json b/snippets/sheets/delete_move_rows_by_conditional/appsscript.json index f1f01e7..130bd73 100644 --- a/snippets/sheets/delete_move_rows_by_conditional/appsscript.json +++ b/snippets/sheets/delete_move_rows_by_conditional/appsscript.json @@ -1,7 +1,6 @@ { "timeZone": "Europe/Moscow", - "dependencies": { - "libraries": [] - }, - "exceptionLogging": "STACKDRIVER" -} \ No newline at end of file + "dependencies": {}, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} diff --git a/snippets/sheets/delete_move_rows_by_conditional/code.js b/snippets/sheets/delete_move_rows_by_conditional/index.js similarity index 60% rename from snippets/sheets/delete_move_rows_by_conditional/code.js rename to snippets/sheets/delete_move_rows_by_conditional/index.js index 4b8984c..9f6e03d 100644 --- a/snippets/sheets/delete_move_rows_by_conditional/code.js +++ b/snippets/sheets/delete_move_rows_by_conditional/index.js @@ -6,54 +6,6 @@ * {@link https://toster.ru/q/690651} */ -/** - * If you wish implement this for EDIT - */ -// function onEdit() { -// run2(); -// } - -/** - * Runs the snippet. - * Removes rows by condition 'B:B=10'. - * @ignore - */ -function run1() { - var sheet = SpreadsheetApp.getActiveSheet(); - deleteRowsByConditional_(sheet, function(values, i) { - return values[i][1] === 10; - }); -} - -/** - * https://toster.ru/q/690651 - * Runs the snippet. - * Removes rows by condition '(A:A<>"")*(B:B<>"")*(D:D<>"")*(F:F<>"")'. Appends deleted rows to the 'Archive' sheet. - * - */ -function run2() { - /* Remove dash */ - var sheet = SpreadsheetApp.getActiveSheet(); - if (sheet.getName() === 'Archive') return; - var archive = SpreadsheetApp.getActive().getSheetByName('Archive'); - - var action = function(values, i, i2) { - var data = values.slice(i, i + i2); - archive - .getRange(archive.getLastRow() + 1, 1, data.length, data[0].length) - .setValues(data); - }; - - var condition = function(values, i) { - var row = values[i]; - return ( - i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== '' - ); - }; - - deleteRowsByConditional_(sheet, condition, action); -} - /** * Removes rows from a sheet according to the condition * @@ -76,14 +28,14 @@ function deleteRowsByConditional_(sheet, condition, action) { .getDataRange() .getValues() .forEach( - function(_, i, arr) { - var j = arr.length - i - 1; + (_, i, arr) => { + const j = arr.length - i - 1; if (this.condition.apply(null, [arr, j])) { this.isContinue++; if (j > 0) return; } if (this.isContinue > 0) { - var prevPos = j + 1; + const prevPos = j + 1; if (action) action(arr, prevPos, this.isContinue); this.sheet.deleteRows(prevPos + 1, this.isContinue); this.isContinue = 0; diff --git a/snippets/sheets/delete_move_rows_by_conditional/readme.md b/snippets/sheets/delete_move_rows_by_conditional/readme.md index 93b85f5..86ffb8a 100644 --- a/snippets/sheets/delete_move_rows_by_conditional/readme.md +++ b/snippets/sheets/delete_move_rows_by_conditional/readme.md @@ -1,5 +1,35 @@ --- -title: Deleting rows from a Google Sheet by condition +title: 'Deleting rows by conditions' +date: '2021-06-23' +description: 'Removes or moves rows within Google Sheets by conditions' +tags: ['sheets'] +categories: ['snippets'] +images: ['./snippets/sheets/delete_move_rows_by_conditional/screenshot.png'] --- -## Deleting rows from a Google Sheet by condition +## Removes or moves rows within Google Sheets by conditions + +{{< toc >}} + + + +### Snippet + +- {{< externalLink >}} +- {{< commentLink >}} +- {{< scrvizLink >}} + +{{< codeFromFile "index.js" >}} + +### Run it + +{{< codeFromFile "run.js" >}} + +### User actions + +{{< codeFromFile "userActions.js" >}} + +{{< clipboard >}} diff --git a/snippets/sheets/delete_move_rows_by_conditional/readme.ru.md b/snippets/sheets/delete_move_rows_by_conditional/readme.ru.md deleted file mode 100644 index 0b9c654..0000000 --- a/snippets/sheets/delete_move_rows_by_conditional/readme.ru.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: Удаление строк из Таблицы Гугл по условию ---- - -## Удаление строк из Таблицы Гугл по условию diff --git a/snippets/sheets/delete_move_rows_by_conditional/run.js b/snippets/sheets/delete_move_rows_by_conditional/run.js new file mode 100644 index 0000000..6363c87 --- /dev/null +++ b/snippets/sheets/delete_move_rows_by_conditional/run.js @@ -0,0 +1,40 @@ +/* global deleteRowsByConditional_ */ + +/** + * Runs the snippet. + * Removes rows from an active sheet by condition 'B:B=10' + */ +function run1() { + const sheet = SpreadsheetApp.getActiveSheet(); + deleteRowsByConditional_(sheet, (values, i) => values[i][1] === 10); +} + +/** + * Runs the snippet. + * Removes rows by condition '(A:A<>"")*(B:B<>"")*(D:D<>"")*(F:F<>"")'. + * Appends deleted rows to the 'Archive' sheet. + * + * @see https://toster.ru/q/690651 + * + */ +function run2() { + const sheet = SpreadsheetApp.getActiveSheet(); + if (sheet.getName() === 'Archive') return; + const archive = SpreadsheetApp.getActive().getSheetByName('Archive'); + + const action = (values, i, i2) => { + const data = values.slice(i, i + i2); + archive + .getRange(archive.getLastRow() + 1, 1, data.length, data[0].length) + .setValues(data); + }; + + const condition = (values, i) => { + const row = values[i]; + return ( + i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== '' + ); + }; + + deleteRowsByConditional_(sheet, condition, action); +} diff --git a/snippets/sheets/delete_move_rows_by_conditional/screenrecord.gif b/snippets/sheets/delete_move_rows_by_conditional/screenrecord.gif deleted file mode 100644 index 6c83a65..0000000 Binary files a/snippets/sheets/delete_move_rows_by_conditional/screenrecord.gif and /dev/null differ diff --git a/snippets/sheets/delete_move_rows_by_conditional/screenshot.png b/snippets/sheets/delete_move_rows_by_conditional/screenshot.png new file mode 100644 index 0000000..7aa7a4c Binary files /dev/null and b/snippets/sheets/delete_move_rows_by_conditional/screenshot.png differ diff --git a/snippets/sheets/delete_move_rows_by_conditional/userActions.js b/snippets/sheets/delete_move_rows_by_conditional/userActions.js new file mode 100644 index 0000000..812a979 --- /dev/null +++ b/snippets/sheets/delete_move_rows_by_conditional/userActions.js @@ -0,0 +1,8 @@ +/* global run2 */ + +/** + * If you wish implement the snippet for EDIT event + */ +function onEdit() { + run2(); +}