GAS是否可以立即替换getActiveDocument().getSelection()? [英] GAS is it possible to replace getActiveDocument().getSelection() at once?

查看:44
本文介绍了GAS是否可以立即替换getActiveDocument().getSelection()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户在他的Gdoc中具有以下选择.

My User has the following selection in his Gdoc.

现在他要从侧边栏中替换在文档上所做的选择.
GAS问题是是否可以一次执行该操作,例如:
var selection = DocumentApp.getActiveDocument().getSelection() selection.replace("newtext")
还是我必须遍历 selection.getRangeElements()以便删除它们(或替换它们),然后将新文本放置在那个位置?

Now from the sidebar he wants to to replace the selection he made on the document.
The GAS question is if it is possible to do that at once, something like:
var selection = DocumentApp.getActiveDocument().getSelection() selection.replace("newtext")
Or do I have to loop through selection.getRangeElements() in order to delete them (or replace them) and than in someway place the new text in that position?

推荐答案

不可能,这是不可能的(嗯,如果没有的话,则没有记载).

Not, that's not possible (well, if it is, it's not documented).

您必须遍历所选元素,主要是因为所选内容可能包含段落的一部分,从而迫使您进行管理.即仅删除所选部分.对于完整的选定元素,您可以将它们完全删除(如图像).

You have to loop through the selected elements, mainly because the selection may take part of paragraphs, forcing you to manage that. i.e. deleting just the selected part. And for completed selected elements, you can just remove them entirely (like images).

这是一个实现方法的实现(我修改了Kaylan的Translate脚本的一部分,以正确替换图像和部分选定的段落.

Here's an implementation on how to do this (part of the Kaylan's Translate script modified by me to properly replace images and partially selected paragraphs.

function replaceSelection(newText) {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var elements = selection.getRangeElements();
    var replace = true;
    for (var i = 0; i < elements.length; i++) {
      if (elements[i].isPartial()) {
        var element = elements[i].getElement().asText();
        var startIndex = elements[i].getStartOffset();
        var endIndex = elements[i].getEndOffsetInclusive();
        var text = element.getText().substring(startIndex, endIndex + 1);
        element.deleteText(startIndex, endIndex);
        if( replace ) {
          element.insertText(startIndex, newText);
          replace = false;
        }
      } else {
        var element = elements[i].getElement();
        if( replace && element.editAsText ) {
          element.clear().asText().setText(newText);
          replace = false;
        } else {
          if( replace && i === elements.length -1 ) {
            var parent = element.getParent();
            parent[parent.insertText ? 'insertText' : 'insertParagraph'](parent.getChildIndex(element), newText);
            replace = false; //not really necessary since it's the last one
          }
          element.removeFromParent();
        }
      }
    }
  } else
    throw "Hey, select something so I can replace!";
}

这篇关于GAS是否可以立即替换getActiveDocument().getSelection()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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