Merging or Combining Two Onedit Trigger Functions

Merging or Combining two onEdit trigger functions

A script cannot contain two functions with the same name. Rename your first onEdit function to onEdit1 (actually it will be better to assign a descriptive name) and the second function as onEdit2, then put them both in one function named onEdit and pass the parameter e to both of them:

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

Related:

  • Two OnEdit functions not working together
  • Best Practices for Multiple OnEdit Functions
  • How to run multiple onEdit functions in the same google script (google sheets)?
  • Bracketing multiple onEdit functions

How do I combine two onEdit functions into one

Put both into a nested function under onEdit and call them.

function onEdit(e) {
first();
second();

function first() {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != "Submit" || r.columnStart != 44 || r.rowStart == 1) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
src.getRange(r.rowStart,1,1,43).copyTo(dest.getRange(dest.getLastRow()+1,1,1,43),{contentsOnly:true});
}

function second() {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != "Watchlist" || r.columnStart != 11 || r.rowStart == 1) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchlist");
src.getRange(r.rowStart,1,1).copyTo(dest.getRange(r.getLastRow()+1,1,1),{contentsOnly:true});
src.getRange(r.rowStart,2,1).copyTo(dest.getRange(r.getLastRow()+1,2,1));
src.getRange(r.rowStart,3,1).copyTo(dest.getRange(r.getLastRow()+1,3,1));
src.getRange(r.rowStart,4,1).copyTo(dest.getRange(r.getLastRow()+1,4,1),{contentsOnly:true});
src.getRange(r.rowStart,5,1).copyTo(dest.getRange(r.getLastRow()+1,5,1));
src.getRange(r.rowStart,6,1).copyTo(dest.getRange(r.getLastRow()+1,6,1));
src.getRange(r.rowStart,7,1).copyTo(dest.getRange(r.getLastRow()+1,7,1));
src.getRange(r.rowStart,8,1).copyTo(dest.getRange(r.getLastRow()+1,8,1));
src.getRange(r.rowStart,9,1).copyTo(dest.getRange(r.getLastRow()+1,9,1),{contentsOnly:true});
src.getRange(r.rowStart,10,1).copyTo(dest.getRange(r.getLastRow()+1,10,1));
}

}

Two OnEdit functions not working together

Barry Smith's comment about "two functions with same name" is right; only the second would execute in that case.

You can have just one spreadsheet-contained function named onEdit(). If you want to use another function as an onEdit trigger, you need to set it up as an installable trigger.

You can use the dialog from Resources -> Current Project's Triggers to install the second trigger.

screenshot

Alternatively, you could have just one onEdit() simple trigger, but have it call the "sub-trigger" functions, passing the event object e to each of them.

Background: Guide to Triggers.

How to use multiple OnEdit functions across multiple Google Sheets

You can't have multiple onEdit functions.

Keep only one onEdit function:

function onEdit(e) {
addTimeStampGlossary(e);
addTimeStampTables(e);
addTimeStampFields(e);
}

and the other functions as well:

function addTimeStampGlossary(x){
// variables
var startRow = 2;
var ws = "Sheet1";

//get modified row and column
var row = x.range.getRow();
var col = x.range.getColumn();

if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 || col === 17 && row >= startRow && x.source.getActiveSheet().getName() === ws ){
x.source.getActiveSheet().getRange(row,18).setValue(new Date());
}

}

function addTimeStampTables(y){
// variables
var startRow = 2;
var ws = "Sheet2";

//get modified row and column
var row = y.range.getRow();
var col = y.range.getColumn();

if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 || col === 17 || col === 18 || col === 19 && row >= startRow && y.source.getActiveSheet().getName() === ws ){
y.source.getActiveSheet().getRange(row,20).setValue(new Date());
}

}

function addTimeStampFields(z){
// variables
var startRow = 2;
var ws = "Sheet3";

//get modified row and column
var row = z.range.getRow();
var col = z.range.getColumn();

if(col === 1 || col === 2 || col === 3 || col === 4 || col === 5 || col === 6 || col === 7 || col === 8 || col === 9 || col === 10 || col === 11 || col === 12 || col === 13 || col === 14 || col === 15 || col === 16 && row >= startRow && z.source.getActiveSheet().getName() === ws ){
z.source.getActiveSheet().getRange(row,17).setValue(new Date());
}

}


Related Topics



Leave a reply



Submit