使用Google Apps冻结表格中的行会引发类型错误 [英] Freezing rows in Sheets with Google Apps throws type error

查看:66
本文介绍了使用Google Apps冻结表格中的行会引发类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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:无法调用未定义的方法setFrozenRows"(第6行,文件"freezeLabelRows")

"TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows")

根据Google文档,语法正确. 我正在从开发应用程序的工作表所附的代码编辑器中运行脚本. 我尝试了一个数字(1),其中numRowsFr现在;那是我过去用来规避此错误的解决方法.

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作为循环条件时,将在数组(工作表数组)的末尾循环.在最后一张工作表之后,frsheet将是未定义的,并且undefined没有setFrozenRows方法,因为它不是工作表类型.
  • 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.
      • 仅循环直到数组末尾.
      i <= numSheets - 1;
      

      i < numSheets;
      

      这篇关于使用Google Apps冻结表格中的行会引发类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆