Google apps script 向google文档文本添加自定义属性
我需要创建一个GoogleApps脚本,在活动文档中插入一些字符串。 我需要稍后知道用户是否在文档上使用了脚本并更改了已经插入的文本 是否可以为正在添加的字符串标记或插入自定义属性 例如,不添加Google apps script 向google文档文本添加自定义属性,google-apps-script,Google Apps Script,我需要创建一个GoogleApps脚本,在活动文档中插入一些字符串。 我需要稍后知道用户是否在文档上使用了脚本并更改了已经插入的文本 是否可以为正在添加的字符串标记或插入自定义属性 例如,不添加 <b>Custom Text</b> 自定义文本 可以插入这个吗 <p CustomAttribute=Cust1>Custom Text</p> 自定义文本 另外,我如何在文档中搜索我的自定义属性?您可以使用。基本上,策略是: 添加文本、段落或其
<b>Custom Text</b>
自定义文本
可以插入这个吗
<p CustomAttribute=Cust1>Custom Text</p>
自定义文本
另外,我如何在文档中搜索我的自定义属性?您可以使用。基本上,策略是:
function testing() {
// Add a new paragraph within a Named Range
var named = addTextWithNamedRange('This is my added text', 'RangeLabel01');
// NamedRanges can share the same names, but the IDs are unique,
// so use IDs to easily reference specific NamedRanges
var namedId = named.getId();
// Now save this ID to a data structure, along with any other information
// about it you need to record
// Later, when you need to reference that text/paragraph/element,
// use the ID to find it, and make any changes you need:
accessNamedRange(namedId);
}
function addTextWithNamedRange(str, name) {
// Add a new paragraph to end of doc
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.appendParagraph(str);
// Creates a NamedRange that includes the new paragraph
// Return the created Named Range
var rangeBuilder = doc.newRange();
rangeBuilder.addElement(text);
return doc.addNamedRange(name, rangeBuilder.build());
}
function accessNamedRange(rangeId) {
// Determine if a NamedRange with this ID exists
// If it does, log information about it and the Paragraph
// elements it includes
var doc = DocumentApp.getActiveDocument();
var named = doc.getNamedRangeById(rangeId);
if (named) {
var rangeElements = named.getRange().getRangeElements();
for(var r in rangeElements) {
var text = rangeElements[r].getElement().asParagraph().getText();
// Just logging here, but could potentially edit or
// otherwise manipulate the text
Logger.log('Found NamedRange ' + named.getName() +
' (id='+rangeId+') --> ' + text);
}
}
}