0
votes

I'm trying to create a custom onEdit function with google sheets script editor. I want to know if its possible to add an input parameter such as onEdit(e,row) where row is an integer I use to specify a target cell. Here is my noob code:

function onEdit(e,row) {

// writes the current date to the cell in column B on the same row when a cell in a specific column is edited

var sheetNameToWatch = "M2"; 
var columnNumberToWatch = /* column  */ 7; // column A = 1, B = 2, etc.

var ss = SpreadsheetApp.getActiveSpreadsheet();  // not used atm
var sheet = SpreadsheetApp.getActiveSheet();   // sheet on which changes are tracked
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GRID-TRACKING")  // sheet that contains formula cells
var range = sheet.getActiveCell();  // active cell is cell being edited

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) {
 var targetCell = sheet2.getRange(row, 2);  // I want the input parameter to control the "row" so I can autofill
 targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));   // writes current date to cell
 }
}

I'm using this to write the latest date of any edit on sheet "M2" column G to the sheet with formulas("GRID-TRACKING"). I put the formulas in column A and write the dates to column B. The problem is that my "row" variable is undefined...if I replace it with a fixed number and dont try to add the parameter at all it works fine, but I need a parameter I can autofill.

in the cell my formula is =onedit(cell,row)

Thanks,

Umpsy

2
You cannot feed any other parameters into onEdit. Do you just want it to happen on the same row? Why don't you utilize the event object passed?Robin Gertenbach
read the onEdit docs ao you know what its for.Zig Mandel

2 Answers

1
votes

Short answer

If you want to use two or more arguments use another name instead of onEdit for your function.

Explanation

On Google Apps Script, onEdit is a reserved function name . If you don't follow the guidelines to use the reserved function names you could get unexpected results.

References

0
votes

You cannot pass any other variables to onEdit. However, if you are using this as a formula, I don't really see why you would even need an onEdit trigger, it can just as successfully be thisFunc(). As I see in the code, you do not even use the event object so there is definitely no reason to have an onEdit. You also have to consider that it will start any time you edit any cell on the spreadsheet.