0

Can anyone help me do this? My problem is that all of my three (3) codes has the same name "onEdit". How can I run three (3) of of them?

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);
       }
   }
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Actually all of the functions in a project have to have unique names. So if you wish to use onEdit() as a [simple trigger](https://developers.google.com/apps-script/guides/triggers/) then you need to combine them all into one function. Or as an alternative you can give them unique and different names by running them from an [installable onEdit() trigger](https://developers.google.com/apps-script/guides/triggers/installable). But keep in mind they all have to work together because the trigger is fired when any edit is made to any sheet. – Cooper Jan 11 '19 at 04:31
  • how to combine them in one function? can you give me an example by posting your codes? – Jay Gabronino Jan 11 '19 at 05:56
  • It's said that it's too long. – Jay Gabronino Jan 11 '19 at 06:28
  • Is there any onther way I can contact you? – Jay Gabronino Jan 11 '19 at 06:50
  • I went ahead and provided an example that includes two functions into one onEdit() simple trigger. – Cooper Jan 11 '19 at 07:25
  • Are they both meant to work on all sheets (tabs)? – Cooper Jan 11 '19 at 07:54
  • ahm, actually I don't know, but I want to run it in "sheet1" – Jay Gabronino Jan 11 '19 at 07:56
  • 1
    Possible duplicate of [combining two onEdit Functions](https://stackoverflow.com/questions/47992189/combining-two-onedit-functions) – tehhowch Jan 11 '19 at 12:16

1 Answers1

0

Two functions in one onEdit()

Here's an example that combines two functions into one onEdit() simple trigger.

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.

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.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • TypeError: Cannot read property "source" from undefined. (line 7, file "Code")Dismiss – Jay Gabronino Jan 11 '19 at 08:57
  • 1
    You can’t run these functions without an event object. – Cooper Jan 11 '19 at 09:07
  • Master, can you make my blinker set according to the value of the column? Example: Column A If the cell in column A has " Yes " the range of row will blink? And can you make it also the blinking time has no limit? what I mean is it will never stop on blinking? – Jay Gabronino Jan 12 '19 at 02:46
  • I'm not sure what you mean by blinking. oE2 alternates the background color for rows 1, 2 and 3. Do you want to change the background color based upon the value in some column? – Cooper Jan 12 '19 at 04:04
  • Yes, that's what I mean hehe can you also set the codes just like my post? for me to avoid facing problem on source? because I dont know how to edit it – Jay Gabronino Jan 12 '19 at 05:32
  • Can you provide the colors in this format '#ff0000' which is red for all the colors that you wish to use and figure out how column values correlate to colors. – Cooper Jan 12 '19 at 06:03
  • I think red is enough, I want column A cells, A to M row will blink. my example of value is "DONE" – Jay Gabronino Jan 12 '19 at 06:25
  • I'd appreciate it if you will check off this answer to close the question. – Cooper Jan 12 '19 at 06:42
  • :) Sorry, I forgot! – Jay Gabronino Jan 12 '19 at 06:47