在一定数量的段落后插入分页符

时间:2012-06-11 15:12:58

标签: google-apps-script

我编写了一个脚本,根据电子表格中的数据生成一个段落,它为每行数据添加一个新段落。我的问题是一些段落在页面之间分开,我无法弄清楚如何在6段之后自动插入分页符(实际上是第一页上的5页和之后每页的6页)段落的实例。

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("ecaldwell@kmrmedical.com");
}

我尝试了一个if语句来检查 q 是否等于5或者之后的倍数,但我一定是做错了。我确定这是一个简单的修复,但我对此很新,所以任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

尝试下面的var divider = doc ...

  if(q==5||(q>6&&q/6==parseInt(q/6))){doc.appendPageBreak()}