使用 Google Apps 冻结表格中的行会引发类型错误 [英] Freezing rows in Sheets with Google Apps throws type error
问题描述
我正在尝试使用 GAS 来冻结每张纸的顶行.它可以工作,冻结所需的行,但返回错误:
I'm trying to use GAS to freeze the top row of each sheet. It works, freezes the desired rows, but returns an error:
"TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows")
"TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows")
根据谷歌文档,语法是正确的.我正在从附加到我正在开发应用程序的工作表的代码编辑器中运行脚本.我尝试了 numRowsFr 现在所在的数字 (1);这是我用来避免此错误的解决方法.
According to Google documentation, the syntax is correct. I'm running the script from the code editor attached to the sheet where I'm developing the app. I tried a number (1) where numRowsFr is now; that was a workaround I used to dodge this error.
function rowFreeze() {
var numSheets = SpreadsheetApp.getActiveSpreadsheet().getNumSheets();
for(var i = 0; i <= numSheets; i++) {
var frSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[i];
var numRowsFr = 1;
frSheet.setFrozenRows(numRowsFr);
}
}
正如我所说,代码可以冻结每张纸上所需的行,但会返回错误.我想让这个应用程序的其余部分就位,以便为当前用户升级.
As I said, the code works to freeze the desired row on each sheet, but returns an error. I'd like to get the rest of this app in place to upgrade for current users.
推荐答案
问题:
- 数组索引从 0 开始,以数组 -1 的长度结束.当您使用
<=numSheets
作为循环条件时,您在数组(sheets 数组)结束后循环.在最后一个工作表之后,frsheet
将是未定义的,并且undefined
没有setFrozenRows
方法,因为它不是工作表类型. - Array index starts at 0 and ends at length of array -1. You're looping after the end of the array(sheets array) when you use
<=numSheets
as the loop condition. After the last sheet,frsheet
will be undefined andundefined
doesn't have asetFrozenRows
method as it's not a sheet type. - 只循环到数组末尾.
Issue:
i <= numSheets - 1;
或
i < numSheets;
这篇关于使用 Google Apps 冻结表格中的行会引发类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!