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 8591945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:38:14+00:00 2026-06-11T23:38:14+00:00

This is a followup to a question I asked yesterday on the Google Apps

  • 0

This is a followup to a question I asked yesterday on the Google Apps Script Office Hours Hangout.

The goal of my final script is to create an election process for student elections at the high school where I work using Google Forms. The script has three parts: 1) Create Unique “Voting IDs” (a random 6-digit code) 2) Merge the student data (Name, Homeroom, & Voting ID) on with a template document that will create specific voting instruction for each student. (i.e. an old-fashioned mail merge) 3) Verify the results by checking Voting ID’s and removing duplicate votes.

The part of the script that I am having trouble with is the student data merge (step 2). The first dataset is the only one that works. The rest show up as “DocumentBodySection”. I have a feeling it is either how I am copying the text from the Document Template or how I am adding the text to the new document.

Spreadsheet w/ Data: https://docs.google.com/spreadsheet/ccc?key=0AierVcXWELCudFI1LU10RnlIVHNsUm11a0dDWEV6M1E

Document Template: (see followup comment for url)

Document Created by Script: https://docs.google.com/document/d/12r2D9SpIVmQYVaasMyMWKjHz6q-ZZyIMEBGHTwlQct8/edit

//Get Settings & Data
ss = SpreadsheetApp.getActiveSpreadsheet();
source_sheet = ss.getSheetByName("Student Data");
settings_sheet = ss.getSheetByName("SETTINGS");
results_column = settings_sheet.getRange("B19").getValue();
source_column = settings_sheet.getRange("B18").getValue();
source_lastrow = source_sheet.getLastRow();
docTemplateID = settings_sheet.getRange("B13").getValue();
docCopyName = settings_sheet.getRange("B14").getValue();

//Merge Student Data with Document
function SendDataMerge () {
  // Open docTemplate and Copy Contents to entryTemplate
     var docTemplate = DocumentApp.openById(docTemplateID);
     var entryTemplate = docTemplate.getActiveSection();
     docTemplate.saveAndClose();
  // Make a NEW copy of docTemplate
     var docTemplate = DocsList.getFileById(docTemplateID);
     var docCopy = DocsList.copy(docTemplate, docCopyName);
     var docCopyID = docCopy.getId();
  // Create Array of Student Data (First, Last, Grouping, VID)
     var data = source_sheet.getRange("A2:D"+source_lastrow).getValues();
  // Open docCopy for Editing & Clear Contents
     var doc = DocumentApp.openById(docCopyID);
     var docText = doc.editAsText();
  // Run through Student Data
     for(var i=0; i<5 /*data.length*/; i++) { //For testing, limit this to 5 entries 
       var lastName = data[i][0];
       var firstName = data[i][1];
       var grouping = data[i][2];
       var vid = data[i][3];
       docText.replaceText('keyLastName', lastName);
       docText.replaceText('keyFirstName', firstName);
       docText.replaceText('keyGrouping', grouping);
       docText.replaceText('keyVID', vid);
       docText.appendText('\n*** Appended Text (End of entry) ***');
       docText.appendText(entryTemplate);
     }
  // Save and Close 
     doc.saveAndClose();
  }
  • 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-11T23:38:16+00:00Added an answer on June 11, 2026 at 11:38 pm

    I worked around this issue by creating a copy of the template, doing the text replacement and then appending the template elements from the original document into the copy. In particular, I used: var copyTables = templateDoc.getTables(); to fetch and store the tables (as all of my template data was contained in a table) and copyDoc.appendTable(copyTables[0].copy() ); to append the copy (the .copy() at the end seems to work the real magic). This provides the flexibility of updating the template in the friendly Documents interface without having to see a programmer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup to a question I asked yesterday: Have you ever had
This is a followup to a question I posted yesterday. I thought everything was
This is a follow-up to a question I asked yesterday. What I want to
this is actually a follow up to a question I asked yesterday about problems
This is a followup to another question I asked abou creating your own hooks.
This is a followup question to another question I asked earlier. I thought I
This is a followup to another question I asked earlier ( Earlier redirect question
I have a followup question to the one asked here: Java Abstract Class (this
This is a followup to the question I just asked here . I've created
I refer to the question I asked yesterday and have followup questions: Since I

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.