Google App Script - Search for special charaters in Text.findText()

215 views
Skip to first unread message

Edward Ulle

unread,
May 17, 2019, 8:45:34 AM5/17/19
to Google Apps Script Community
I'm trying to figure out how to find certain characters in a Google Doc.  For example how to search for a blank space Text.findText(" ") throws an exception.  Text.findText("\s") doesn't find anything.  And now Text.findText("^") gives a weird exception "We're sorry, a server error occurred. Please wait a bit and try again.".  I've also tried Text.findText("\^") but get the same weird exception.  Normally you can just put a text string like Text.findText("find this") and it works.  How to properly put regex expressions in findText()?

Marcos Gomes

unread,
May 17, 2019, 1:29:27 PM5/17/19
to google-apps-sc...@googlegroups.com
Try this

Exemple find ##000 to ##999

 var body = doc.getBody();
  var txt = body.getText();
  var rx = /##([000-999]+)/g;     //use regex
  var find = txt.match(rx);

Em sex, 17 de mai de 2019 às 09:45, Edward Ulle <thew...@gmail.com> escreveu:
I'm trying to figure out how to find certain characters in a Google Doc.  For example how to search for a blank space Text.findText(" ") throws an exception.  Text.findText("\s") doesn't find anything.  And now Text.findText("^") gives a weird exception "We're sorry, a server error occurred. Please wait a bit and try again.".  I've also tried Text.findText("\^") but get the same weird exception.  Normally you can just put a text string like Text.findText("find this") and it works.  How to properly put regex expressions in findText()?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4a7ad6ed-1a9f-484d-b462-feed71539254%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edward Ulle

unread,
May 17, 2019, 2:37:49 PM5/17/19
to Google Apps Script Community
body.getText() returns a string.  I want to be able to search a Document Text object.

Tanaike

unread,
May 23, 2019, 8:35:08 PM5/23/19
to Google Apps Script Community
How about this sample script? In order to search " " and "^", "\\s" and "\\^" are used.

Sample script:
var searchValue = "\\s|\\^";
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var s = body.findText(searchValue);
while (s) {
  var text = s.getElement().asText();
  Logger.log(text.getText())
  var s = body.findText(searchValue, s);
}



Note:
  • If you want to only a space and only ^, please use "\\s", "\\^", respectively.

Message has been deleted

Edward Ulle

unread,
May 29, 2019, 10:40:39 AM5/29/19
to Google Apps Script Community
Tanaike, Text.findText("\\s") works but Text.findText("\\^") doesn't.

Tanaike

unread,
May 29, 2019, 6:54:28 PM5/29/19
to Google Apps Script Community
Thank you for replying. Unfortunately, from "Text.findText("\\^") doesn't.", I couldn't understand about the detail situation. I apologize for my poor English skill. In my environment, I could confirm that "\\s" and "\\^" work. I cannot replicate your situation. I apologize for this situation. So can you provide the detail information about the script, flow and sample Document for replicating your situation? By this, I would like to confirm it.

Edward Ulle

unread,
May 30, 2019, 10:52:42 AM5/30/19
to Google Apps Script Community
Tanaike, found my mistake.  I hadn't properly checked for if the character didn't exist.  Thanks now it works.

Tanaike

unread,
May 30, 2019, 6:13:42 PM5/30/19
to Google Apps Script Community
Thank you for replying. I'm glad your issue was resolved.
Reply all
Reply to author
Forward
0 new messages