简体   繁体   English

如何在同一个谷歌脚本(谷歌表)中运行多个 onEdit 函数?

[英]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.我想要一个功能,当有人输入内容时,单元格的内容会自动变为大写并消除所有重音。

Eg: if I type "áéîõÂÃüÚ", it immediately becomes "AEIOAAUU".例如:如果我输入“áéîõÂÃüÚ”,它会立即变成“AEIOAAUU”。

The following cases I've tried:我尝试过以下情况:

  1. Change the letter with accent for a normal letter then uppercase everything, in the same onEdit;在同一个 onEdit 中将带有重音的字母更改为普通字母然后大写所有字母;
  2. Separate the functions in different onEdit;将不同onEdit中的功能分开;
  3. Just one onEdit that calls the functions in another script.只有一个 onEdit 调用另一个脚本中的函数。

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:函数 "myA"、"myE" 和 "myUpper" 分别位于单独的脚本中,它们是:

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.运行这整个代码,只有函数“myE”有效。 In other words, only the letter "e" with accents turns into "E".换句话说,只有带重音的字母“e”才会变成“E”。

What am I doing wrong?我究竟做错了什么?

Thanks in advance.提前致谢。

You can use one onEdit() trigger to do that.您可以使用一个onEdit()触发器来做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM