Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"timeZone": "Europe/Moscow",
"dependencies": {
"libraries": []
},
"exceptionLogging": "STACKDRIVER"
}
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
34 changes: 32 additions & 2 deletions snippets/sheets/delete_move_rows_by_conditional/readme.md
Original file line number Diff line number Diff line change
@@ -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 >}}

<video controls width="100%" height="350px" autoplay="true" loop="true">
<source src="./screenrecord.mp4" type="video/mp4">
Sorry, your browser doesn't support embedded videos.
</video>

### Snippet

- {{< externalLink >}}
- {{< commentLink >}}
- {{< scrvizLink >}}

{{< codeFromFile "index.js" >}}

### Run it

{{< codeFromFile "run.js" >}}

### User actions

{{< codeFromFile "userActions.js" >}}

{{< clipboard >}}
5 changes: 0 additions & 5 deletions snippets/sheets/delete_move_rows_by_conditional/readme.ru.md

This file was deleted.

40 changes: 40 additions & 0 deletions snippets/sheets/delete_move_rows_by_conditional/run.js
Original file line number Diff line number Diff line change
@@ -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);
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* global run2 */

/**
* If you wish implement the snippet for EDIT event
*/
function onEdit() {
run2();
}