Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:09:18+00:00 2026-06-04T20:09:18+00:00

I am trying to create a script that will take a Google Doc template

  • 0

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();
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:09:19+00:00Added an answer on June 4, 2026 at 8:09 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just as the title says, I'm trying to create a script that will take
Im trying to create a bourne shell script that will take 0 or more
I am trying to create a script that will take output of a system
I'm trying to create a script that will read text files and then analyse
Im trying to create a script that will post a notification stored in a
I've been trying to create a script for myself that will allow me to
I'm trying to write a script that will create a user in MediaWiki, so
im trying to create a script that opens a file and replace every 'hola'
I'm trying to create a shell script that shall convert certain Files according to
I'm trying to create a script that will allow me to monitor CPU Utilization,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.