Simple Edit check to email out person not working - if-statement

When someone puts a Y in a cell on a sheet it emails someone.
I am getting it no matter what I enter for the check: Y, F, T, bob - it ALWAYS emails out regardless of the value. It's like the IF is just setting whatever I am asking it to check to the value and doing it. Never seen this in the years of programming..
Can anyone see see something I am missing? I am using an edit trigger
function Checkchange() {
var POs=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(11, 2).getValue();
var rec=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(31, 2).getValue();
var backorder=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(37,5).getValue();
var buyer=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(38,2).getValue();
var QA=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(41,5).getValue();
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(58, 3).setValue(POs)
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(60, 3).setValue(rec)
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(61, 3).setValue(backorder)
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(62, 3).setValue(buyer)
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(63, 3).setValue(QA)
sendEmailsRec();
}
function sendEmailsRec() {
var EMAIL_SENT = 'EMAIL_SENT';
var QAemailAddress = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(60, 1).getValue();
var POs = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(11, 2).getValue();
var message = "Ready for receiving for PO " + POs;
var QAcheckbox = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(31,2).getValue()
var subject = "Ready for receiving for PO" + POs;
var check = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(60, 4).getValue() + QAcheckbox
// if(QAcheckbox ="Y",MailApp.sendEmail(QAemailAddress, subject, message));
if(QAcheckbox ==="hdjkheehehie",
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(60, 3).setValue(EMAIL_SENT)+
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(65, 3).setValue(QAemailAddress + subject + message)+
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(60, 4).setValue(check));
}

Your condition has a wrong logical operator. One = will always return true. Use == or === instead:
if (QAcheckbox == "Y"){
MailApp.sendEmail(QAemailAddress, subject, message));// Always triggers regradless of value
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(63, 3).setValue(EMAIL_SENT));
}
You can read more about operators here.

Related

Google App Script IF function checking only one row, and applying the result to all rows

I seem to be going quite wrong somewhere.
I'm writing a script that will automatically send out a reminder email if a Google sheet cell turns to "Yes".
The problem is my script seems to read it as:
if the second row has a "yes" it will return true for all rows and send out an email to everyone, regardless of the other rows saying "yes" or "no".
if any other row has a yes, then it seems to be completely ignored.
Defining the range to check:
//looping through all of the rows
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// Creating where the if statement is check
var ss = SpreadsheetApp.getActiveSheet();
var thisQuarter = ss.getRange("H2:H50").getValue();
The IF statement to check against:
// checking for this quarter
if (
thisQuarter == "Yes") {
var subject =
'Your BCP is due to expire this quarter: ';
MailApp.sendEmail(emailAddress, subject, message,);
Logger.log('this quarter');
}
}
}
If anyone could give me a couple pointers as to where I'm going wrong, that would be greatly appreciated.
Thank you,
Ideally post a view only copy of the sheet. I believe the problem is this section of code:
// checking for this quarter
if (
thisQuarter == "Yes") {
var subject =
'Your BCP is due to expire this quarter: ';
MailApp.sendEmail(emailAddress, subject, message,);
Logger.log('this quarter');
}
thisQuarter is assigned here:
var thisQuarter = ss.getRange("H2:H50").getValue();
change that line to this:
var thisQuarter = ss.getRange("H2:H50").getValues();
so thisQuarter is an array of values from the range specified
change the if statement to this and see if it helps:
for (i = 0; i < thisQuarter.length; i++) {
if (thisQuarter[i][0] == "Yes" {
// send email
}
}

Gmail App search criteria

I have the following search criteria working very well in Gmail:
user#domain from:/mail delivery/ || /postmaster/ ||/Undeliverable/
I am trying to write Goole Apps code to return the same results. Here is the code:
var thread=GmailApp.search("user#domain from:/mail delivery/ || /postmaster/ ||/Undeliverable/ ");
I am getting different results. I am new to both Regex and Google Apps.
Try Amit Agarwal's tutorial on Gmail Search with Google Apps Script which includes Using Regular Expressions to Find Anything in your Gmail Mailbox:
function Search() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
// Clear existing search results
sheet.getRange(2, 1, sheet.getMaxRows() - 1, 4).clearContent();
// Which Gmail Label should be searched?
var label = sheet.getRange("F3").getValue();
// Get the Regular Expression Search Pattern
var pattern = sheet.getRange("F4").getValue();
// Retrieve all threads of the specified label
var threads = GmailApp.search("in:" + label);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var msg = messages[m].getBody();
// Does the message content match the search pattern?
if (msg.search(pattern) !== -1) {
// Format and print the date of the matching message
sheet.getRange(row,1).setValue(
Utilities.formatDate(messages[m].getDate(),"GMT","yyyy-MM-dd"));
// Print the sender's name and email address
sheet.getRange(row,2).setValue(messages[m].getFrom());
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getSubject());
// Print the unique URL of the Gmail message
var id = "https://mail.google.com/mail/u/0/#all/"
+ messages[m].getId();
sheet.getRange(row,4).setFormula(
'=hyperlink("' + id + '", "View")');
// Move to the next row
row++;
}
}
}
}

google-apps-script multiple criteria writing over headers

I have taken a bit of script from Serge which is great (original link here. I have added in a second criteria to exclude certain rows and it works great except, if there is not header in the sheet being copied to, it will not work (error: "The coordinates or dimensions of the range are invalid.") and if I enter a header or some other data, it overwrites it. Can anyone assist please? I have also found that is there is no match to the criteria I get following message "TypeError: Cannot read property "length" from undefined."
Also, what change would I need to make to change the cell 'dataSheetLog[i][12]' to the status variable, i.e. "COPIED" after I have copied it across. I have tried writing a setValue line but it is obviously the wrong instruction for that syntax.
Code is:
{
var Spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetLog = Spreadsheet.getSheetByName("LOG");
var sheetMaint = Spreadsheet.getSheetByName("MAINTENANCE");
var Alast = sheetLog.getLastRow();
var criteria = "08 - Maintenance"
var status = "COPIED"
var dataSheetLog = sheetLog.getRange(2,1,Alast,sheetLog.getLastColumn()).getValues();
var outData = [];
for (var i in dataSheetLog) {
if (dataSheetLog[i][2]==criteria && dataSheetLog[i][12]!=status){
outData.push(dataSheetLog[i]);
}
}
sheetMaint.getRange(sheetMaint.getLastRow(),1,outData.length,outData[0].length).setValues(outData);
}
In:
sheetMaint.getRange(sheetMaint.getLastRow(),1,outData.length,outData[0].length).setValues(outData);
getLastRow() refers to the last occupied row and should be ,getLastRow() + 1,to keep from overwriting your headers and other problems.
Edited:
{
var Spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetLog = Spreadsheet.getSheetByName("LOG");
var sheetMaint = Spreadsheet.getSheetByName("MAINTENANCE");
var Alast = sheetLog.getLastRow(); // Log
var criteria = "08 - Maintenance"
var status = "COPIED"
var dataSheetLog = sheetLog.getRange(2,1,Alast,sheetLog.getLastColumn()).getValues(); //Log
var dataSheetLogStatusRange = sheetLog.getRange(2,13,Alast,1); //Log
var dataSheetLogStatus = dataSheetLogStatusRange.getValues(); //Log
var outData = [];
for (var i =0; i < dataSheetLog.length; i++) {
if (dataSheetLog[i][2]==criteria && dataSheetLog[i][12]!=status){
outData.push(dataSheetLog[i]);
dataSheetLogStatus[i][0] = "COPIED";
}
}
if(outData.length > 0) {
sheetMaint.getRange(sheetMaint.getLastRow() + 1,1,outData.length,outData[0].length).setValues(outData);
dataSheetLogStatusRange.setValues(dataSheetLogStatus);
}
}
}
what change would I need to make to change the cell
'dataSheetLog[i][12]' to the status variable, i.e. "COPIED" after I
have copied it across.
You were trying to update the value in the array that was extracted from the sheet and not the sheet itself. As arrays are zero based and spreadsheets are not, to translate, +1 must be added to array row and column indices. I am assuming status is in column M of your sheet.

if does not work in changing color cell

After testing code below, all steps works except the if statement. Values of cells which are indeed "ik kom niet, mijn partner komt wel" >> but the color doesnot change into red.
Who is able to solve this problem?
function Changecolor (e)
{
var target = SpreadsheetApp.openById("1234");
var target_sheet = target.getSheetByName("lijst beschikbare bridgers");
var rows = target_sheet.getDataRange();
var numRows = rows.getNumRows();
var range = target_sheet.getRange("A1:K100");
range.setBackground('');
for (var i=2; i <= numRows; i++)
{
var value = target_sheet.getRange(i, 4).getValue();
if (value == "ik kom niet, mijn partner komt wel")
{
target_sheet.getRange(i,1).setBackground('red');
}
}
}
Mikeng ths for editing. I will change all my scripts like this.
Problem is solved. Actually there was no problem the script is OK and works. I just gave the wrong range:
var value = target_sheet.getRange(i, 4).getValue();
should be:
var value = target_sheet.getRange(i, 10).getValue();
Such a stupidy costs me at least 3 hours...

how to start intro.js tour only on first visit to website

I have implemented intro.js to my website. But I wanted to start the tour only on first visit. may be by using cookies.. website is made with html no php..
JavaScript cookies are a solution although I should point out that it will only work for as long as the user keeps the cookie.
//set the cookie when they first hit the site
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
//check for the cookie when user first arrives, if cookie doesn't exist call the intro.
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
code is from http://www.w3schools.com/js/js_cookies.asp
obviously there's some blanks you'll have to fill in there, but it's a good starting point for working with cookies in javascript.
EDIT:
So you want to make a new function, put it in the head, inside script tags (if you have them already, just copy the function into there (you'll want to put the other two functions I provided within the script tag also)). This function will check to see if you have a cookie. If you do, just return. If you don't, create the cookie and run the intro,
<head>
<script type="text/javascript">
function checkCookieIntro(){
var cookie=getCookie("mySite");
if (cookie==null || cookie=="") {
setCookie("mySite", "1",90);
runIntro(); //change this to whatever function you need to call to run the intro
}
}
</script>
</head>
now change your body to be:
<body onload="checkCookieIntro()">
so when the body loads it will check to see if a cookie exists, if it doesn't, create a cookie with a value of one that will last for 90 days (unless the user deletes it) and then run the intro. If the cookie does exist with a value then it does nothing.