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

时间:2019-01-11 03:36:45

标签: google-apps-script google-sheets triggers

有人可以帮助我吗? 我的问题是我所有的三(3)个代码都具有相同的名称“ onEdit”。 我该如何运行其中的三(3)个?

有人建议我需要更改其名称,但是当我更改名称时,我的代码将无法运行。

谁能为我发布示例代码供我参考? 好吗?

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

1 个答案:

答案 0 :(得分:0)

两个函数合在一个onEdit()

这是一个将两个函数组合到一个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);
}

有很多不同的方法可以做到这一点。我希望尽快返回不涉及任何功能的图纸。

您可能已经知道,除非提供事件对象,否则无法直接运行这些onEdit()函数。

您的两个功能结合在一起

我稍微修改了第二个,因为这对我来说没有意义。因此您可以更改它,因为您可能了解自己想要的东西。

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

最好对组织进行返工,以便您可以更快地返回不涉及这两个功能的图纸。调试这些功能时,查看执行页面确实很有帮助。