日期过去的第二天删除行
我在脚本写作方面仍然是相当新的,并且正在尝试写一个脚本,该脚本在第L列的日期之后的一天删除了一排。数据在A133至L117范围内。我从几个不同的[来源] [1]中获得了下面的代码,但是它有几个问题。第一个是我收到错误消息“无法在第3行上读取null的属性'getrange',即使我已经描述了我希望它从中删除数据的数组。
第二期是以下代码 - 我是我 您目前的需求。
function DeleteOldEntries() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
var values = sheet.getRange(2,1,5,12);
var currentDate = new Date();//today
for (i=lastrow;i>=3;i--) {
var tempDate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function
不确定这是正确的,并感谢
I am still fairly new at script writing and am trying a write a script that deletes a row one day after the date which is in column L. The data is in range A133 to L117. I've gotten the code below from several different [sources][1] but it has several issues. The first being that I get the error message "cannot read property 'getRange' of Null, on line 3 even though I've describe the array I want it to pull data from.
The second issue is with the below code - I'm not sure it's right, and would appreciate any help getting it to work for my current need. Here is the code.
function DeleteOldEntries() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
var values = sheet.getRange(2,1,5,12);
var currentDate = new Date();//today
for (i=lastrow;i>=3;i--) {
var tempDate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的第一个问题:
第一个是我收到错误消息“无法读取null的属性'getrange',即使我描述了我 变量不正确
。 代替
dreversheetapp.getactive()
。或
dreversheetapp.openbyurl( )
,只需在您的第二个问题上声明:
比较日期不是那么削减
的解决方案 。 strong>是将
Current-Date
作为格式的字符串,类似于您在日期列中使用的字符串。 06 2022 ):然后,您可以使用您写的for循环删除日期的所有行与当前日期不匹配的所有行:
如果您想删除 专门的日期1天大,您将必须使用日期比较,这可以完成几种方法。
这是一个解决方案:
解决方案
日期作为字符串方法:
日期比较:(严格的一天前)
For your first issue:
This error means your
sheet
variable is incorrect.This could mean your sheet name is incorrect, or that there is no active spreadsheet. If you are running this in a script file not attached to a Sheets file, you will need to use
SpreadsheetApp.openById()
orSpreadsheetApp.openByUrl()
in place ofSpreadsheetApp.getActive()
.On a side note, you are also not retrieving any values in
values
, rather, just declaring a range.For your second issue:
Comparing dates is not so cut and dry.
A possible solution to remove all rows that are not from today, would be to declare
currentDate
as a string with formatting similar to what you are using in your date columns use. As an example, if you're sheet dates look like (May-06 2022
):You can then remove all rows in your sheet that's date does not match the current date, using the for loop you wrote:
If you are looking to remove dates that are specifically 1 day old, you will have to use date comparison, which can be accomplished a few ways.
Here's one solution:
Solutions
Date as String Method:
Date Comparison: (Strictly one day ago)