1
votes

I have these scripts working separately but when I combine them or create two different scripts only one will function. What am I missing?

function onEdit(e) {

var sheet = e.source.getActiveSheet(),

    editCols = [20, 21, 22, 23, 24, 25, 26, 27]

if (sheet.getName() !== 'THANG11' || editCols.indexOf(e.range.columnStart) ==

    -1 || e.range.rowStart < 3 || e.range.rowStart > 6) 

if (sheet.getName() !== 'THANG12' || editCols.indexOf(e.range.columnStart) ==

    -1 || e.range.rowStart < 3 || e.range.rowStart > 6)   

    return;

sheet.getRange("I1").setValue(new Date());
}
1
you have an compile error on assignment of sheet variable, remove the , at the endConstantin Trepadus
what error do you get?CocoNess
@ConstantinTrepadus How to fix it ? Do you know? Thanks.Phong Hoang
I can see only one script in your question. Can you show what your separate working scripts look like?ziganotschka
@CocoNess Sheet name "THANG11" ok => "THANG12" not work. And contrary.Phong Hoang

1 Answers

1
votes

There are two possibilities to merge your if statements.

First possibility

function onEdit(e) {
  ...
  if (sheet.getName() != 'THANG11' || ...){
    return;
    } 
  if (sheet.getName() != 'THANG12' || ...){  
    return;
  }
  sheet.getRange("I1").setValue(new Date());
  ...
}

In this case you implement two separate if statements and specify within curly brackets the block of code to execute if the respective condition is fulfilled.

In your case, the code to execute is identical for both coditions (return), thus you can implement both conditions within one if statement:

Second possibility

function onEdit(e) {
  ...
  if (sheet.getName() != 'THANG11' || sheet.getName() != 'THANG12' ||...){
    return;
    }
  sheet.getRange("I1").setValue(new Date());
  ...
}