C
C
CREWbig2019-11-30 19:16:20
Google Sheets
CREWbig, 2019-11-30 19:16:20

How to clear cells of selected range (A2:B28) when cell B2 is activated in google sheets?

Guys, help me write a script so that when cell B2 is active, cells A2: B28 are cleared?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2019-12-01
@ProgrammerForever

What does "active" mean? There is no "active cell" event, but there is a "cell changed" event - onEdit ( event )
Here is a code snippet for this function:

function onEdit(event) {
  //Возникает при изменении ячейки
  var ss = event.source.getActiveSheet();//Текущий лист
  	var address = event.range.getA1Notation().toUpperCase();//Адрес ячейки
  var row = event.range.getRow();							//Номер строки
  var col = event.range.getColumn();						//Номер столбца
  var newValue = event.value;								//Новое значение
  var oldValue = event.oldValue;							//Старое значение
  
  if (["Лист1","Лист2"].indexOf(ss.getName())==-1) return;	//Указываем на каких листах должен работать скрипт
  
  //Что-то делаем...
  };

Instead of "Doing something" enter the desired actions, for example, clearing the range. The code for cleaning can be done with a macro recorder Tools - Macros - Record macro
1) Record
2) Select range
3) Del
4) Stop recording
Or use this code:
But there is a point - this clearing on the same sheet can provoke onEdit () again and you need to deal with this by checking which range is changing before clearing something
if (address !="B2") return;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question