Erase content in Google Apps Script Document Servi

2020-03-19 02:21发布

How to flush a document in Google Apps Script Document Service? Do I need to loop through all the kind of elements e.g. paragraph, image, table and remove them as a child? Is there an easier way to delete everything in the body of a document?

Thank you!

4条回答
We Are One
2楼-- · 2020-03-19 02:48

Counter intuitively (but, as documented) setText("") clears more than the text, it removes images as well.

查看更多
太酷不给撩
3楼-- · 2020-03-19 02:55

According to the Documentation, the Document.setText should be able to clear the document content. I assume the following command should do it.

doc.setText('');
查看更多
萌系小妹纸
4楼-- · 2020-03-19 03:02

In the current Document of google apps script as doc.setText method is not available this could be achieved by doc.getBody().clear()

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-03-19 03:02

May i mention that the body.clear() method does not remove bookmarks, because they are associated with the document.

This worked for me:

scratchBody = scratchDoc.getBody();
scratchBody.clear();
bookmarks   = scratchDoc.getBookmarks();
while ( bookmarks.length ) { bookmarks.shift(); }

I didn't try this yet, but as with the bookmarks, there could be the HeaderSection, the FooterSection, and the FootnoteSections remaining in the document.

查看更多
登录 后发表回答