Deleting Content in Google Apps Script Document Service

How to clear a document in Google Apps Script Document Service? Do I need to iterate over all elements, for example. paragraph, images, tables and delete them as a child? Is there an easier way to delete everything in the body of a document?

Thanks!

+6
source share
4 answers

According to the Documentation , Document.setText should be able to clear the contents of the document. I guess the next team should do this.

 doc.setText(''); 
+8
source

The counter intuitively (but as documented) setText ("") clears more than text It also deletes images.

0
source

In current google script applications, since the doc.setText method is not available, this can be achieved with doc.getBody (). Clear ()

0
source

May I mention that the body.clear () method does not delete 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 have not tried this yet, but, as with bookmarks, there may be HeaderSection, FooterSection, and FootnoteSections sections in the document.

0
source

Source: https://habr.com/ru/post/921016/


All Articles