对所选文字使用replaceText(),而不是Google文档的整个元素 [英] Use replaceText() for selected text, not the entire element of a Google Document

查看:92
本文介绍了对所选文字使用replaceText(),而不是Google文档的整个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 replaceText()替换元素的选定区域中的单词,以下代码将替换整个元素中的匹配而不是所选区域。任何想法来解决这个问题?

How can I use replaceText() to replace a word in selected area of an element, the following code will replace matches in an entire element not the selected area. any ideas to fix this?

我知道我可以使用 isPartial()来区分部分选择,但不能

I know I can use isPartial() to separate partial selection but couldn't figure out how to replace the selected text.

function test() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (!selection) {
    DocumentApp.getUi().alert('Cannot find a selection in the document.');
    return;
  }

 var selectedElements = selection.getSelectedElements();
 for (var i = 0; i < selectedElements.length; ++i) {
  var selectedElement = selectedElements[i];
  //if (selectedElement.isPartial()) {
    selectedElement.getElement().asText().replaceText("alpha","beta");
    selectedElement.getElement().asText().replaceText("gamma","delta");
  //}
}
}


推荐答案

selectedElement.getElement().replaceText("alpha","beta");

这篇关于对所选文字使用replaceText(),而不是Google文档的整个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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