用 table 替换搜索文本

Replace search text with table

希望我能得到一些建议。我正在使用脚本自动创建文档,其中数据来自现有的 google 文档。在使用链接 tables 失败后,我尝试在文档中由搜索文本标识的点插入 table。

例如,我会在文档中搜索 TABLE1HERE 并将其替换为 [[Heading,Data],[Heading2,MoreData]] 定义的 table。我已经尝试了下面的代码和一些没有运气的变体。任何指针都会很棒。

function updateTablesTEST(searchText,replacementTable){

  var document = DocumentApp.getActiveDocument();
  var documentBody = document.getBody();
  var textLocation;
  var newPosition;
  var textChild;

  textLocation = documentBody.findText(searchText).getElement();
  textChild = textLocation.getParent().getChildIndex(textLocation);
  documentBody.insertTable(textChild+1, replacementTable);

}

将文本替换为 Table

这会找到包含所需文本的容器的 childIndex。它删除文本并将 table 插入到同一个子项中。

function searchTextReplTable(texttofind){
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  var rgel=body.findText(texttofind);
  var element=rgel.getElement();
  var childIndex=body.getChildIndex(element.getParent());
  var t=[];//This is the table I used
  for(var i=0;i<5;i++){//just created a 5,5 table with row, col indices
    t[i]=[];
    for(var j=0;j<5;j++){
      t[i][j]=Utilities.formatString('%s,%s',i,j);
    }
  }
  body.getChild(childIndex).asText().setText('');
  body.insertTable(childIndex,t)
}