替换表搜索文本

问题描述 投票:0回答:1

希望我能就此得到一些建议。我使用的脚本来自动执行文件,其中的数据来自谷歌现有文档的创建。使用连接表没有运气后,我试图通过在文档中搜索文本标识的位置插入一个表格。

例如,我会寻找TABLE1HERE文档中,并与[[Heading,Data],[Heading2,MoreData]]定义的表替换它。我试过下面的代码和一些没有运气的变化。任何指针将是非常美妙的。

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);

}
google-apps-script google-sheets google-docs
1个回答
1
投票

替换表文本

这个发现包含所需文本容器在childIndex。它删除的文字和插入表格插入同一个孩子。

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.