user2326333
user2326333

Reputation: 83

Using Google Script how to find offset to insert text

I am not sure how to find the offset value to use insertText method

My google doc should look some thing like

paragraph 1

paragraph 2

Tracking bug: 567587

paragraph 3

All the paragraph data is fetched from excel and write in to doc

Using append paragraph I am not able to use url only for the '567587'. Below script is so far I created

var doc=DocumentApp.openById('abcxxdcdcd').getBody();
var text=doc.editAsText();
doc.appendParagraph('paragraph1');
doc.appendParagraph('paragraph2');
doc.appendParagraph('Tracking bug: ');
//I want to insert text next to the above paragraph 'Tracking bug: ' which means I need to find the offset of the location.
text.insertText(offset,567587).setLinkUrl(startOffset, endOffsetInclusive, url) 

Note: I am not using container bound with this document.

Upvotes: 0

Views: 911

Answers (1)

user6655984
user6655984

Reputation:

You want to append text to a paragraph and make that text a hyperlink. Here is how.

doc.appendParagraph('Tracking bug: ').appendText("567587").setLinkUrl("http://example.com");

The methods work as a chain: appendParagraph returns a newly created paragraph; appendText returns the newly appended text element; setLinkUrl links that text element. No need for offsets.

Upvotes: 2

Related Questions