Adam Newman
Adam Newman

Reputation: 59

Both Google Sheets scripts work, but don't work when both added

I have 2 different scripts, the first one copies the row when I select the name of the sheet from the drop down list and pastes it on the sheet with that name, the second script is a checkbox that when ticked then copies the entire row to another sheet and deletes it from the original sheet. So after I have first selected the option from the dropdown menu and then ticked the checkbox that row should be copied to 2 different sheets.

My trouble is that although both these scripts work when they're on their own, they don't work when I have them both.

Script 1

function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Approval' && e.range.columnStart==11)  {
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
var tsh=e.source.getSheetByName(e.value);
if(tsh) {
  tsh.getRange(tsh.getLastRow() + 1,1,1,v[0].length).setValues(v);
  }
 }
}

Script 2

function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "Approval" && r.getColumn() == 12 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ZSD Sheet");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
 }
}

I've looked at script merging but it's going waaaaaaay over my head so any help is hugely appreciated.

Upvotes: 0

Views: 51

Answers (1)

Marios
Marios

Reputation: 27348

You can't have two or more onEdit() triggers. But you can do the following simple workaround. Add the following function to your scripts:

function onEdit(e) {
onEdit1(e)
onEdit2(e)  
}

and then, modify your scripts as follows:

Script 1:

function onEdit1(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Approval' && e.range.columnStart==11)  {
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
var tsh=e.source.getSheetByName(e.value);
if(tsh) {
  tsh.getRange(tsh.getLastRow() + 1,1,1,v[0].length).setValues(v);
  }
 }
}

Script 2:

function onEdit2(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "Approval" && r.getColumn() == 12 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ZSD Sheet");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
 }
}

Upvotes: 2

Related Questions