Chris
Chris

Reputation: 129

Issue with body.replaceText() in Google Docs

I am populating a Google Doc template based on a Google Form submission. Upon submit, the program copies the template Google Doc, captures the first item from the Google Form which is always the person's name (because this is a required field), and then replaces {{Name}} in the new file with the entered name using:

var name = itemResponses[0].getResponse();
body.replaceText('{{Name}}', name);

That works correctly. But then I iterate through the rest of the item response and not all the items are required, so I use a lookup table in a Google sheet. The loop takes the item id in the item response and then looks up the text that the response will replace. Then the program does:

var textToReplace //this value is from column B in the Google Sheet lookup table
var newText //this value is the entered response from the Google Form
body.replaceText(textToReplace, newText);

When I do this, I am getting a "Exception: Invalid argument: searchPattern" error. Why are these two body.replaceText() functions different? They are both finding a variable with in double brackets in the Google doc, but it only works in one case.

And to be clear, this was previously working correctly for the last couple of months and only recently started to not work (maybe Google changed something??). My hypothesis is that it has to do with a regex pattern in the first parameter of replaceText.

Upvotes: 0

Views: 1746

Answers (1)

Cameron Roberts
Cameron Roberts

Reputation: 7367

The "searchPattern" error is a good clue, in certain circumstances, it tells us the value of "textTopReplace" is not a valid search pattern. Since the code hasn't changed, the lookup table in your spreadsheet, or the fields on the form probably have.

One of your lookups is returning a value that isn't a valid search pattern. Perhaps it is returning Null, or an empty string?

You can get more information by using console.log to log debug info to the stackdriver log interface provided by Google, like so:

console.log('text to replace: "'+textToReplace+'"'); //this value is from column B in the Google Sheet lookup table
console.log('value: "'+newText+'"'); //this value is the entered response from the Google Form
body.replaceText(textToReplace, newText);

Then, to view the logs, select "stackdriver logging" from the View menu.

Upvotes: 4

Related Questions