日期过去的第二天删除行

发布于 2025-01-26 15:11:37 字数 671 浏览 3 评论 0原文

我在脚本写作方面仍然是相当新的,并且正在尝试写一个脚本,该脚本在第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2025-02-02 15:11:37

对于您的第一个问题:

第一个是我收到错误消息“无法读取NULL的属性'getrange',即使我已经描述了我希望它从中获取数据的数组。

第一个是我收到错误消息“无法读取null的属性'getrange',即使我描述了我 变量不正确

。 代替dreversheetapp.getactive()

dreversheetapp.openbyurl( ),只需

// var values = sheet.getRange(2,1,5,12);
var values = sheet.getRange(2,1,5,12).getValues();

在您的第二个问题上声明

比较日期不是那么削减

的解决方案 。 strong>是将Current-Date作为格式的字符串,类似于您在日期列中使用的字符串。 06 2022 ):

const values = sheet.getRange(2,1,5,12).getDisplayValues();

const currentDate = Utilities.formatDate(new Date(), `YOUR_TIMEZONE`, `MMM-dd YYY`)

然后,您可以使用您写的for循环删除日期的所有行与当前日期不匹配的所有行:

  for (let i = lastrow; i > 2; i--) {
    if (values[i-1][2] !== currentDate) {
      sheet.deleteRow(i)
    }
  }

如果您想删除 专门的日期1天大,您将必须使用日期比较,这可以完成几种方法。

这是一个解决方案:

const currentDate = new Date();

for (let i = lastrow; i > 2; i--) {
  if (currentDate.getDate()-1 === values[i-1][2].getDate()) {
    sheet.deleteRow(i)
  }
}

解决方案

日期作为字符串方法:

function DeleteOldEntries() {

  const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
  const values = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getDisplayValues();

  const currentDate = Utilities.formatDate(new Date(), "YOUR_TIMEZONE", "m/d/yyyy");

  for (let i = values.length-1; i >= 0; i--) {
    if (values[i][11] !== currentDate) {
      sheet.deleteRow(i+2)
    }
  }

}

日期比较:(严格的一天前)

function DeleteOldEntries() {

  const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
  const values = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();

  const currentDate = new Date();

  for (let i = values.length; i >= 0; i--) {
    if (currentDate.getDate()-1 === values[i][11].getDate()) {
      sheet.deleteRow(i+2);
    }
  }

}

For your first issue:

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.

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() or SpreadsheetApp.openByUrl() in place of SpreadsheetApp.getActive().

On a side note, you are also not retrieving any values in values, rather, just declaring a range.

// var values = sheet.getRange(2,1,5,12);
var values = sheet.getRange(2,1,5,12).getValues();

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

const values = sheet.getRange(2,1,5,12).getDisplayValues();

const currentDate = Utilities.formatDate(new Date(), `YOUR_TIMEZONE`, `MMM-dd YYY`)

You can then remove all rows in your sheet that's date does not match the current date, using the for loop you wrote:

  for (let i = lastrow; i > 2; i--) {
    if (values[i-1][2] !== currentDate) {
      sheet.deleteRow(i)
    }
  }

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:

const currentDate = new Date();

for (let i = lastrow; i > 2; i--) {
  if (currentDate.getDate()-1 === values[i-1][2].getDate()) {
    sheet.deleteRow(i)
  }
}

Solutions

Date as String Method:

function DeleteOldEntries() {

  const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
  const values = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getDisplayValues();

  const currentDate = Utilities.formatDate(new Date(), "YOUR_TIMEZONE", "m/d/yyyy");

  for (let i = values.length-1; i >= 0; i--) {
    if (values[i][11] !== currentDate) {
      sheet.deleteRow(i+2)
    }
  }

}

Date Comparison: (Strictly one day ago)

function DeleteOldEntries() {

  const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet 1");
  const values = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();

  const currentDate = new Date();

  for (let i = values.length; i >= 0; i--) {
    if (currentDate.getDate()-1 === values[i][11].getDate()) {
      sheet.deleteRow(i+2);
    }
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文