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.
Edited: I took Coopers answer but now need it to be 2 months back and leave all of Aug and Sept. See comment below for clarification.
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() - 2, Date().getDate()).valueOf();
const dtv1 = new Date(new Date().getFullYear(),new Date().getMonth(), Date().getDate()).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
I would like to filter my table based on a date column that is formatted to show only the year.
Manually I just go to the filter --> filter by value and check only the year I am interested in.
How can I do this in Apps Script?
Select by Date
function selectbydate(dt) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const sr = 2;//data startrow
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf()
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues().filter(r => {
let d = new Date(r[26]);
let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
return dv == dtv;
});
return vs;
}
Script for current date + 5 sends an email with exact 5 days from now. I want this to only output the date leaving out the timestamp
current output is: Sun Jul 10 2022 09:29:16 GMT-0400 (Eastern
Daylight Time)
expected output is: 07/0/2022
Here is what I have tried.
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
var date5 = curDate.setDate(curDate.getDate() + 5);
var dateFormat = moment(date5).format('DD/MM/YY');
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ dateFormat + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}
I am stumped as to why this doesn't work.
Try it this way:
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
curDate.setDate(curDate.getDate() + 5);
let d = Utilities.formatDate(curDate,Session.getScriptTimeZone(),"MM/dd/yyyy")
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ d + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}
I found this code on here which should work perfectly for me. Was just hoping someone could change the code to delete entries that have dates that are 2 weeks old or older. So if the script were to run today, it would delete any rows that are October 26th or older.
function DeleteOldEntries() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("MASTER");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();//today
for (i=lastrow;i>=3;i--) {
var tempDate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function
Deleting Rows in a forEach loop
function DeleteOldEntries() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("MASTER");
const sr = 3;//guessing data start on row 3
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues();
let d = 0;//delete counter
const dtv = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 15).valueOf();
vs.forEach((r, i) => {
let cdt = new Date(r[2]);//assume date is in column 3
let cdtv = new Date(cdt.getFullYear(), cdt.getMonth(), cdt.getDate()).valueOf();
if (cdtv < dtv) {
sh.deleRow(i + sr - d++);
}
});
}
Date.valueOf()
I believe your goal is as follows.
From your script and question, you want to delete the rows when the date of column "C" is before 2 weeks from today.
In this case, how about the following modification? In your script, when the value of column "C" is the date object, you are comparing the date object.
From:
var currentDate = new Date();//today
for (i=lastrow;i>=3;i--) {
var tempDate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function
To:
var currentDate = new Date();
currentDate.setDate(currentDate.getDate() - 14); // Added: This means before 2 weeks from today.
var d = currentDate.getTime(); // Added
for (i = lastrow; i >= 3; i--) {
var tempDate = values[i - 1][2];
if ((tempDate != NaN) && (tempDate.getTime() <= d)) { // Modified
sheet.deleteRow(i);
}
}
}
References:
getDate()
setDate()
Compare two dates with JavaScript
I'm trying to get a date from a cell, add 5 days to it, and check if that date is today. Here's what I've tried:
function test() {
sheet = SpreadsheetApp.getActive();
var today = new Date(new Date().setHours(0,0,0,0));
var weekStartDate = sheet.getRange('B5').getValue().getTime();
var newDate1 = new Date(new Date().setHours(0,0,0,0));
var newDate5 = new Date(new Date().setHours(0,0,0,0));
newDate1.setTime(weekStartDate + (1*24*60*60*1000));
newDate5.setTime(weekStartDate + (5*24*60*60*1000));
if(newDate5==today){
var answer = 'Yes';
} else{
var answer = 'No';
}
Logger.log(today);
Logger.log(newDate1);
Logger.log(newDate5);
Logger.log(answer);
}
The log shows:
11:29:59 AM Info Wed Jul 21 00:00:00 GMT-04:00 2021
11:29:59 AM Info Sat Jul 17 00:00:00 GMT-04:00 2021
11:29:59 AM Info Wed Jul 21 00:00:00 GMT-04:00 2021
11:29:59 AM Info No
How do I get "answer" to return "Yes"?
Screenshot
To compare two dates, we use values of these dates. This means if you changed newDate5 == today with newDate5.valueOf() == today.valueOf() it will return Yes.
Here is a simplified version of your code:
function test() {
var sheet = SpreadsheetApp.getActive();
var today = new Date().setHours(0,0,0,0).valueOf();
var dateToCompare= new Date(sheet.getRange('B5').getValue()).valueOf();
var dayInMs = 24*60*60*1000
Logger.log(today);
Logger.log(dateToCompare);
Logger.log(today == dateToCompare + 5*dayInMs);
}
Just in case
function myFunction() {
// date from cell 'A1'
var date = SpreadsheetApp.getActiveSheet().getRange('A1').getValue();
// today
var today = new Date();
// function to add 5 days to a date
const five_days_later = (d) => new Date(d.setDate(d.getDate() + 5));
// function to compare two dates
const if_same_date = (d1, d2) => d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() && d1.getYear() == d2.getYear();
// test
Logger.log(if_same_date(five_days_later(date), today));
}
var now = new Date();
var then = new Date("2021-07-16");
const to_str = x => '' + x.getYear() + x.getMonth() + x.getDate();
const if_same_date = (d1, d2) => to_str(d1) == to_str(d2);
const plus_days = (date, gap) => new Date(date.valueOf() + gap*24*60*60*1000);
console.log(if_same_date(now, plus_days(then, 5)));
I was able to get the script to log a single cell. However, I am looking now to log 2 columns with 49 rows and then add the date timestamp to the third column. What would I need to change within the script? I have been changing the numerical values and no matter what, its still looking for 1 cell. Is it a greater change than I thought?
function DHLtracking() {
const ss = SpreadsheetApp.openById('Sample_ID_1');
const sh = ss.getSheetByName('DHL Shipping Data');
const data = [[sh.getRange('C3:D51').getValue(),new Date()]];
const tss = SpreadsheetApp.openById('Sample_ID_1');
const ts = tss.getSheetByName('Archived Data');
ts.getRange(getColumnHeight() + 1, 8, data.length, data[0].length).setValues(data);
}
function getColumnHeight(col, sh, ss) {
var ss = ss || SpreadsheetApp.openById('Sample_ID_1');
var sh = sh || ss.getSheetByName('Archived Data');
var col = col || 8;
const rcA = sh.getRange(1, col, sh.getLastRow(), 1).getValues().flat().reverse()
let s = 0;
for (let i = 0; i < rcA.length; i++) {
if (rcA[i].toString().length == 0) {
s++;
} else {
break;
}
}
return rcA.length - s;
}
Try this:
function DHLtracking() {
const ss = SpreadsheetApp.openById('Sample_ID_1');
const sh = ss.getSheetByName('DHL Shipping Data');
const sr = 3;
const sc = 3;
const dt = new Date();
const data = sh.getRange(sr, sc, 49, 3).getValues().map(r => r[2] = dt)
const tss = SpreadsheetApp.openById('Sample_ID_1');
const tsh = tss.getSheetByName('Archived Data');
tsh.getRange(tsh.getLastRow() + 1, 8, data.length, data[0].length).setValues(data);
}