onEdit several sheets

0

How do I make the onEdit function work independently on different sheets? You will see I made this script that works perfectly on the active sheet (Col Y). I have 8 sheets (different names) in which when modifying a specific column I stamp the date and time in the adjacent cell, for each sheet it is a different column. Try to do several functions onedit and none of them work. Thank you for your support.

function onEdit(event){
  var ColY = 25;  // Número de la Columna "Y"

  var changedRange = event.source.getActiveRange();
  if (changedRange.getColumn() == ColY) {
    // Una celda de la Columna Y ha sido editada
    var state = changedRange.getValue();
    var adjacent = event.source.getActiveSheet().getRange(changedRange.getRow(),ColY+1);
    var timestamp = new Date(); // Obtener la fecha actual
    // Dependiendo del valor de la celda será lo que se hará
    switch (state) {
      case 'Liquidado':
        // Escribir la fecha en la celda contigua
        adjacent.setValue(timestamp);
        break;
      case 'En Stock':
        adjacent.setValue(timestamp);
        break;
      default:
        // Algo que no se esperaba
        adjacent.setValue("*ERROR*");
        break
    }
  }
}
    
asked by Cristian Laureano 30.05.2018 в 17:12
source

1 answer

0
  

Adaptation of my publication in link

It is not clear to me what you want to achieve, but in relation to having several onEdit, I share the following:

  • In Google Apps Script / JavaScript an important concept is the scope. In a very general way, there are the global scope and the scope of the object.
  • All files in a Google Apps Script project share the same global scope
  • Because of the above and given that the files are loaded one by one in an order that only Google knows, if you define several functions with the same name, only the one loaded to the last one will come into effect.
  • Not directly related to the question, but it may help you to improve your script: it is more efficient to use event.range instead of event.source.getActiveRange () since you save a call to Google Apps Script services which is recommended to minimize the execution time.

        
    answered by 31.05.2018 в 15:30