在documentation sample之后,我正在尝试创建一个搜索google文档中的数字列表的函数,如果找到它,则会向该列表中添加一个新项目。但是我收到此错误:Cannot find method setListId(string). (line 21, file "test")
或者,如果我更改第21行内容(替换elementContent
的{{1}}),我收到消息:newElement
并且没有任何反应。如何解决?
这是我的代码:
Preparing for execution...
答案 0 :(得分:1)
根据我的评论,我试着用你的剧本来看看发生了什么,我想出了下面的代码......
我并不是说它解决了你的问题和/或是达到你想要的最佳方式,但至少它会给出一个按预期工作的结果。
请将其视为“新游乐场”并继续尝试以使其更好; - )
function test() {
var elementContent = "New item testing"; // a paragraph with its formating
var targetDocId = DocumentApp.getActiveDocument().getId();
var targetDoc = DocumentApp.openById(targetDocId);
var body = targetDoc.getBody();
var childIndex = 0;
for (var i = 0; i < targetDoc.getNumChildren(); i++) {
var child = targetDoc.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM){
while(child.getType() == DocumentApp.ElementType.LIST_ITEM){
child = targetDoc.getChild(i)
childIndex = body.getChildIndex(child);
Logger.log(childIndex)
i++
}
child = targetDoc.getChild(i-2)
var listId = child.getListId();
Logger.log(childIndex)
var newElement = child.getParent().insertListItem(childIndex, elementContent);
newElement.setListId(child);
break;
}
}
}