嘿,我正在开发一个谷歌脚本程序,供朋友在即将到来的梦幻棒球赛季中使用,并且我得到错误
" ReferenceError:" range"未定义。"
我已经查了过来,并没有发现任何有帮助的东西。我的大部分编程技巧都是自学成才的,所以他们有点不稳定,有些事情让我无法理解,直到我让它工作一次。如果你有一个解决方案或任何想法有什么不对,任何帮助将不胜感激,对错误的解释会更好!谢谢。我还没有实施除C以外的任何案例,因为我还在测试。
//var rowsToHide = new Array(); possibly more effiecent and easier to just hide directly
var pos = "";
//var dataStartRow = 1; would like to add a adjustabke data start spot for use through any spreadsheet
var cellID = "A";
var cellInfo = "";
var cellInfoFirstHalf = "";
var cellInfoSecondHalf = "";
var forwardSlashIndex;
var doublePos = 0; //0 for false, 1 for true
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formula = activeRange.getFormula();
//sets up menu item to use script
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Hide Menu')
.addItem('Show Catchers', 'showCatchers')
.addItem('Show First Base', 'showFirstBase')
.addItem('Show Second Base', 'showSecondBase')
.addItem('Show Third Base', 'showThirdBase')
.addItem('Show Short Stop', 'showShortStop')
.addItem('Show Outfield', 'showOutfield')
.addToUi();
}
//checks which cells need to be hidden
function hideData(posCase) {
switch(posCase) {
case "C":
for (var i=3; i <= range.getNumRows(); i++) {
doublePos = 0;
cellID = "A" + i;
cellInfo = sheet.getRange(cellID).getValue();
forwardSlashIndex = cellInfo.search("/");
if (forwardSlashIndex != -1) {
cellInfoFirstHalf = cellInfo.substring(0,forwardSlashIndex);
cellInfoSecondHalf = cellInfo.substring(forwardSlashIndex + 1);
doublePos = 1;
};
if (doublePos == 0) {
if (cellInfo != "C") {
log(i);
//sheet.hideRows(i, 1);
};
} else {
if (cellInfoFirstHalf == "C" || cellInfoSecondHalf == "C") {
log(i);
//sheet.hideRows(i, 1);
};
};
};
break;
case "1B":
break;
case "2B":
break;
case "3B":
break;
case "SS":
break;
case "OF":
break;
default:
window.alert("There was an error, debugging required!");
break;
};
};
//multiple functions for the multiple menu buttons
function showCatchers() {
pos = "C";
hideData(pos);
};
function showFirstBase() {
pos = "1B";
};
function showSecondBase() {
pos = "2B";
};
function showThirdBase() {
pos = "3B";
};
function showShortStop() {
pos = "SS";
};
function ShowOutfield() {
pos = "OF";
};
&#13;
答案 0 :(得分:0)
此错误消息表示您尝试访问未声明的变量,即没有var
语句的变量。 (它确实应该说“未声明”而不是“未定义”,但这只是它在javascript中的方式。)我只在您的脚本中看到range
出现一次,并且没有声明它。这就是说for (var i=3; i <= range.getNumRows(); i++) {
的行。
也许你打算写activeRange.getNumRows()
。或许你打算在脚本的顶部写var range = ... ;
。这就像我现阶段可以告诉你的那样......希望你能自己完成剩下的工作。