Check if column A is equal today's date and get the value of column B - function

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);

Related

Partial Match Not Working in Google Sheets

I have this sheet tab that contains these columns:
In another tab, I have this column:
I want the result to be like this:
I have this formula that I'm using that somewhat works:
=IFERROR(TEXTJOIN(" ",TRUE,QUERY(ARRAYFORMULA(IF(REGEXMATCH($B2,Masterlist!$A$2:$A$139),Masterlist!$A$2:$A$139,"")),"where Col1 is not null")))
but the result I'm getting is like this:
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh1 = ss.getSheetByName("Sheet1");
const [h1, ...vs1] = sh1.getDataRange().getDisplayValues();
let obj1 = {};
h1.forEach((h, i) => { obj1[h] = i + 1; });
const code = sh1.getRange(2,obj1["Activity Code"],sh1.getLastRow() - 1).getDisplayValues().flat().map(c => c.replace(/ /g,''));
const sh2 = ss.getSheetByName("Sheet2");
const [h2, ...vs2] = sh2.getDataRange().getDisplayValues();
let obj2 = {};
h2.forEach((h, i) => { obj2[h] = i + 1; });
const acct = sh2.getRange(2,obj2["Account"],sh2.getLastRow() - 1).getDisplayValues().flat();
let arr = [["Activity Code","Account"]]
acct.forEach((a,i) =>
code.forEach(c => {
if(~c.indexOf(a)) {
arr.push([c,a]);
}
}
));
Logger.log(JSON.stringify(arr));
const sh3 = ss.getSheetByName("Sheet3");
sh3.clearContents();
sh3.getRange(1,1,arr.length, arr[0].length).setValues(arr);
}
Sheet1:
A
1
Activity Code
2
_account1_immediate
3
_lunch_account2
4
_break_account3_lunch
5
_break account 3
Sheet2:
A
1
Account
2
account1
3
account2
4
account3
5
account11
Sheet3:
A
B
1
Activity Code
Account
2
_account1_immediate
account1
3
_lunch_account2
account2
4
_break_account3_lunch
account3
5
_breakaccount3
account3

Filter date by values not condition

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;
}

set value of row for previous month

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);
}
})
}

Script calls newDate and adds 5 days. I am looking to format the output to just date

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);
});
}

Compare text from url in Google web app script

I have a container bound script which sends the query result perfectly.
function doGet(e) {
var ss = SpreadsheetApp.getActive();
var fn = e.parameter.FN;
var key = e.parameter.KEY;
if ( fn == 'CSA' ) {
var sh = ss.getSheetByName("SubmittedAnswers");
var data_raw = e.parameter.DATA.toString().replace("\n ", String.fromCharCode(10));
var data = data_raw.split('|||| ');
sh.appendRow(data);
return ContentService.createTextOutput("New record created");
}
else if ( fn == 'QQ' ) {
var sh = ss.getSheetByName("QuestionBank");
var rg = sh.getDataRange().getValues();
var rgq = sh.getName() + "!" + sh.getDataRange().getA1Notation();
var sql = "Select A, B, C, D, E, F WHERE G IS NOT NULL";
var qry = '=query(' + rgq + ';\"' + sql + '\";1)';
var ts = ss.insertSheet();
var setQuery = ts.getRange(1,1).setFormula(qry);
var getResult = ts.getDataRange().getValues();
ss.deleteSheet(ts);
return ContentService.createTextOutput(JSON.stringify(getResult));
}
}
Now i'm trying to add something like a password so that without this none can run the script. I've tried the code below.
else if ( fn == 'QQ' ) {
var sheet_key = ss.getSheetByName("DashBoard").getRange(9,2).getValue;
if (sheet_key == key) {
var sh = ss.getSheetByName("QuestionBank");
var rg = sh.getDataRange().getValues();
var rgq = sh.getName() + "!" + sh.getDataRange().getA1Notation();
var sql = "Select A, B, C, D, E, F WHERE G IS NOT NULL";
var qry = '=query(' + rgq + ';\"' + sql + '\";1)';
var ts = ss.insertSheet();
var setQuery = ts.getRange(1,1).setFormula(qry);
var getResult = ts.getDataRange().getValues();
ss.deleteSheet(ts);
return ContentService.createTextOutput(JSON.stringify(getResult));
}
}
The sheet_key is perfectly in the "DashBoard" sheet at cell (9,2). Still the script is not returning anything. If i remove the IF part the script works fine :(
What should I do?
This var sheet_key = ss.getSheetByName("DashBoard").getRange(9, 2).getValue;
needs to be this: var sheet_key = ss.getSheetByName("DashBoard").getRange(9, 2).getValue();
This is something that is easy to do with the current editor