Google Sheets - Script Dates

Finalfight40

Active Member
Joined
Apr 24, 2018
Messages
273
Office Version
  1. 365
Platform
  1. Windows
Hi All


I am having a problem with making a script. I am making a script that will delete a row a week after something has been approved.

As of right now i have this which when a dropdown is selected as "YES", another cell gets the current date entered:

Code:
function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  if( activeSheet.getName() == "TASKS" ) {
    var activeRange = ss.getActiveRange();
    var activeRow = activeRange.getRow();
    var activeColumn = activeRange.getColumn()
    
    if( activeColumn == 13 && activeRow >= 2 ) { 
      if ( activeRange.getValue() == "YES" ) {
        activeSheet.getRange(activeRow, activeColumn + 1).setValue(new Date());
      }
      else if( activeRange.getValue() != "YES" ) {
      activeSheet.getRange(activeRow, activeColumn + 1).setValue("Not Approved");
      }
    }
  }
};


This would then be followed by this which i would run once a day or so:

Code:
function isValidDate(d) {
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
    return false;
  return !isNaN(d.getTime());
}


function DeleteEditorialRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var taskSheet = ss.getSheetByName("TASKS");
  var lastRow = taskSheet.getLastRow();
  var formattedDate = Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy");
  
  for (var i = lastRow; i >= 2; i--){
    if (taskSheet.getRange(i, 13).getDisplayValue() == "YES"){
      if (taskSheet.getRange(i, 12).getDisplayValue() == "YES"){
        if([COLOR=#ff0000](new Date() - taskSheet.getRange(i, 14).getValue()) >= 7[/COLOR] && isValidDate(taskSheet.getRange(i, 14).getValue())){
          taskSheet.deleteRows(i);
        }
      }
    }
  }
};

The problem im having is with the section i have highlighted in red.

When i test the following:

Code:
Logger.log(new Date() - taskSheet.getRange(i, 14).getValue());

The answers i am getting are like 152786.0 when i feel like i should be expecting numbers less than 1 since not even a day has passed.

Any help is appreciated.
 
Last edited:

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Watch MrExcel Video

Forum statistics

Threads
1,126,981
Messages
5,621,952
Members
415,869
Latest member
LWSkinner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top