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.
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);
}
If my understanding is correct, how about this sample script? The flow of this script is as follows.
deletePages
, the script is stopped.By this flow, several pages can be deleted from the last page in order.
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;
}
}
}
var deletePages = 5
.myFunction(deletePages)
as a method. At that time, please remove var deletePages = 3;
.DocumentApp.getActiveDocument()
instead of DocumentApp.openById(id)
.