Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

JavaScript 'indexOf'; question - I need multiple values not just a single value

The following code works but it isn't really what I need
//Begin Code//

var ss = SpreadsheetApp.getActiveSpreadsheet();
var email_test = ss.getSheetByName('Recipient').getRange('B2').getValue();
var messagechecked = e.values[3];
var x=messagechecked.toString();    
//var stringLoc = x.indexof("Chris", [fromIndex] )
    var stringLoc=x.toLowerCase().indexOf("search engines");
    if ( stringLoc == -1) {
MailApp.sendEmail(email_test, "Successful submission", body); 
}
else {
MailApp.sendEmail(email_test, "Check this submission", body); 
}

Open in new window


//end code//

What I want to do is add multiple sets of words like an array (see the example below)  
   
var stringLoc=x.toLowerCase().indexOf("search engines", "bad words2","bad words3", "bad words3");

Open in new window


So that if the person filling out my form puts in "bad words3" for example the code would jump to the code listed below  
MailApp.sendEmail(email_test, "Check this submission", body); 

Open in new window


If another put in  "bad words2" for example the code would jump to the code listed below  
MailApp.sendEmail(email_test, "Check this submission", body); 

Open in new window


If no bad words are identified the code would jump to  

MailApp.sendEmail(email_test, "Successful submission", body);

Open in new window



Regards,

Chris
0
ivh05
Asked:
ivh05
1 Solution
 
BadotzCommented:
Just string all of the "bad words" together with a separator:

var stringLoc=x.toLowerCase().indexOf("search engines|bad words2|bad words3|", "bad words3|");
0
 
ProculopsisCommented:

//Try match instead:

var testThis = "Hello world";
alert( testThis.match( "goodbye|cruel|world" ) );
0
 
BadotzCommented:
The separator prevents things like "words2bad" from being excluded.

function ok(word) {
    return (word + "|").toLowerCase().indexOf("search engines|bad words2|bad words3|bad words3|");
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BadotzCommented:
Or use JSON:

var bad = {
    "search engines": 1,
    "bad words2": 2,
    "bad words3": 3
}

function ok(word) {
    return bad[word];
}
0
 
ivh05Author Commented:
I have to apologize because I think you all are at a higher level then I am with this javascript stuff.
 
I have been working with your comments for a while now but nothing seems to click...

 Badotz
I like the idea of stringing the bad words together with seperators but can't find the right combination to make it work.

I want to try to clear up what I am after.
I am using Google Apps Script and this Script is attached to a Google Spread Sheet
The data entry for the spreadsheet is populated using a form from a web page
The script is trigged as soon as the form has written data to the spreadsheet
I don't want to print the bad words to the computer screen for the user; I just want to send me an email.

If someone in the form field sends me the following
We can increase rankings of your website in search engines. Please reply back for more details.
  I want the script to send me an email  that has the subject of "Check this submission"
The following line does this
MailApp.sendEmail(email_test, "Check this submission", body); 

Open in new window


So the bad words in that form field were "search engines"

Ok the next salesman fills out the form and says something like
We can can increase you chest size by 6 inches for free. Please reply back for more details.
so I want to be able to add a new set of words like "increase you chest size" to my list of bad words.

so I don't want to look for both "search engines" and "increase you chest size" to get a match

I want to look for either
"search engines" or "increase you chest size"

so back to the separator idea
how should the following be written?
var stringLoc=x.toLowerCase().indexOf("search engines|bad words2|bad words3|", "bad words3|");

Open in new window


Proculopsis
I think you are talking over my head because what you are trying to tell me isn't clicking

Badotz:
You then offered more code and I really got lost on how i might use it
function ok(word) {
    return (word + "|").toLowerCase().indexOf("search engines|bad words2|bad words3|bad words3|");
}

Open in new window


Same thing with the JSON example

var bad = {
    "search engines": 1,
    "bad words2": 2,
    "bad words3": 3
}

function ok(word) {
    return bad[word];
}

Open in new window


Hopefully one of will be able help me see the light

Regards,

Chris
0
 
Kusala WijayasenaSoftware EngineerCommented:
Check this out:

var str = messagechecked.toString();

var blackList = new Array();
blackList[0] = "search engines";
blackList[1] = "bad words1";
blackList[2] = "bad words2";
blackList[3] = "bad words3";

if (str.toLowerCase().match(blackList.join("|").toLowerCase())) {
    MailApp.sendEmail(email_test, "Check this submission", body);
}
else {
    MailApp.sendEmail(email_test, "Successful submission", body);
}

Open in new window


-Kusala

0
 
BadotzCommented:
No how does THAT help?
0
 
ivh05Author Commented:
kusala,
It will be a few hours before I can get back to my computer to try your code out... I am anxious to see if it works.

Regards,

Chris
0
 
ivh05Author Commented:
WOW.... that rocks!
kusala you nailed it.  Your code works perfectly Thank-you!
0
 
ivh05Author Commented:
A big thank-you goes out to everyone who tried to help me solve this puzzle.

For other beginners that happen upon this thread that are wanting to do the same thing; I wanted to include the two missing lines that made this work using the solution given to me by kusala.

var email_test = ss.getSheetByName('Recipient').getRange('B2').getValue();  
var messagechecked = e.values[3];
var str = messagechecked.toString();

var blackList = new Array();
blackList[0] = "search engines";
blackList[1] = "bad words1";
blackList[2] = "bad words2";
blackList[3] = "bad words3";

if (str.toLowerCase().match(blackList.join("|").toLowerCase())) {
    MailApp.sendEmail(email_test, "Check this submission", body);
}
else {
    MailApp.sendEmail(email_test, "Successful submission", body);
} 

Open in new window



Very nice Kusala... thank you so much!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now