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

风格不统一 提交于 2020-06-29 04:07:17

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/55262195/how-to-run-multiple-onedit-functions-in-the-same-google-script-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!