简体   繁体   English

根据日期删除单元格,但保留格式

[英]Delete cells based on date but leave formatting

I got this script from "Delete Cells Based On Date" question that was asked. 我从询问的“基于日期删除单元格”问题中获得了此脚本。 It deletes the actual row which removes all the formatting and variables. 它删除实际行,从而删除所有格式和变量。 Is there a way to have it just remove the values? 有没有办法让它只是删除值?

Delete Cells Based on Date 根据日期删除单元格

This is the code: 这是代码:

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Field1");
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() - 1);

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 < oneweekago)  
{
  sheet.deleteRow(i);
}
}
}

What you're looking for is .clearContent() rather than deleteRow. 您正在寻找的是.clearContent()而不是deleteRow。 This will only clear the contents of the cell, but leave all formatting in tact. 这只会清除单元格的内容,而所有格式保持不变。 Here's the Class Range documentation , it's really useful when looking at what you can do with a specific range in a sheet. 这是“ 类范围”文档 ,在查看您可以对工作表中的特定范围进行处理时,它非常有用。

Now to get this to work the way you want, you'll have to use 2 for statements, one to get the row number (i), and the other to get the column number (j), which you can then use in the .getRange to run the .clearContent() on. 现在,要使它按您希望的方式工作,您将必须对语句使用2,一个用于获取行号(i),另一个用于获取列号(j),然后可以在列中使用它。 .getRange来运行.clearContent()。

function clearOldRecords() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Field1");
  var dataRange = sheet.getDataRange();
  var lastRow = dataRange.getLastRow();
  var data = dataRange.getValues();

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

  for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].length; j++) {
    var tempDate= data[i][0]; //column number is indexed from 0, change accordingly to your data

      //if start date column is older than 7 days, clear the content of the row
      if (tempDate!= "" && tempDate < oneWeekAgo) {
        sheet.getRange(i+1,j+1).clearContent();
      }
    }
  }
}

So, to explain this further, these are the notable changes I've made to your script. 因此,为了进一步解释这一点,这些是我对脚本进行的显着更改。

Date subtractions are worked out in days, therefore doing a -1 will only take 1 day off of the date. 日期减法以天为单位计算,因此执行-1只会比日期少1天。 You need a week so I have changed this to -7: 您需要一个星期,所以我将其更改为-7:

oneWeekAgo.setDate(currentDate.getDate() - 7);

The script also no longer scans the last row of the sheet, this could impact performance if the sheet is huge, but on your normal day-to-day sheet this shouldn't be an issue. 该脚本也不再扫描工作表的最后一行,如果工作表很大,这可能会影响性能,但是在正常的日常工作表上,这不应该成为问题。

As you can see below, for loop for i gets all of the row numbers, and for loop j can be used for the column numbers, as you can see in the getRange() when trying to clear the contents: 如您在下面看到的,for循环for i获取所有行号,并且for循环j可用于列号,正如您在尝试清除内容时在getRange()中看到的那样:

sheet.getRange(i+1,j+1).clearContent();

Note: this may leave pretty huge gaps in your data if it's not sorted by date, you could add something like this to sort it afterwards (put this outside of the FOR loops): 注意:如果未按日期排序,这可能会在数据中留下很大的空白,您可以添加类似以下内容以对其进行排序(将其放在FOR循环之外):

sheet.getRange('A1:Z').sort({column: 2, ascending: false}) //change column number accordingly, this is NOT indexed from 0

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

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