theforestecologist
theforestecologist

Reputation: 4917

Adding formatted text to a Google Doc

I have a Google Sheet with form responses. I created some code so that each time a form is completed, the results populate a Google Doc. This works fine.

However, I want to add text to my Google Doc as well, which I accomplish using:

function myFunction(e) {
  var doc = DocumentApp.create('File');
  var text = 'insert text here';
  body.appendParagraph(text);
  doc.saveAndClose();
}

This text only is added as plain text, however, and I'd like to format this text. Specifically, I'd like to add the text so that it's bolded, underlined, and center-aligned in the document body.

How do I do this?

After some internet searching and SO searching, I tried adding html (e.g., <b> </b>) and I tried text.setBold(true). These approaches did not work.

I'll admit that I know very little about coding in Google Script editor, so I'm not at all sure how to go about this. I'm lucky enough that I got all my form responses to populate a named Google Doc file!

Upvotes: 0

Views: 1580

Answers (2)

Julie McMurry
Julie McMurry

Reputation: 159

The previous response got me on the right path (Thanks!). Google hasn't documented this feature very well, and some of the features that should work do not. I had found the info on formatting using offsets but that is incredibly tedious and verbose. Here's the full example method that works to insert pre-formatted text into a Google Doc. Hope it helps someone else. Obviously the 2D array can instead be wired up to be fetched from a form, but this will at least show how the rest of the formatting works.

      // Open a document by ID.
  var body = DocumentApp.openById('YourDocId').getBody();
  
  var authorAffils = [['Tony Tiger',1],['Micky Mouse',2],['Daffy Duck',3],['Elmo Orange',4]];
  
  var nameStyle={};
  nameStyle[DocumentApp.Attribute.FONT_SIZE]=11;
//  nameStyle[DocumentApp.TextAlignment.Normal]; // This seems to do nothing
  nameStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
  
  var affilStyle={};
  affilStyle[DocumentApp.Attribute.FONT_SIZE]=11;
//  affilStyle[DocumentApp.TextAlignment.SUPERSCRIPT];  // This seems to do nothing
  affilStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#cc0000';
      
  for(var i=0;i<authorAffils.length;i++){
    var par1=body.appendParagraph(Utilities.formatString(' %s,',authorAffils[i][0])).setAttributes(nameStyle);
    var par2=body.appendParagraph(Utilities.formatString('%s',authorAffils[i][1])).setAttributes(affilStyle);
    // I am not entirely clear why alignment only works this way whereas font size and color work the other way.
    par1.setTextAlignment(DocumentApp.TextAlignment.NORMAL).merge();
    par2.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT).merge();
  }

Upvotes: 1

Cooper
Cooper

Reputation: 64062

Here's a fragment of a document that I created recently:

    var nameStyle={};
    nameStyle[DocumentApp.Attribute.FONT_SIZE]=8;
    nameStyle[DocumentApp.Attribute.BOLD]=true;
    nameStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    var valStyle={};
    valStyle[DocumentApp.Attribute.FONT_SIZE]=12;
    valStyle[DocumentApp.Attribute.BOLD]=false;
    valStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#cc0000';
    body.appendParagraph('Basic Project Data').setAttributes(hdg1Style);
    var p1=body.appendParagraph(Utilities.formatString('%s: ','PID')).setAttributes(nameStyle);
    var p2=body.appendParagraph(Utilities.formatString('%s',selObj.pid)).setAttributes(valStyle);
    p2.merge();
    for(var i=0;i<basicDataA.length;i++){
      var par1=body.appendParagraph(Utilities.formatString('%s: ',basicDataA[i][0])).setAttributes(nameStyle);
      var par2=body.appendParagraph(Utilities.formatString('%s',basicDataA[i][1])).setAttributes(valStyle);
      par2.merge();
    }

Note, the appendParagraph first and then setAttributes.

Upvotes: 3

Related Questions