复制文档的页面并替换每个页面上的文本 [英] Duplicating a page of a document and replacing text on each page

查看:223
本文介绍了复制文档的页面并替换每个页面上的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将采用Google Doc模板文档,制作副本,用电子表格中的一行替换某些文本,附加另一个页面,将文本替换为电子表格,追加另一个页面等。

I am trying to create a script that will take a Google Doc template document, make a copy, replace certain text with information from a row on my spreadsheet, append another page, replace the text with information from the next row on the spreadsheet, append another page, etc.

这是我到目前为止:

Here is what I have so far:

// Global variables 
var templateDocID = ScriptProperties.getProperty("backRxRequestDocID");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeSheetName = sheet.getName();
var user = Session.getUser().getEmail();

function requestGen3() {
  var physName = ["doc john", "doc evan", "doc jane"];
  var physAddr1 = ["fake st.", "faker st.", "fakest st."];
  var physAddr2 = ["ste 100", "", "ste 209"];
          var physCity = ["SLC", "Provo", "Orem"];
  var physState = ["UT", "AZ", "NV"];
  var physZip = ["84049", "84044", "84601"];
          var physPhone = ["8015555555", "7206666666", "4803333333"];
  var ptName = ["ed", "sue", "izzy"];
  var ptDOB = ["12/10/1979", "1/1/2001", "45/94/4561"];
  // Get document template, copy it as a new temp doc, and save the Doc’s id
  var docID = DocsList.getFileById(templateDocID).makeCopy().getId();
  var doc = DocumentApp.openById(docID);
  var body = doc.getActiveSection();
  var pars = doc.getParagraphs();
  var bodyCopy = body;
  for (var i = 0; i < physName.length; ++i) {
    // Replace place holder keys,  
    body.replaceText('%PHYS_NAME%', physName[i]);
    body.replaceText('%PHYS_ADDR1%', physAddr1[i]);
    body.replaceText('%PHYS_ADDR2%', physAddr2[i]);
    body.replaceText('%PHYS_CITY%', physCity[i]);
    body.replaceText('%PHYS_STATE%', physState[i]);
    body.replaceText('%PHYS_ZIP%', physZip[i]);
    body.replaceText('%PHYS_PHONE%', physPhone[i]);
    body.replaceText('%PT_NAME%', ptName[i]);
    body.replaceText('%PT_DOB%', ptDOB[i]);    
    doc.appendPageBreak();
    for (var j = 0; j < pars.length; ++j) {
      doc.appendParagraph(pars[j].copy());
    }
  }

  // Save and close the document
  doc.saveAndClose();
}

我从电子表格中阅读了教程,但看不到使 getRowsData() getObjects()正常工作。我上面的脚本正确地创建了文档,但没有将第二组信息插入第二页,第三组插入到第三页等。

I went through the tutorial on reading from the spreadsheet but I couldn't seem to make getRowsData() and getObjects() to work properly. My script above is creating the document properly but is not inserting the second set of info into the second page and third set into the third page, etc.

推荐答案

只有在复制段落后,才需要替换文本,因为如果您之后这样做了,则占位符将被替换,并且不会出现在下一个副本中。你可以这样做:
$ b

You have to replace the text only after you copied the paragraphs, because if you do it after, the placeholders will be already replaced and will not be present for the next copies. You can do it like this:

//...
var pars = doc.getParagraphs();
for( var i in pars ) //loop to keep a copy of the original paragraphs
  pars[i] = pars[i].copy();

for( var i = 0; i < physName.length; ++i ) {
  body.replaceText('%PHYS_NAME%', physName[i]);
  //do all your replaces...
  if( i != physName.length-1 ) { //has next?
    doc.appendPageBreak();
    for( var j in pars )
      doc.appendParagraph(pars[j].copy());
  }
}
doc.saveAndClose();

这篇关于复制文档的页面并替换每个页面上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆