简体   繁体   English

如何自动删除包含从现在开始偏移过去 3 天以上的日期的行?

[英]How to automatically delete rows that contain a date with more than 3 days offset to the past from now?

I am trying to get a script going that will automatically delete rows with a date over 3 days old (4 days on)我正在尝试运行一个脚本,该脚本将自动删除日期超过 3 天(4 天)的行

I found this script that I was hoping to be able to adapt:我发现了这个我希望能够适应的脚本:

function DeleteOldEntries() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //give your sheet name below instead of Sheet1
  var sheet = ss.getSheetByName("Foglio1");

  var datarange = sheet.getDataRange();
  var lastrow = datarange.getLastRow();


  var currentDate = new Date();
  var oneweekago = new Date();
  oneweekago.setDate(currentDate.getDate() - 7);

  for (i = lastrow; i >= 2; i--) {
    var tempdate = sheet.getRange(i, 1).getValue();

    if (tempdate < oneweekago) {
      sheet.deleteRow(i);
    }
  }
}

But there seems to be an error in the script itself that I need to figure out before adapting it to 4 days instead of 7 (that part is easy)但是脚本本身似乎有一个错误,我需要在将其调整为 4 天而不是 7 天之前弄清楚(这部分很容易)

My sheet has 3 columns with the date in column C if that info is helpful如果该信息有帮助,我的工作表有 3 列,日期在 C 列

"date in column C, if that info is helpful" “C 列中的日期,如果该信息有帮助的话”

It is indeed !确实是 ! instead of trying to get a date in column A as it it done in this line :而不是像在这一行中那样尝试在 A 列中获取日期:

var tempdate = sheet.getRange(i, 1).getValue();

you should look at the value in column C like this :您应该像这样查看 C 列中的值:

var tempdate = sheet.getRange(i, 3).getValue();

but to be more efficient you should do these comparisons at array level, try it like below and it will run much faster ...但为了更有效,你应该在数组级别进行这些比较,像下面一样尝试它,它会运行得更快......

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Foglio1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array

var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);

for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]

if(tempdate < oneweekago)  
{
  sheet.deleteRow(i);
}
}
}

It will be much much faster if your rows are in order of date, newest at the top and you delete all the rows at once and not one at a time.如果您的行按日期顺序排列,最新的在顶部并且您一次删除所有行而不是一次删除一个,那么速度会快得多。

function deleteOldData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("CallHub1");
  var datarange = sheet.getDataRange();
  var lastrow = datarange.getLastRow();
  var values = datarange.getValues();// get all data in a 2D array

  var currentDate = new Date();
  var daysago = new Date().setDate(currentDate.getDate() - 3);
  var yearago = new Date().setDate(currentDate.getDate() - 365);


  for (i=lastrow;i>=2;i--) {
    var tempdate = values[i-1][0];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
    if(tempdate < daysago)  {
      if(tempdate < yearago)  {continue;}
      sheet.deleteRows(1, i);
      break;
    }
  }
}

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

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