1

Following this code, I am using Google Apps Script to append a document template to another document. The template has bullets lists, however these will be lost upon copy, while indentation will be retained correctly.

Link to the template

Code:

     var newDoc = DocumentApp.openById('anotherGoogleID');
      var newDocBody = newDoc.getBody();
      var templateBody = DocumentApp.openById('aGoogleID').getActiveSection();
 // has bullets
          var totalElements = templateBody.getNumChildren();
          newDocBody.appendPageBreak();
          for( var j = 0; j < totalElements; ++j ) {
            var element = otherBody.getChild(j).copy();
            var type = element.getType();
            if( type == DocumentApp.ElementType.PARAGRAPH )
              newDocBody.appendParagraph(element);
            else if( type == DocumentApp.ElementType.TABLE )
              newDocBody.appendTable(element);
            else if( type == DocumentApp.ElementType.LIST_ITEM )
              newDocBody.appendListItem(element);
            else
              throw new Error("Unknown element type: "+type);
          }
    newDocBody.saveAndClose()
Riccardo
  • 2,054
  • 6
  • 33
  • 51
  • Can you provide a sample Document for replicating your issue? Because I thought that your document might use the types which are not included in [Enum GlyphType](https://developers.google.com/apps-script/reference/document/glyph-type). At first, I would like to confirm this. And if your script is not the latest one, please update it. – Tanaike Apr 25 '19 at 00:30
  • @Tanaike I have provided a link in the OP – Riccardo Apr 25 '19 at 09:53
  • Got this [link] (https://stackoverflow.com/questions/24787504/apps-script-appendlistitem-with-correct-glyphtype) seems there could be some troubles... I have tweaked the code `else if( type == DocumentApp.ElementType.LIST_ITEM ){ body.appendListItem(element); var glyphType = element.getGlyphType(); element.setGlyphType(glyphType); }` and now I get some round bullets... better than nothing but not the same as the template – Riccardo Apr 25 '19 at 10:50
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Apr 25 '19 at 12:09
  • But the copied bullets are not the same as the original .... Any idea? – Riccardo Apr 25 '19 at 14:08
  • Thank you for replying. Can you provide your latest script for replicating your issue? I would like to confirm about the issue. If you can do, please add it to your question. Even if you cannot do, don't worry. – Tanaike Apr 25 '19 at 22:21
  • Thank you for adding the script. Unfortunately, although I ran your additional script to your shared Document, I couldn't replicate your situation. I apologize for this situation. Was your issue obtained from your shared Document? – Tanaike Apr 26 '19 at 23:33

3 Answers3

4

I found I could get it to work with

newDocBody.appendListItem(listItem.getText()).setAttributes(element.getAttributes()); 

To put it in context: (based of https://www.labnol.org/code/19892-merge-multiple-google-documents)

for( var j = 0; j < totalElements; ++j ) {
    var element = otherBody.getChild(j).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH )
      body.appendParagraph(element).setAttributes(element.getAttributes());             
    else if( type == DocumentApp.ElementType.TABLE )
      body.appendTable(element).setAttributes(element.getAttributes());
    else if( type == DocumentApp.ElementType.LIST_ITEM ){ 
      var listItem = element.asListItem();
    body.appendListItem(listItem.getText()).setAttributes(element.getAttributes());            
}
David
  • 710
  • 5
  • 15
0

Following this sample, I have modified the code this way:

var newDoc = DocumentApp.openById('anotherGoogleID');
      var newDocBody = newDoc.getBody();
      var templateBody = DocumentApp.openById('aGoogleID').getActiveSection();
 // has bullets
          var totalElements = templateBody.getNumChildren();
          newDocBody.appendPageBreak();
          for( var j = 0; j < totalElements; ++j ) {
            var element = otherBody.getChild(j).copy();
            var type = element.getType();
            if( type == DocumentApp.ElementType.PARAGRAPH )
              newDocBody.appendParagraph(element);
            else if( type == DocumentApp.ElementType.TABLE )
              newDocBody.appendTable(element);
            else if( type == DocumentApp.ElementType.LIST_ITEM )
               { newDocBody.appendListItem(element); 
                 var glyphType = element.getGlyphType(); 
                 element.setGlyphType(glyphType); 
                 }
            else
              throw new Error("Unknown element type: "+type);
          }
    newDocBody.saveAndClose()

Now some bullets are being created, but circles bullets instead of letters....

Riccardo
  • 2,054
  • 6
  • 33
  • 51
0

It looks to me like the glyphType gets lost when calling appendListItem. I managed to restore it by storing the type in a variable before that call. This works for me:

if (type == DocumentApp.ElementType.PARAGRAPH) mainBody.appendParagraph(element);
else if (type == DocumentApp.ElementType.TABLE) mainBody.appendTable(element);
else if (type == DocumentApp.ElementType.LIST_ITEM) {
  var glyphType = element.getGlyphType();
  mainBody.appendListItem(element);
  element.setGlyphType(glyphType)
} else throw new Error('Unknown element type: ' + type);
i4h
  • 851
  • 7
  • 8