简体   繁体   English

谷歌脚本删除行

[英]Google script to delete rows

when I use the deleteRows function I get the error: TypeError: Cannot read property 'deleteRows' of null at clearRange(Code:4:4)当我使用 deleteRows function 时出现错误:TypeError: Cannot read property 'deleteRows' of null at clearRange(Code:4:4)

here is my code:这是我的代码:

function clearRange() {
var ss = SpreadsheetApp.getActive();
// Rows start at "2" -  delete of N rows (400)
ss.deleteRow(2, 400);
}

Can you please help me to solve this issue?你能帮我解决这个问题吗?

Thanks and regards Thierry谢谢和问候蒂埃里

As Andres has said, your script may not be attached to your sheet, and you will have to go to the sheet in question and click Tools > Script Editor .正如安德烈斯所说,您的脚本可能不会附加到您的工作表上,您必须将 go 转到相关工作表,然后单击Tools > Script Editor

However, there are two other possible issues -但是,还有另外两个可能的问题 -

  • The function is named deleteRows and not deleteRow function 被命名为deleteRows而不是deleteRow
  • You're calling deleteRows() on a Spreadsheet object.您在Spreadsheet object 上调用deleteRows() It is a Sheet function.它是一张Sheet function。

If these were the issues, this code should fix it.如果这些是问题,这段代码应该可以解决它。

function clearRange() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  // Rows start at "2" -  delete of N rows (400)
  sheet.deleteRows(2, 400);
}

Your ss spreadsheet variable is null which means your script is not bound to any spreadsheet as it should be to use the getActive method.您的ss电子表格变量是null这意味着您的脚本没有绑定到任何电子表格,因为它应该使用getActive方法。 You can create a container bound script following the steps explained here :您可以按照此处说明的步骤创建容器绑定脚本:

To create a bound script, open a Google Sheets, Docs, Slides, or Forms file, then select Tools > Script editor.要创建绑定脚本,请打开 Google 表格、文档、幻灯片或 Forms 文件,然后打开 select 工具 > 脚本编辑器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM