3

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Makonnen
  • 495
  • 2
  • 6
  • 8

1 Answers1

3

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);
  }
}
Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks, its working! Any ideas how to copy text, formatting and images within a document and copy it to the second page of the doc?? Also what does the [0] in "docIDs[0]" do? – Makonnen Jan 11 '13 at 06:45
  • Thx, I added some comments to explain, this is a part of a bigger script and I left some parts that are not related to the question... sorry bout that. Now I hope it is more clear. As for your oher question I'll try to update my answer later in the day ;-) no time now... – Serge insas Jan 11 '13 at 07:04