1

I am attempting to replace some text in a Google Doc with alternate text and an underlying link. (The link is to a cell in a Google SS.)

I am working off of the answer to Google Apps Script Make text a clickable URL using replaceText()

The script runs but I get the following error on the last line:

"Execution failed: Index (114) must be less than the content length (8). (line 116, file "Code")"

The "114" refers to the length of the URL. The "8" refers to the length of the replacement text which is BTW, "New Song".

It seems that this approach has a fatal flaw in that the length of the link (URL) can not exceed the length of the text being replaced. (I can't imagine that is really the case, but I can't see a way around the problem.) I have searched for alternate approaches to the code I used, but have found none.

Note: Any variables below that are not defined within this script, are defined in the script preceding this section (which I have omitted for brevity). All of these variables are working properly. The error always occurs on the last line of the script below.

var titleUrl = titleLink;
var targetString = "Song Title"
var replacementString = songTitle
var element = body.findText(targetString);
var start = element.getStartOffset();
var text = element.getElement().asText();
text.replaceText(targetString, replacementString  );
text.setLinkUrl(start, start+titleUrl.length, titleUrl);

What I expect is the old text, "Song Title" to be replaced with "New Song" and for "New Song" to be a clickable link to a cell on a Google Sheet.

Update

To close the loop on this; the final code is:

var titleUrl = titleLink;
var targetString = "Song Title"
var replacementString = songTitle
var element = body.findText(targetString);
var text = element.getElement().asText();
text.replaceText(targetString, replacementString  );
text.setLinkUrl(titleUrl);
Bee Tee
  • 129
  • 2
  • 15
  • Can you provide a sample Document and script for replicating your issue? Of course, please remove your personal information. – Tanaike Jun 05 '19 at 01:29

1 Answers1

0

I think you are trying to invoke setLinkUrl() with an invalid parameter start+titleUrl.length. The second parameter in this method refers to text length, not the link length. What you are doing now, is trying to set link to text that is 114 - text.getText().length longer than your actual text "New Song".

  • You are so right. I put so much stock in the referenced "answer" to this problem as posted at [https://stackoverflow.com/questions/21211804/google-apps-script-make-text-a-clickable-url-using-replacetext], I didn't closely read the Google documentation. Just did that, and of course you are correct. There is only one other `setLinkUrl()` method (and in my case), it's `setLinkUrl(titleUrl)`. Worked like a charm. One more problem solved. One more lesson learned *"RTFM"!). Thanks for your help. – Bee Tee Jun 05 '19 at 10:08
  • Yes, I also thought about asking you if the easier `setLinkUrl(titleUrl)` method will work, but assumed you want to retain the ability to make text a link in part. Glad I could help! – Oleg Valter is with Ukraine Jun 05 '19 at 10:14