Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

arrays - Freezing rows in Sheets with Google Apps throws type error

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")

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue:

  • 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 and undefined doesn't have a setFrozenRows method as it's not a sheet type.

Solution:

  • Loop only till the end of the array.

Snippet:

i <= numSheets - 1;

or

i < numSheets;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...