Deleting whitespace in a cleared Doc page.

42 views
Skip to first unread message

Matteo Hee

unread,
Sep 24, 2020, 11:29:10 PM9/24/20
to Google Apps Script Community
I'm trying to write a simple script to delete the first page from a Google doc (well, many, many Google docs, but that part I've got figured out already). I haven't had any luck with the .removeFromParent or other commands, but I seem to be able to delete the text using the paragraph.clear() command. 

However, the cleared paragraphs leave a page of whitespace (and bullet points) that I would like to get rid of. How can I delete the page completely so the document starts from Page 2?



----------------------------


function deleteFirstPage(target) {
  
  
var deletePages = 1;


var paragraph = DocumentApp.openById(target)
  .getBody()
  .getParagraphs();
  
var counter = 0;
for (var i = 0; i < paragraph.length; i++) {
  for (var j = 0; j < paragraph[i].getNumChildren(); j++){
    if (
      paragraph[i].getChild(j).getType() == DocumentApp.ElementType.PAGE_BREAK
    )
      counter++;
   
  }
  
  //paragraph[i].editAsText().setText('');
  paragraph[i].clear();
 
  if (counter == deletePages) break;
}


Martin Molloy

unread,
Sep 25, 2020, 6:07:22 AM9/25/20
to google-apps-sc...@googlegroups.com
try 

paragraph[i].removeFromParent();

instead


Martin Molloy


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/bbc8927d-f37b-420c-b5a3-67eb2ced1606o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages