Yan
Yan

Reputation: 13

How to run multiple onEdit functions in the same google script (google sheets)?

I've been trying many different ways and none worked.

I want a function that when someone types something, the content of the cell automatically turns upper case and eliminate all accents.

E.g.: if I type "áéîõÂÃüÚ", it immediately becomes "AEIOAAUU".

The following cases I've tried:

  1. Change the letter with accent for a normal letter then uppercase everything, in the same onEdit;
  2. Separate the functions in different onEdit;
  3. Just one onEdit that calls the functions in another script.

This is my code:

function onEdit(e){
  try{
    myA(e);
    myE(e);
    myUpper(e);
  }
  catch(e){
    if(e){}}
}

function myFunction(){
  var app= SpreadsheetApp;
  var targetSheet= app.getActiveSpreadsheet().getSheetByName("Sheet1");

  onEdit();
} 

The functions "myA", "myE" and "myUpper" are, each one, in separated scripts and they are:

function myUpper(e) {
   e.range.setValue(e.value.toUpperCase());
}

function myE(e) {
  e.range.setValue(e.value.replace(new RegExp("[[éèêëÉÈÊË]", 'g'),"E"));
}

function myA(e) {
  e.range.setValue(e.value.replace(new RegExp("[[áàâäãÁÀÂÄÃ]", 'g'),"A"));
}

Running this whole code, only the function "myE" works. In other words, only the letter "e" with accents turns into "E".

What am I doing wrong?

Thanks in advance.

Upvotes: 1

Views: 890

Answers (1)

ra89fi
ra89fi

Reputation: 1245

You can use one onEdit() trigger to do that.

function onEdit(e){
  var ss = e.source;
  var sheet = ss.getSheetByName('Sheet1');
  var value = e.value;
  var range = e.range;
  value = value.replace(new RegExp("[[éèêëÉÈÊË]", 'g'),"E");
  value = value.replace(new RegExp("[[áàâäãÁÀÂÄÃ]", 'g'),"A");
  value = value.toUpperCase();
  range.setValue(value);
}

Upvotes: 1

Related Questions