Reputation: 13
Is there a Google Scripts function that can search a designated Google Document for a specific string of words? Or any way to do that? I am trying to make a conditional statement that will find a word in a Google Document, and if that word exists, it will carry out a task. Thank you.
An example:
if (for example, the word gym exists in the Google Document){
Send an email to the gym teacher.
}
Upvotes: 0
Views: 420
Reputation: 5839
You can use the findText
. For example:
var body = DocumentApp.getActiveDocument().getBody();
if(body.findText("gym"))
{
// send an email
}
Upvotes: 0
Reputation: 1713
Something like this should do what you need. I commented out some of the Logger stuff but left it there in case you wanted to see how things worked more clearly.
function findText() {
var doc = DocumentApp.getActiveDocument();//get doc
var body = doc.getBody().getText();//get text as string
//Logger.log(body);
var regex = /gym/g;//look for 'gym'
var searchSuccess = body.search(regex);
Logger.log(searchSuccess);
if(searchSuccess > 0){
Logger.log('Item Found')
} else {
Logger.log('Item not found')
}
}
Upvotes: 1