Search code examples
google-apps-scriptgoogle-docs

How to delete a page from a google doc using app script


I have created a template with several pages. After the template is used, I want to automatically remove any unused page.

This is how it works. I have created a certificate template with 25 pages with placeholders for name and other details. Each page is a certificate. After going through the data, the code below replaces the placeholders with data from the Google Sheets. Once that is completed - I want to remove all the extra pages in the document - for example: if only 5 template pages are modified, I want to remove the remaining 20 template pages from the document.

Any other improvement suggestions are welcome as this is my first App Script.

Addition to clarify the question:

The script takes data from a Google Sheet which has hundreds of rows of data. For example, if 5 certificates need to be created, the script gets all the data and loop and look for a certain flag (cert_data[i][6] == 1) to identify the rows of data that should be used for the certificate. Once the flag is found, the data in the row are stored in variables and is used to replace the place holders in the template file. Once the data in all flagged rows are replaced - for this example, only 5 template pages are replaced. Hence there will be a balance of 20 pages in the template that has not been used - I want to delete these pages.

function createDocument() {

  //Setting ID for database
  var SPREADSHEET_ID = "doc ID"
  var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID);
  var worksheet = spreadsheet.getSheetByName("Sheet1");

  var cert_data = worksheet.getDataRange().getValues();  
  //Setting template ID
  var templateId = 'template ID goes here';

  //Make a copy of the template file
  var documentId = DriveApp.getFileById(templateId).makeCopy().getId();


  //Get the document body as a variable
  var body = DocumentApp.openById(documentId).getBody();

  //Foramt date
  var curDate = Utilities.formatDate(new Date(), "GMT+1", "dd MMMM yyyy")

  var d = 0;


  //Looping through all the data that is in the Google Sheet
  for(var i = 1; i < cert_data.length ; i++){

    var curdata = cert_data[i][6];

    //Checking if the row data is to be used to create certificate
    if (cert_data[i][6] == 1) {
      var training_type = cert_data[i][12];
      var hours = cert_data[i][9];
      var user_name = cert_data[i][1];
      var NIC = cert_data[i][3];
      var date_completed = Utilities.formatDate(cert_data[i][8], "GMT+1", "dd MMMM yyyy");
      var company = cert_data[i][2];
      var cert_number = cert_data[i][0];
      var date_now = curDate;

      //Setting training names

      if (training_type == "01G") {training_type = "Basic First Aid" + String.fromCharCode(10) + "& Automated External" + String.fromCharCode(10) + "Defibrillator Certificate"; var file_name = 'AED Training';}

      if (training_type == "01B") {var file_name = 'Refresher Receipts';}

      d++;

      //Insert the data into the file
      body.replaceText('{training_type' + d + '}', training_type);
      body.replaceText('{hours' + d + '}', hours);
      body.replaceText('{name' + d + '}', user_name);
      body.replaceText('{NIC' + d + '}', NIC);
      body.replaceText('{date_completed' + d + '}', date_completed);
      body.replaceText('{company' + d + '}', company);
      body.replaceText('{cert_numb' + d + '}', cert_number);
      body.replaceText('{date_now' + d + '}', date_now);
    }
  }

    //d is the number of pages used from the template file
    //I want to delete all the balance pages (i.e. 25-d = x)
    //Rename the copied file
    DriveApp.getFileById(documentId).setName(file_name + ' - ' + company);

}

Solution

    • Each page of your Google Document is separated by the page breaks.
    • You want to delete several pages of Google Document from the last page in order.
    • Number of pages you want to delete is decided by your script.

    If my understanding is correct, how about this sample script? The flow of this script is as follows.

    Flow:

    1. Retrieve paragraphs in the body of Document.
    2. Retrieve elements in each paragraph. The page break is included in the paragraph.
    3. Delete elements from last page in order.
    4. When the number of page breaks is the same with deletePages, the script is stopped.

    By this flow, several pages can be deleted from the last page in order.

    Sample script:

    Please copy&paste the following script and set the variables of deletePages and id. Then, run the script.

    function myFunction() {
      var deletePages = 3;
      var id = "### documentId ###";
    
      var paragraph = DocumentApp.openById(id)
        .getBody()
        .getParagraphs();
      var counter = 0;
      for (var i = paragraph.length - 1; i >= 0; i--) {
        for (var j = 0; j < paragraph[i].getNumChildren(); j++)
          if (
            paragraph[i].getChild(j).getType() == DocumentApp.ElementType.PAGE_BREAK
          )
            counter++;
        if (counter < deletePages)
          paragraph[i].clear();
        else if (counter == deletePages){
          paragraph[i].getChild(paragraph[i].getNumChildren() - 1).removeFromParent();
          break;
        }
      }
    }
    

    Note:

    • If you want to delete 5 pages from the last page, please set var deletePages = 5.
    • Also you can use this function like myFunction(deletePages) as a method. At that time, please remove var deletePages = 3;.
    • You can also use DocumentApp.getActiveDocument() instead of DocumentApp.openById(id).
    • From @derekantrican's suggestion, I updated the sample script.

    References: