2013-10-11 81 views
1

我很难过。我有一个文档,它是一系列的段落和表格。有时候,我需要删除文档的最后一个段落,而且我想这样做而不留下任何空的空格。删除段落错误

目前,我得到这个错误:

Can't remove the last paragraph in a document section.

有谁知道为什么发生这种情况?有没有解决方法?

+0

似乎与到https://code.google.com/p/google-apps-script-issues/issue s/detail?id = 1489您应该将您的经验添加到问题中,并将其加入星标以获得反馈。 – ScampMichael

+0

您可能想要创建一个类型为type的新问题:缺陷,因为上述问题是增强请求。 – ScampMichael

回答

2

试试这个办法,似乎工作如预期,至少我没有成功,使其无法;-)

function deleteAllParagraphs() { 
    var doc = DocumentApp.getActiveDocument().getBody(); 
    doc.insertParagraph(0, '');// insert a dummy paragraph at the beginning of the doc that we are going to let there 
    doc.appendParagraph('');// append another dummy in case doc is empty 
    for(var p=doc.getNumChildren();p>=0;p--){ 
    try{ 
     doc.getChild(p).removeFromParent(); 
    }catch(e){} 
    } 
} 

编辑:issue tracker我找到了更好的通过代码#11建议cyberaxe 我略作修改:

function emptyDocument() { 
    var document = DocumentApp.getActiveDocument(); 
    var body = document.getBody(); 
    body.appendParagraph('');// to be sure to delete the last paragraph in case it doesn't end with a cr/lf 
    while (body.getNumChildren() > 1) body.removeChild(body.getChild(0)); 
} 
+0

你好。谢谢您的回复。 我想知道如何在最初的问题中使用它?这看起来好像在摆脱文档中的一切。我读的代码错了吗? – cvnntg