简体   繁体   中英

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. 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.

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. You need a week so I have changed this to -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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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