我有一个函数,它接受电子表格范围作为参数,然后在给定范围的同一行中添加日期。
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);
}
}
我有一个菜单,我在其中运行一个单独的函数,该函数以活动单元格作为参数调用 autoDate() (因为菜单系统不允许使用参数调用函数)
function autoDateMenu(){
var sheet = SpreadsheetApp.getActiveSheet();
var activeCell = sheet.getActiveCell();
autoDate(activeCell);
}
如果我从脚本编辑器中运行 autoDateMenu() ,它工作正常。
如果我通过从电子表格的菜单中选择 autoDateMenu() 来运行它,则会收到以下错误:
类型错误:无法调用未定义的方法“getValue”。 (第 64 行,文件
“代码”)
第 64 行引用了这一行:
if (cell.getValue() == ""){
这是我为菜单编写的代码:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Read Data",
functionName : "readRows"
},{
name : "Set Date",
functionName : "autoDateMenu"
}];
spreadsheet.addMenu("Script Center", entries);
}
感谢您的回复:)