简体   繁体   English

如何将“ onEdit”名称更改为其他名称?

[英]How can I change the name “onEdit” to other name?

Can anyone help me do this? 有人可以帮我吗? My problem is that all of my three (3) codes has the same name "onEdit". 我的问题是我所有的三(3)个代码都具有相同的名称“ onEdit”。 How can I run three (3) of of them? 如何运行其中三(3)个?

There is a suggestion that I need to change their name, but when I change the names my codes wont run. 有建议,我需要更改其名称,但是当我更改名称时,我的代码将无法运行。

Can anyone post an example code for me, for my reference? 谁能为我发布示例代码供我参考? Please? 请?

function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
  var lastRow = sheet.getLastRow();
  for (p=1 ; p<=lastRow ; p++) { // p <= lastRow
       var status = sheet.getRange("C"+p).getValue(); // Change P  to the completed column
       if (status == "no") {                                           // status == "no"
         sheet.hideRows(p);
       }
   }
}
function onEdit() {
   var spreadsheet = SpreadsheetApp.getActive();
   var sheet = spreadsheet.getActiveSheet();
   var cell = spreadsheet.getActiveCell();
   var col = cell.getColumn();
   var row = cell.getRow();
   var rows = [1, 2, 3];
   // This is a list of the rows that should blink if edited.
   if (col === 7 && rows.indexOf(row) !== -7 && sheet.getName() === 'Sheet1') {
   // If the edited cell is in column A (1) and if the edited cell
   // is one of the rows listed
      for (var num = 0; num < 50; num++) {
           var colour = num%2 === 0
               ? 'GOLD'
               : 'WHITE';
           // Using ? and : like this is called a ternary operation. It's a
           // shorter form of if. ifStatement ? true : false.
           sheet.getRange('G' + row + ':G' + row).setBackground(colour);
           // Get the range for the edited row and set the bg colour
           SpreadsheetApp.flush();
           Utilities.sleep(500);
       }
   }
}

Two functions in one onEdit() 一个onEdit()中的两个函数

Here's an example that combines two functions into one onEdit() simple trigger. 这是一个将两个函数组合为一个onEdit()简单触发器的示例。

function onEdit(e){
  var ss=e.source;
  var rg=e.range;
  var sh=rg.getSheet();
  var name=sh.getName();
  Logger.log('Name: %s',name);
  var includedSheets=['Sheet45','Sheet46']
  if(includedSheets.indexOf(name)==-1){
    return;
  }
  if(name=='Sheet45'){
    Sheet45(e);//You could put the code right here but I wanted to make it clear that it's two different operation.  This one deletes a row if all conditions are met.
  }
  if(name=='Sheet46'){
    Sheet46(e);//This one deletes a column if all conditions are met
  }
}

function Sheet45(e){//You can name them whatever you want
  var sh=e.range.getSheet();
  var row=e.range.getRow();
  var col=e.range.getColumn();
  Logger.log('Name: %s',e.range.getSheet().getName());
  if(col==1 && row==4 && e.value=='delete'){//if column1 and row4 is changed to 'delete'
    sh.deleteRow(row);//the it deletes row 4
  }
}

function Sheet46(e){
  var sh=e.range.getSheet();
  var row=e.range.getRow();
  var col=e.range.getColumn();
  Logger.log('Name: %s',e.range.getSheet().getName());
  if(row==1 && col==4 && e.value=='delete'){//if column4 and row1 is changed to 'delete'
    var rg=sh.getDataRange();//then it deletes column 4
    var vA=rg.getValues();
    for(var i=0;i<vA.length;i++){
      vA[i].splice(3,1);
      Logger.log('vA[%s]: %s',i,vA[i]);
    }
  }
  rg.clear();
  sh.getRange(1,1,vA.length,vA[0].length).setValues(vA);
}

There are a lot of different ways to do it. 有很多不同的方法可以做到这一点。 I like to return as quickly as possible on sheets that are not involved in any of the functions. 我希望在不涉及任何功能的工作表上尽快返回。

You probably already know that you can't run these onEdit() functions directly unless you provide the event object. 您可能已经知道,除非提供事件对象,否则无法直接运行这些onEdit()函数。

Your two functions combined 您的两个功能相结合

I modified the second one a bit because it didn't make sense to me. 我修改了第二个,因为这对我来说没有意义。 So you change it since you probably understand what you want. 因此,您可以更改它,因为您可能了解自己想要什么。

function onEdit(e){
  oE1(e);
  oE2(e);
}

function oE1(e) {//this works on all sheet
  var ss=e.source;
  var rg=e.range;
  var sh=rg.getSheet();
  var name=sh.getName();
  var lastRow = sh.getLastRow();
  for(var row=1;row<=lastRow;row++) {
    var status=sh.getRange(row,3).getValue();//Column3 is C
       if (typeof(status)=="string" && status.toLowerCase()=="no") {                                   
         sh.hideRows(row);
       }
   }
}

function oE2(e) {//this only works on sheet 1
  var ss=e.source;
  var rg=e.range
  var sh=rg.getSheet();
  var row=rg.getRow();
  var col=rg.getColumn();
  var cell=sh.getRange(row,col);
  var rows = [1, 2, 3];
  if(col==1 && rows.indexOf(row)!=-1 && sh.getName()=='Sheet1') {
    sh.getRange(row,7).setBackground((rows.indexOf(row)%2==0)?'Gold':'White');
  }
}

It would be nice to rework the organization so that you can return more quickly for sheets that are not involved with either function. 最好对组织进行返工,以便您可以更快地返回不涉及这两个功能的工作表。 It's really helpful to look at the executions page when debugging these functions. 调试这些功能时,查看执行页面确实很有帮助。

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

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