Link to home
Start Free TrialLog in
Avatar of ivh05
ivh05

asked on

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
Avatar of Badotz
Badotz
Flag of United States of America image

Just string all of the "bad words" together with a separator:

var stringLoc=x.toLowerCase().indexOf("search engines|bad words2|bad words3|", "bad words3|");
Avatar of Proculopsis
Proculopsis


//Try match instead:

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

function ok(word) {
    return (word + "|").toLowerCase().indexOf("search engines|bad words2|bad words3|bad words3|");
}
Or use JSON:

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

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

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Kusala Wijayasena
Kusala Wijayasena
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No how does THAT help?
Avatar of ivh05

ASKER

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
Avatar of ivh05

ASKER

WOW.... that rocks!
kusala you nailed it.  Your code works perfectly Thank-you!
Avatar of ivh05

ASKER

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!