使用Google Doc Apps脚本缓存选定的文本元素 [英] Caching Selected Text Element with Google Doc Apps Script

查看:48
本文介绍了使用Google Doc Apps脚本缓存选定的文本元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是询问以下问题的更好方法.

Update: This is a better way of asking the following question.

Document 中的 Element 是否有类似 Id 的属性,以后可以用来访问该元素.假设我在文档中插入了一个段落,如下所示:

Is there an Id like attribute for an Element in a Document which I can use to reach that element at a later time. Let's say I inserted a paragraph to a document as follows:

var myParagraph = 'This should be highlighted when user clicks a button';
body.insertParagraph(0, myParagraph);

然后,用户手动(即通过键入或粘贴)在开头插入另一个.现在,我的段落的 childIndex 0 更改为 1 .我想稍后再谈及该段并突出显示.但是由于插入, childIndex 不再有效.对于 Element 接口,没有任何类似于 Id 的属性,也没有任何实现该属性的类型. CahceService PropertiesService 仅接受 String 数据,因此我无法将 myParagraph 存储为 Object .

Then the user inserts another one at the beginning manually (i.e. by typing or pasting). Now the childIndex of my paragraph changes to 1 from 0. I want to reach that paragraph at a later time and highlight it. But because of the insertion, the childIndex is not valid anymore. There is no Id like attribute for Element interface or any type implementing that. CahceService and PropertiesService only accepts String data, so I can't store myParagraphas an Object.

你们有什么想法要实现我想要的吗?

Do you guys have any idea to achieve what I want?

谢谢

同一问题的旧版本(可选阅读):

想象一下,用户选择了一个单词并按下了我的加载项的突出显示按钮.然后,她再做几个字就做同样的事情.然后,她以一种方式更改了这些突出显示的单词的开始-结束索引的方式编辑文档.

Imagine that user selects a word and presses the highlight button of my add-on. Then she does the same thing for several more words. Then she edits the document in a way that the start end end indexes of those highlighted words change.

此时,她按下删除突出显示按钮.我的加载项应禁用所有先前选择的单词的突出显示.问题是我不想扫描整个文档并找到任何突出显示的文本.我只想直接访问以前选择的那些.

At this point she presses the remove highlighting button. My add-on should disable highlighting on all previously selected words. The problem is that I don't want to scan the entire document and find any highlighted text. I just want direct access to those that previously selected.

有没有办法做到这一点?我尝试缓存选定的元素.但是,当我从缓存取回它们时,出现 TypeError:在对象Text中找不到函数insertText.错误.似乎是对象的类型,或者在 cache.put() cache.get()之间进行了某些更改.

Is there a way to do that? I tried caching selected elements. But when I get them back from the cache, I get TypeError: Cannot find function insertText in object Text. error. It seems like the type of the object or something changes in between cache.put() and cache.get().

var elements = selection.getSelectedElements();
    for (var i = 0; i < elements.length; ++i) {
      if (elements[i].isPartial()) {
        Logger.log('partial');
        var element = elements[i].getElement().asText();

        var cache = CacheService.getDocumentCache();
        cache.put('element', element);  


        var startIndex = elements[i].getStartOffset();
        var endIndex = elements[i].getEndOffsetInclusive();
     }
    // ...
   }

当我取回元素时,我得到 TypeError:在对象Text中找不到函数insertText.错误.

When I get back the element I get TypeError: Cannot find function insertText in object Text. error.

 var cache = CacheService.getDocumentCache();
 cache.get('text').insertText(0, ':)');  

我希望我能清楚地说明我想要实现的目标.

I hope I can clearly explained what I want to achieve.

推荐答案

一种直接方法是添加书签,它不依赖于后续的文档更改.它有一个缺点:每个人都可以看到书签...

One direct way is to add a bookmark, which is not dependent on subsequent document changes. It has a disadvantage: a bookmark is visible for everyone...

更有趣的方法是添加命名范围具有唯一的名称.示例代码如下:

More interesting way is to add a named range with a unique name. Sample code is below:

function setNamedParagraph() {
  var doc = DocumentApp.getActiveDocument();
  // Suppose you want to remember namely the third paragraph (currently)
  var par = doc.getBody().getParagraphs()[2];
  Logger.log(par.getText());
  var rng = doc.newRange().addElement(par);
  doc.addNamedRange("My Unique Paragraph", rng);
}


function getParagraphByName() {
  var doc = DocumentApp.getActiveDocument();
  var rng = doc.getNamedRanges("My Unique Paragraph")[0];
  if (rng) {
    var par = rng.getRange().getRangeElements()[0].getElement().asParagraph();
    Logger.log(par.getText());
  } else {
    Logger.log("Deleted!");
  }
}

第一个函数将第三段标记"为命名范围.尽管随后进行了文档更改,但第二个命令还是以范围名称作为该段.确实在这里,当我们删除唯一段落"时,我们需要考虑例外情况.

The first function "marks" the third paragraph as named range. The second one takes this paragraph by the range name despite subsequent document changes. Really here we need to consider the exception, when our "unique paragraph" was deleted.

这篇关于使用Google Doc Apps脚本缓存选定的文本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆