Home > Net >  set value of row for previous month
set value of row for previous month

Time:08-18

Setting value of each row that has a date of last month, I am stumped about how to get the result I want.

function hideRows() {
 const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Archived Videos");
  const vs = sh.getDataRange().getValues();
  const dtv = new Date(new Date().getFullYear(),new Date().getMonth() - 1,new Date().getDate()).valueOf();
  let rows = [];
  vs.forEach((r,i) => {
    if(new Date(r[0]).valueOf() < dtv) {
      rows.push(i 1);
      var val = sh.getRange(i   1,1,1,20   1).setValue(null);
    }
  })
}   

I need to set each row that is from previous month to null. My code right now sets each row that is exactly one month ago or older to null. The final result should be that If today is any date in Sept all of August will set to null.

CodePudding user response:

Setting the date in the rows of the previous month to null

function hideRows() {
 const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Archived Videos");
  const vs = sh.getDataRange().getValues();
  const dtv0 = new Date(new Date().getFullYear(),new Date().getMonth() - 1,1).valueOf();
  const dtv1 = new Date(new Date().getFullYear(),new Date().getMonth(),1,0).valueOf();
  let rows = [];
  vs.forEach((r,i) => {
    let d = new Date(r[0]);
    let dv = d.valueOf();
    if(dv >= dtv0 && dv < dtv1) {
      rows.push(i 1);
      sh.getRange(i   1,1).setValue(null);
    }
  })
}   
  • Related