Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.

Type of Edition (Cell or Range) - OnEdit

Hello,
This is my scenario:
I have a Google Sheet with 4 dropdowns (all depended one from each other) and I need to clean the next ones when one is edited... this I already have it.

However it shouldn't work always, just when the user changes the value manually for a specific one. If the user copies and pastes the values from another file (the 4 at same time) it shouldn't clean them.

This is the code I have so far... to control this I should know if it was edited a single cell or a range of cells, do you know how to know that?

function installedOnEdit(e) {

if(e.range.rowStart!=1) {
// create dynamic and filtered dropdowns
dropdown(e);

var s = SpreadsheetApp.getActive();
var r = s.getActiveCell();

// clean depended dropdowns when a single cell is edited

if( r.getColumn() == 1 ) {
r.offset(0, 1).setValue('');
r.offset(0, 2).setValue('');
r.offset(0, 3).setValue('');
}
if( r.getColumn() == 2 ) {
r.offset(0, 1).setValue('');
r.offset(0, 2).setValue('');
}
if( r.getColumn() == 3 ) {
r.offset(0, 1).setValue('');
}
}
}


Or maybe you have a best way to do it.
I appreciate your help.
Regards.

1 1 281
1 REPLY 1

To determine whether the user edited a single cell or pasted multiple values, you can use e.range.getNumRows() and e.range.getNumColumns() . If either of these is greater than 1, it indicates a multi-cell range, which often happens when the user pastes data.

Here’s how to modify your installedOnEdit function :-

function installedOnEdit(e) {
// Ignore edits to row 1 (header row)
if (e.range.getRow() !== 1) {
var sheet = e.source.getActiveSheet();
var activeCell = e.range;

// Check if a single cell is edited
if (activeCell.getNumRows() === 1 && activeCell.getNumColumns() === 1) {
// Clear dependent dropdowns based on the column edited
if (activeCell.getColumn() === 1) {
activeCell.offset(0, 1).setValue('');
activeCell.offset(0, 2).setValue('');
activeCell.offset(0, 3).setValue('');
} else if (activeCell.getColumn() === 2) {
activeCell.offset(0, 1).setValue('');
activeCell.offset(0, 2).setValue('');
} else if (activeCell.getColumn() === 3) {
activeCell.offset(0, 1).setValue('');
}
}
}
}

Top Labels in this Space
Top Solution Authors