Reputation: 85
i have written a script that generates a paragraph based on data from a spreadsheet, it adds a new paragraph for each line of data. my problem is that some of the paragraphs are split between pages and i can't figure out how to insert a page break automatically after 6 (actually 5 on the first page and 6 for every page after that) instances of the paragraph.
function worksheetGen () {
var templateDocID = ScriptProperties.getProperty("listTemplateDocID");
var docID = DocsList.getFileById(templateDocID).makeCopy().getId();
var doc = DocumentApp.openById(docID);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeSheetName = sheet.getName();
var user = Session.getUser().getEmail();
if (sheet != null) {
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastACell = sheet.getRange("A"+lastRow);
Logger.log("Last Column: " + lastColumn + " Last A Cell: " + lastACell)
for (var q = 0; q < lastRow; ++q){
var firstName = sheet.getRange(q+2,3).getValue();
var lastName = sheet.getRange(q+2,4).getValue();
var include = sheet.getRange(q+2,1).getValue();
var dispo1 = "Request Left With Dr.";
var dispo2 = "Needs Appointment";
var dispo3 = "Prescription Received";
var dispo4 = "Prescription Denied";
var notes = "Notes:";
if (firstName != null && include == "Y") {
var nameSet = firstName + " " + lastName + ": ";
var dispoSet = "| " + dispo1 + " | " + dispo2 + " | " + dispo3 + " | " + dispo4 + " |";
var cells = [[nameSet, dispoSet]];
var table = doc.appendTable(cells);
table.setBorderWidth(0).setColumnWidth(1, 334);
var cell2 = table.getCell(0, 1).getChild(0).asParagraph();
cell2.setAlignment(DocumentApp.HorizontalAlignment.RIGHT).setFontSize(8);
var content2 = doc.appendParagraph(notes);
var divider = doc.appendHorizontalRule();
}
}
}
doc.addFooter().appendParagraph(activeSheetName + " -- " + new Date());
doc.setName(activeSheetName + " - " + Utilities.formatDate(new Date(), "MDT", "MM/dd/yy"));
doc.addEditor(user);
doc.addEditor("[email protected]");
}
i tried an if statement to check if q equaled 5 or a multiple after that but i must have been doing it wrong. i'm sure it's a simple fix but i'm fairly new to this so any help is appreciated.
Upvotes: 1
Views: 4930
Reputation: 46802
Try this just below var divider = doc...
if(q==5||(q>6&&q/6==parseInt(q/6))){doc.appendPageBreak()}
Upvotes: 2