hea*_*ley 1 google-docs google-sheets google-apps-script
希望我能就此得到一些建议。我正在使用脚本自动创建文档,其中数据来自现有的谷歌文档。在使用链接表失败后,我尝试在文档中由搜索文本标识的点插入表格。
例如,我会在文档中搜索 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);
}
Run Code Online (Sandbox Code Playgroud)
用表格替换文本
这会找到包含所需文本的容器的 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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2200 次 |
| 最近记录: |