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;
}
Related
I have a Google Sheet with a table like this:
Data
On Duty
Support
15/02/2023
Name1
Name4
16/02/2023
Name2
Name5
17/02/2023
Name3
Name6
I need to check the column A if the date is equal with today's date and get the value of column B and C and send the values to a slack channel.
I tried this but isn't working:
function sendSlackMessage() {
const onduty = QUERY("SELECT B WHERE todate(A)=date'" & text(today(), "dd/MM/yyyy") &"'");
const support = QUERY("SELECT C WHERE todate(A)=date'" & text(today(), "dd/MM/yyyy") &"'");
const url = "https://hooks.slack.com.services/xxxxxx/xxxxxxx/xxxxxxxxxxxxxxxxxxxxxx";
const params = {
method: "post",
contentType: "application/json",
payload: JSON.stringfy({
"text" : "Analyst on duty today: " + onduty + "\n" + "Support analyst: " + support
})
}
const sendMsg = UrlFetchApp.fetch(url, params);
var respCode = sendMsg.getResponseCode();
Logger.log(sendMsg);
Logger.log(respCode);
}
Here's how you can get the values:
const dt = new Date();
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2,1,sh.getLastRow() - 1, 2).getValues();
let vo = vs.map(r => {
let d = new Date(r[0]);
let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
if(dtv == dv) {
return r[1];
} else {return null;}
}).filter(e => e);
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);
}
})
}
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 have a data similar to this one
Screenshot of data
First, I filtered out the range where column N is marked as yes and I success to get that. Then I want to filter out all unique value in column A together with value in column M and return the row number of the unique value. Please help me with that one.
So the result will be a range/array like this:
[[HCMC 1, handoi3648#gmail.com, 1 (/// row 1 of the filtered range)],
[HCMC 4, handoi3648#gmail.com, 3 (/// row 3 of the filtered range)],
[HCMC 5, handoi3648#gmail.com, 4 (/// row 4 of the filtered range)]]
My code upto the first filtered range as follows
function HouseLeaseReminderAtYE(){
var SS = SpreadsheetApp.getActiveSpreadsheet();
var Sheet = SS.getSheetByName("Tax_Master");
var Range = Sheet.getDataRange();
var Values = Range.getDisplayValues();
var hl_to_remind_at_final_range = Values.filter(function(item){return item[13]==="Y"});}
function HouseLeaseReminderAtYE() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Tax_Master");
const rg = sh.getDataRange();
const vs = rg.getDisplayValues();
let fvs = vs.filter(function (item) { return item[13] == "Y" });
let uO = {pA:[]};
fvs.forEach((r,i) => {
if(!uO.hasOwnProperty(r[0])) {
uO[r[0]] = [r[0],r[12],i+1];
uO.pA.push(r[0]);
}
});
let uA = uO.pA.map( p => [uO[p]]);
Logger.log(uA);
}
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);
}