使用 Google 脚本如何找到插入文本的偏移量
Using Google Script how to find offset to insert text
我不确定如何找到偏移值以使用 insertText 方法
我的 google 文档应该看起来像
第 1 段
第 2 段
跟踪错误:567587
第 3 段
所有段落数据都取自excel并写入doc
使用附加段落我无法仅对“567587”使用url。下面的脚本是我到目前为止创建的
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)
注意:我没有使用与此文档绑定的容器。
您想将文本附加到段落并使该文本成为超链接。方法如下。
doc.appendParagraph('Tracking bug: ').appendText("567587").setLinkUrl("http://example.com");
这些方法作为链式工作:appendParagraph
returns 一个新创建的段落; appendText
returns 新添加的文本元素; setLinkUrl
链接该文本元素。不需要偏移量。
我不确定如何找到偏移值以使用 insertText 方法
我的 google 文档应该看起来像
第 1 段
第 2 段
跟踪错误:567587
第 3 段
所有段落数据都取自excel并写入doc
使用附加段落我无法仅对“567587”使用url。下面的脚本是我到目前为止创建的
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)
注意:我没有使用与此文档绑定的容器。
您想将文本附加到段落并使该文本成为超链接。方法如下。
doc.appendParagraph('Tracking bug: ').appendText("567587").setLinkUrl("http://example.com");
这些方法作为链式工作:appendParagraph
returns 一个新创建的段落; appendText
returns 新添加的文本元素; setLinkUrl
链接该文本元素。不需要偏移量。