I have a function that accepts a spreadsheet range as a parameter, then adds a date in the same row as the given range.
function autoDate(cell) {
var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
var currentDateStr = new String();
currentDateStr = currentDate.getDate() + "/" + currentMonth;
var sheet = SpreadsheetApp.getActiveSheet();
if (cell.getValue() == ""){
var activeRowInt = cell.getRow();
var dateCell = sheet.getRange(activeRowInt,3); //3 is my date column
dateCell.setValue(currentDateStr);
}
}
I have a menu where I run a separate function that calls autoDate() with the active cell as the parameter (since the menu system doesn't allow calling functions with parameters)
function autoDateMenu(){
var sheet = SpreadsheetApp.getActiveSheet();
var activeCell = sheet.getActiveCell();
autoDate(activeCell);
}
If I run autoDateMenu() from within the script editor, it works fine. If I run autoDateMenu() by selecting it from the menu of the spreadsheet, I get the following error:
TypeError: Cannot call method "getValue" of undefined. (line 64, file "Code")
Line 64 refers to this line:
if (cell.getValue() == ""){
Here's the code I wrote for the menu:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Read Data",
functionName : "readRows"
},{
name : "Set Date",
functionName : "autoDateMenu"
}];
spreadsheet.addMenu("Script Center", entries);
}
Thanks for responding :)