在google API脚本中获取findtext的子索引

use*_*736 5 google-apps-script

我的目标是使用其他文档的内容替换Google云端硬盘文档中的一段文字.

我已经能够将文档插入到另一个文档中的某个位置,但是我无法确定要替换的文本的子索引.这是我到目前为止:

function replace(docId, requirementsId) {

var body = DocumentApp.openById(docId).getActiveSection();
var searchResult = body.findText("<<requirementsBody>>");
var pos = searchResult.?? // Here I would need to determine the position of the searchResult, to use it in the insertParagraph function below

var otherBody = DocumentApp.openById(requirementsId).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
  var type = element.getType();
  if( type == DocumentApp.ElementType.PARAGRAPH ) {
      body.insertParagraph(pos,element);   
  } else if( type == DocumentApp.ElementType.TABLE ) {
    body.insertTable(pos,element);
  } else if( type == DocumentApp.ElementType.LIST_ITEM ) {
    body.insertListItem(pos,element);
  } else {
    throw new Error("According to the doc this type couldn't appear in the body: "+type);
  }
}


};
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

bru*_*uce 5

findText()

返回RangeElement.

您可以使用

var r = rangeElement.getElement()

获取包含找到的文本的元素.

要获得其childIndex,您可以使用

r.getParent().getChildIndex(r)
Run Code Online (Sandbox Code Playgroud)