javascript-Google Apps脚本:使用电子表格范围作为参数从菜单中调用函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-Google Apps脚本:使用电子表格范围作为参数从菜单中调用函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2092字,纯文字阅读大概需要3分钟。
内容图文
![javascript-Google Apps脚本:使用电子表格范围作为参数从菜单中调用函数](/upload/InfoBanner/zyjiaocheng/696/394900fab1f44cdc993f29e3c774600f.jpg)
我有一个函数,该函数接受电子表格范围作为参数,然后在与给定范围相同的行中添加日期.
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()来运行它,则会出现以下错误:
TypeError: Cannot call method “getValue” of undefined. (line 64, file
“Code”)
第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);
}
感谢您的回复:)
解决方法:
为什么不这样做呢?我认为您的参考文献存在问题,可以解决此问题.
菜单:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Read Data",
functionName : "readRows"
},{
name : "Set Date",
functionName : "autoDate"
}];
spreadsheet.addMenu("Script Center", entries);
}
自动约会功能:
function autoDate() {
var sheet = SpreadsheetApp.getActiveSheet();
var activeCell = sheet.getActiveCell();
var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
var currentDateStr = new String();
currentDateStr = currentDate.getDate() + "/" + currentMonth;
if (activeCell.getValue() == ""){
var activeRowInt = cell.getRow();
var dateCell = sheet.getRange(activeRowInt,3); //3 is my date column
dateCell.setValue(currentDateStr);
}
}
删除中间人的AutoDateMenu函数.
如果这不能满足您的目的,我们可以尝试其他方法.
内容总结
以上是互联网集市为您收集整理的javascript-Google Apps脚本:使用电子表格范围作为参数从菜单中调用函数全部内容,希望文章能够帮你解决javascript-Google Apps脚本:使用电子表格范围作为参数从菜单中调用函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。