D
D
Dofre2020-04-08 06:38:40
Autocomplete
Dofre, 2020-04-08 06:38:40

Google Sheets How to restrict onEdit trigger?

I have never been involved in writing scripts before, following the example of grief, I built this one in half. I'm trying to make it so that when adding information to the last row of the table in column F, the trigger fires and fills columns B, E, H (copied the information from the line above this one). But the trigger itself is more generous and fires when changes are made anywhere in the sheet (if you insert information not right next to the table, but, for example, after 5 rows, then all the information in all three columns simply disappears) - this is problem number 1. Problem number two , lies in the fact that the script itself works with the entire column at once, and not with a specific line above its head (I see how the entire column disappears and reloads, instead of taking the information from the cell above).

function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
  var cell = activeSheet.getRange(activeSheet.getLastRow()-1, 2);
  var destination = activeSheet.getRange(2, 2, activeSheet.getLastRow()-1, 1);
cell.copyTo(destination);
  
var cell1 = activeSheet.getRange(activeSheet.getLastRow()-1, 5);
  var destination1 = activeSheet.getRange(2, 5, activeSheet.getLastRow()-1, 1);
cell1.copyTo(destination1);

var cell2 = activeSheet.getRange(activeSheet.getLastRow()-1, 8);
  var destination2 = activeSheet.getRange(2, 8, activeSheet.getLastRow()-1, 1);
cell2.copyTo(destination2);

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-04-08
@Dofre

The e parameter has a lot of useful stuff. Just on it, you can limit the action of the script to some sheets or individual cells, or according to more complex conditions.

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;	//Указываем на каких листах должен работать скрипт
  
  //Что-то делаем...
  };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question