Home > front end >  Moving Row in Google Sheets if Date is Past 60 Days from Today
Moving Row in Google Sheets if Date is Past 60 Days from Today

Time:11-09

I am trying to automate moving a row of data from a sheet called Pending to a sheet called Closed if the bid due date (located in column F) is older than 60 days. I am a beginner and use a lot of outside sources to help me build what I want, but I don't fully understand what I am writing, as long as it works.

This is what I have tried to write and I am looking for some fine tuning

function approveRequests() {

   // Initialising
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var scheduleSheet = ss.getSheetByName("Pending");
   var pastSheet = ss.getSheetByName("Closed");
   var lastColumn = scheduleSheet.getLastColumn();

   for(var i = scheduleSheet.getLastRow(); i > 60; i--){

    var dateCell = scheduleSheet.getRange(i, 1).getValue();
    if(isValidDate(dateCell)){
      var today = new Date();
      var test = new Date(dateCell);

    
      if(test < today){

        var rangeToMove = scheduleSheet.getRange(i, 1, 1, scheduleSheet.getLastColumn()).getValues();
        pastSheet.getRange(pastSheet.getLastRow()   1, 1, 1, scheduleSheet.getLastColumn()).setValues(rangeToMove);
        scheduleSheet.deleteRow(i);

      }
    }
  }
}


function isValidDate(value) {
  var dateWrapper = new Date(value);
  return !isNaN(dateWrapper.getDate());
}

CodePudding user response:

Move Row is column 1 is older than 60 days:

function approveRequests() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName("Pending");
  const vs1 = sh1.getRange(61,1,sh1.getLastRow() - 60, sh1.getLastColumn()).getValues();
  const sh2 = ss.getSheetByName("Closed");
  const dt = new Date();
  const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() - 60);
  let d = 0;
  vs1.forEach((r,i) => {
    if (new Date(r[0]).valueOf() < dtv) {
      sh2.getRange(sh2.getLastRow()   1, 1, 1, r.length).setValues([r]);
      sh1.deleteRow(i   61 - d  );
    }
  });
}
  • Related