简体   繁体   English

在今天的日期应用脚本之前删除电子表格中的日期

[英]delete date in spreadsheet prior today's date apps script

I need to delete rows where date is before today. 我需要删除日期在今天之前的行。

I have something like this: 我有这样的事情:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var data = sheet.getDataRange().getValues(); 

var today = new Date();
var date = Utilities.formatDate(new Date(today), "GMT", "yyyy-MM-dd HH:mm:ss");
// logs date 2013-06-12 19:23:53

for(n=1;n<data.length;++n){
  var start = data[n][0];
  //logs start  Wed Jul 10 19:00:00 GMT-07:00 2013
}

Thanks! 谢谢!

You don't show any of your data, so I'm guessing a bit. 您没有显示任何数据,所以我有点猜测。

It appears that the date format of the logs in your spreadsheet is like this: 您的电子表格中日志的日期格式似乎是这样的:

Wed Jul 10 19:00:00 GMT-07:00 2013

That format is not automatically recognized as a date by Google Spreadsheets. Google Spreadsheets不会自动将该格式识别为日期。 However, it is OK for creating a javascript Date object, so we can do that for comparisons, like so: 但是,可以创建javascript Date对象,因此可以进行比较,例如:

new Date("Wed Jul 10 19:00:00 GMT-07:00 2013")

You've started by getting the spreadsheet data in an array, and that's good - it's quick, and we can write out the final data in one step. 您首先将电子表格数据存储在一个数组中,这很好-很快,我们可以一步一步写出最终数据。 To remove rows from the array, use the Array.splice() method. 要从数组中删除行,请使用Array.splice()方法。 Here's the resulting script: 这是生成的脚本:

function killOldLogs() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var data = sheet.getDataRange().getValues(); 

  var today = new Date();

  // Loop over whole range, removing rows that are too old.
  // Leaves row 0, assuming it's a header row.
  for(n=data.length-1;n>0;--n){
    if (new Date(data[n][0]) < today) {
      data.splice(n,1);
    }
  }
  sheet.clearContents(); // Get rid of old contents, then write new
  sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}

暂无
暂无

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

相关问题 Go 到带有 Google Apps 脚本的工作表中包含今天日期的单元格 - Go to the cell that contains today's date in a Sheet with Google Apps Script 使用日期选择器输入Google Apps脚本的电子表格日期 - Google Apps Script for Spreadsheet date input using date picker AWQL /应用脚本,日期日期名称等于今天的名称 - AWQL / Apps Script WHERE Date Day Name Equals Today's Day Name 使用谷歌应用脚本在电子表格中滚动到今天的日期 - Scroll through the spreadsheet to today's date using google app scripts 如何在 Google Apps 电子表格脚本中将日期和时间合并为日期时间? - How to merge date and time as a datetime in Google Apps Spreadsheet script? 使用带有完整日期名称的谷歌应用脚本自动滚动电子表格以显示“今天的日期” - Auto Scroll through the spreadsheet to display "today's date" using google app scripts with the full date name GSheet 的 Google 应用程序脚本 - 在 B 列上查找今天的日期并在同一行的 C 列上写下当前时间 - Google apps script for GSheet - Find today's date on column B and write current time on column C of the same row Google脚本:带有今天日期的行中的getValue - Google Script: getValue from row with today's date 如何在包含今天日期的单行中打开 Google Sheets 电子表格 - How to open Google Sheets spreadsheet to cell in a single row that contains today's date 如何计算JavaScript中的日期是今天之前的三个月? - How do I calculate the date in JavaScript three months prior to today?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM