如何将命名范围添加到Google apps-script

时间:2015-06-04 21:21:50

标签: google-apps-script named-ranges

我想在Google文档中实现“html-span-like”功能,但每当我尝试将NamedRange添加到Google文档中的文本子字符串时,该范围就会与之前的文本合并同一段。

结果,NamedRange应用于整个段落。

这是我的测试用例:

function createTextNamedRange(){
  // Retrieve the current document's body
  var doc = DocumentApp.getActiveDocument();
  var docBody = doc.getBody();

  // Add a new paragraph with text {NotNamed}
  var para = docBody.appendParagraph('{NotNamed}');

  // Append some text that will be named ( .appendText() method returns a Text element )
  var textElem = para.appendText('{NamedText}');

  // Build a range for the Text element
  var range=doc.newRange().addElement(textElem).build();
  // Name the range and append it to the document
  doc.addNamedRange('myNamedRange',range);
}

然后我在此函数中使用NamedRange类显示Logger的内容:

function getTextNamedRange(){
  // Retrieve the named range
  var namedRanges =    DocumentApp.getActiveDocument().getNamedRanges();

  // Iterate through each instance of name 'myNamedRange' (there's only one)
  for (var nr in namedRanges){
    var namedRange = namedRanges[nr];

    // A range may contain several RangeElements, iterate through them
    var rangeElements = namedRange.getRange().getRangeElements();
    for(var re in rangeElements) {

      // Get the text of each RangeElement and display it in the logger
      var text = rangeElements[re].getElement().asText().getText();
      Logger.log('Text with namedRange (' + namedRange.getName() + ') : "' + text +'"');
    }
  }
}

我假设将{NamedText}作为输出,但是日志窗口告诉我:

Text with namedRange (myNamedRange) : "{NotNamed}{NamedText}"

可以看出,该范围适用于未标记文本和命名文本。

我找到了a workaround detailed below,但这绝对不符合我的口味:它包括在.appendText()次来电之间添加空内联图片。这样就不会合并Text元素了。

我仍在寻找更好的解决方案。

1 个答案:

答案 0 :(得分:1)

以下内容适用于仅支持,但似乎有效。

它依赖于必须命名的InlineImage块之前和之后的Text插入。

这会阻止NamedRangeText

中应用Paragraph块时合并
function createTextNamedRangeWorkaround(){
  // WORKAROUND: prevent the Text Merging (and hence the NamedRange merging)
  // by separating Text Elements with blank Inline Images

  // Import a blank PNG file
  // NOTE: This shouldn't be fetched each time 
  //       but only once and passed as an argument
  var blankImage = UrlFetchApp.fetch('http://upload.wikimedia.org/wikipedia/commons/d/d2/Blank.png');

  // Retrieve the current document's body
  var doc = DocumentApp.getActiveDocument();
  var docBody = doc.getBody();

  // Add a new paragraph with text {NotNamed}
  var para = docBody.appendParagraph('{NotNamed}');

  // WORKAROUND HERE : 
  para.appendInlineImage(blankImage.getBlob());
  // Append some text
  var textElem1 = para.appendText('{NamedText1}');

  // WORKAROUND HERE : 
  para.appendInlineImage(blankImage.getBlob());
  // Append some text
  var textElem2 = para.appendText('{NamedText2}');

  // Build Named ranges after text insertion (see notice below)
  var range1=doc.newRange().addElement(textElem1).build();
  // Name the range and append it to the document
  doc.addNamedRange('Range1',range1);
  var range2=doc.newRange().addElement(textElem2).build();
  // Name the range and append it to the document
  doc.addNamedRange('Range2',range2);
}

执行后,文档会附加一个新段落,其中包含由两个空行内联PNG分隔的三个单词

{NotNamed}{NamedText1}{NamedText2}

我的getTextNamedRange()的执行显示了所需的行为:

Text with namedRange (Range1) : "{NamedText1}"
Text with namedRange (Range2) : "{NamedText2}"

重要提示:

  • 必须在最后Text次插入后全部应用命名范围,否则,之前的NamedRanges会采用新的插入和附加。

  • InlineImages无法使用Paragraph从父image.removeFromParent()移除,因为这会导致Text元素(以及NamedRange)再次合并