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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:16:34+00:00 2026-06-17T01:16:34+00:00

After trying a few mail merge scripts, I decided t write my own. My

  • 0

After trying a few mail merge scripts, I decided t write my own. My merge script runs as a separate fiIt reads a template from a GDoc, reads data from a GSpreadsheet, and merges it either into Gmails or into a new GDoc – one page / email per SS row.

The problem is that it doesn’t copy text formatting, margins or images into the Gmail or new GDoc … only the plain text.

I am using DocumentApp.openById > getActiveSection > getText() to capture the text.

Here is the code in GDoc http://goo.gl/fO5vP
I can’t seem to share a script so I had to put it in a doc. Copy it to a new script and it will be color coded.

  • 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-17T01:16:35+00:00Added an answer on June 17, 2026 at 1:16 am

    You should copy the template first using DocsList so you start with a “complete” initial document.

      var template = DocsList.getFileById(docIDs[0]);// get the template model, in this sample I had an array of possible templates, I took the first one
      var newmodelName=template.substr(0,11)+'multipage'+template.substring(18);// define a new name, do what you need here...
      var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
      var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
    

    then use the document class that has a direct replaceText method


    EDIT : about your secondary question, here is a suggestion on how you could do. It works nicely except for inlineImage, I’ll keep looking at this. You could also make the script more universal by adding other element types…

    function myFunction() {
      var template = DocsList.getFileById(key);// get the template model
      var newmodelName='testcopy';// define a new name, do what you need here...
      var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
      var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
      var body = baseDoc.getActiveSection();
      body.appendPageBreak();
      var totalElements = body.getNumChildren();
      for( var j = 0; j < totalElements; ++j ) {
        var element = body.getChild(j).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH )
          body.appendParagraph(element);
        else if( type == DocumentApp.ElementType.TABLE )
          body.appendTable(element);
        else if( type == DocumentApp.ElementType.LIST_ITEM )
          body.appendListItem(element);
        else if( type == DocumentApp.ElementType.INLINE_IMAGE )
          { var blob = body.getChild(j).asInlineImage().getBlob();
           body.appendImage(blob); }
      }
    }
    

    Edit 2 Thanks to @Fausto, here is a fully working version. Inline images are included in a paragraph so we had to dig one level more to get the blob…

    function myFunction() {
      var template = DocsList.getFileById(key);// get the template model
      var newmodelName='testcopy';// define a new name, do what you need here...
      var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
      var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
      var body = baseDoc.getActiveSection();
      body.appendPageBreak();
      var totalElements = body.getNumChildren();
      for( var j = 0; j < totalElements; ++j ) {
        var element = body.getChild(j).copy();
        var type = element.getType();
        if (type == DocumentApp.ElementType.PARAGRAPH) {
          if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
            var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
            body.appendImage(blob);
          }
          else body.appendParagraph(element.asParagraph());
        }
        else if( type == DocumentApp.ElementType.TABLE )
          body.appendTable(element);
        else if( type == DocumentApp.ElementType.LIST_ITEM )
          body.appendListItem(element);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After few weeks break, I'm trying to expand and extend my knowlege of templates
After trying a lot i decided to ask question on stackoverflow, I create a
I asked this question earlier today. After trying out suggestions from a few members,
I am trying to recreate that Google-style toolbar on G-mail and a few other
After trying few packages with git hub, and trying to parse/process this quite a
I'm trying to write a script that logs into my email server(yahoo) and checks
I just finally after trying many time over the past few years, got my
[FIXED] After trying quite a few different solutions I finally got one to work.
I'm implementing RavenDB in a project and after a few days trying the database
after having spent few months in trying to master the syntax and rules, 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.