Skip to content

Commit a4dddbd

Browse files
Merge pull request #77 from contributorpw/edits
Update sheets/delete_move_rows_by_conditional
2 parents 960178a + 305b5e1 commit a4dddbd

File tree

8 files changed

+87
-63
lines changed

8 files changed

+87
-63
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"timeZone": "Europe/Moscow",
3-
"dependencies": {
4-
"libraries": []
5-
},
6-
"exceptionLogging": "STACKDRIVER"
7-
}
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8"
6+
}

snippets/sheets/delete_move_rows_by_conditional/code.js renamed to snippets/sheets/delete_move_rows_by_conditional/index.js

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,6 @@
66
* {@link https://toster.ru/q/690651}
77
*/
88

9-
/**
10-
* If you wish implement this for EDIT
11-
*/
12-
// function onEdit() {
13-
// run2();
14-
// }
15-
16-
/**
17-
* Runs the snippet.
18-
* Removes rows by condition 'B:B=10'.
19-
* @ignore
20-
*/
21-
function run1() {
22-
var sheet = SpreadsheetApp.getActiveSheet();
23-
deleteRowsByConditional_(sheet, function(values, i) {
24-
return values[i][1] === 10;
25-
});
26-
}
27-
28-
/**
29-
* https://toster.ru/q/690651
30-
* Runs the snippet.
31-
* Removes rows by condition '(A:A<>"")*(B:B<>"")*(D:D<>"")*(F:F<>"")'. Appends deleted rows to the 'Archive' sheet.
32-
*
33-
*/
34-
function run2() {
35-
/* Remove dash */
36-
var sheet = SpreadsheetApp.getActiveSheet();
37-
if (sheet.getName() === 'Archive') return;
38-
var archive = SpreadsheetApp.getActive().getSheetByName('Archive');
39-
40-
var action = function(values, i, i2) {
41-
var data = values.slice(i, i + i2);
42-
archive
43-
.getRange(archive.getLastRow() + 1, 1, data.length, data[0].length)
44-
.setValues(data);
45-
};
46-
47-
var condition = function(values, i) {
48-
var row = values[i];
49-
return (
50-
i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== ''
51-
);
52-
};
53-
54-
deleteRowsByConditional_(sheet, condition, action);
55-
}
56-
579
/**
5810
* Removes rows from a sheet according to the condition
5911
*
@@ -76,14 +28,14 @@ function deleteRowsByConditional_(sheet, condition, action) {
7628
.getDataRange()
7729
.getValues()
7830
.forEach(
79-
function(_, i, arr) {
80-
var j = arr.length - i - 1;
31+
(_, i, arr) => {
32+
const j = arr.length - i - 1;
8133
if (this.condition.apply(null, [arr, j])) {
8234
this.isContinue++;
8335
if (j > 0) return;
8436
}
8537
if (this.isContinue > 0) {
86-
var prevPos = j + 1;
38+
const prevPos = j + 1;
8739
if (action) action(arr, prevPos, this.isContinue);
8840
this.sheet.deleteRows(prevPos + 1, this.isContinue);
8941
this.isContinue = 0;
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
---
2-
title: Deleting rows from a Google Sheet by condition
2+
title: 'Deleting rows by conditions'
3+
date: '2021-06-23'
4+
description: 'Removes or moves rows within Google Sheets by conditions'
5+
tags: ['sheets']
6+
categories: ['snippets']
7+
images: ['./snippets/sheets/delete_move_rows_by_conditional/screenshot.png']
38
---
49

5-
## Deleting rows from a Google Sheet by condition
10+
## Removes or moves rows within Google Sheets by conditions
11+
12+
{{< toc >}}
13+
14+
<video controls width="100%" height="350px" autoplay="true" loop="true">
15+
<source src="./screenrecord.mp4" type="video/mp4">
16+
Sorry, your browser doesn't support embedded videos.
17+
</video>
18+
19+
### Snippet
20+
21+
- {{< externalLink >}}
22+
- {{< commentLink >}}
23+
- {{< scrvizLink >}}
24+
25+
{{< codeFromFile "index.js" >}}
26+
27+
### Run it
28+
29+
{{< codeFromFile "run.js" >}}
30+
31+
### User actions
32+
33+
{{< codeFromFile "userActions.js" >}}
34+
35+
{{< clipboard >}}

snippets/sheets/delete_move_rows_by_conditional/readme.ru.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* global deleteRowsByConditional_ */
2+
3+
/**
4+
* Runs the snippet.
5+
* Removes rows from an active sheet by condition 'B:B=10'
6+
*/
7+
function run1() {
8+
const sheet = SpreadsheetApp.getActiveSheet();
9+
deleteRowsByConditional_(sheet, (values, i) => values[i][1] === 10);
10+
}
11+
12+
/**
13+
* Runs the snippet.
14+
* Removes rows by condition '(A:A<>"")*(B:B<>"")*(D:D<>"")*(F:F<>"")'.
15+
* Appends deleted rows to the 'Archive' sheet.
16+
*
17+
* @see https://toster.ru/q/690651
18+
*
19+
*/
20+
function run2() {
21+
const sheet = SpreadsheetApp.getActiveSheet();
22+
if (sheet.getName() === 'Archive') return;
23+
const archive = SpreadsheetApp.getActive().getSheetByName('Archive');
24+
25+
const action = (values, i, i2) => {
26+
const data = values.slice(i, i + i2);
27+
archive
28+
.getRange(archive.getLastRow() + 1, 1, data.length, data[0].length)
29+
.setValues(data);
30+
};
31+
32+
const condition = (values, i) => {
33+
const row = values[i];
34+
return (
35+
i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== ''
36+
);
37+
};
38+
39+
deleteRowsByConditional_(sheet, condition, action);
40+
}
-1.94 MB
Binary file not shown.
38.7 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* global run2 */
2+
3+
/**
4+
* If you wish implement the snippet for EDIT event
5+
*/
6+
function onEdit() {
7+
run2();
8+
}

0 commit comments

Comments
 (0)