0

I'm using Google Apps Script to iterate through my contacts and print name, address, email and phone numbers to a document.

I want the name in style "Heading 2" and the rest in style "Normal text".

How can I do this?

Here's what I've got so far but it makes the whole paragraph Heading 2, instead of just the name.

var myContacts = ContactsApp.findContactGroup('Some group').getContacts();

for (i=0; i < myContacts.length; i++)
{    

  var fullName = myContacts[i].getFullName();
  if (fullName == '')
    fullName = 'Anonymous';

  var contactPara = doc.appendParagraph(fullName);
  contactPara.setHeading(DocumentApp.ParagraphHeading.HEADING2);    

  var homeAddresses = myContacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);

  var homeAddress = '';
  if (homeAddresses.length > 0)
    contactPara.appendText('\n' + homeAddresses[0].getAddress());

  contactPara.appendText('\n' + 'Email: ' + myContacts[i].getPrimaryEmail());

  var mobilePhones = myContacts[i].getPhones(ContactsApp.Field.MOBILE_PHONE);    
  if (mobilePhones.length > 0)
    contactPara.appendText('\n' + 'Mobile phone: ' + mobilePhones[0].getPhoneNumber());        

  var homePhones = myContacts[i].getPhones(ContactsApp.Field.HOME_PHONE);    
  if (homePhones.length > 0)
    contactPara.appendText('\n' + 'Home phone: ' + homePhones[0].getPhoneNumber());            

  var workPhones = myContacts[i].getPhones(ContactsApp.Field.WORK_PHONE);    
  if (workPhones.length > 0)
    contactPara.appendText('\n' + 'Work phone: ' + workPhones[0].getPhoneNumber());         

}

Alternatively, if there's a better way to do what I want them please suggest it.

cja
  • 9,512
  • 21
  • 75
  • 129

1 Answers1

0

You initially set the style throughout the paragraph, and then add back the text.

You can create 2 section: for the fullName

var headPara = doc.appendParagraph(fullName);
headPara.setHeading(DocumentApp.ParagraphHeading.HEADING2); 

and new paragraph with normal style for content

var contactPara = doc.appendParagraph(' ').setHeading(DocumentApp.ParagraphHeading.NORMAL);

Result

  var myContacts = ContactsApp.getContacts();

  for (i=0; i < myContacts.length; i++)
  {    

    var fullName = myContacts[i].getFullName();
    if (fullName == '')
      fullName = 'Anonymous';

    var headPara = doc.appendParagraph(fullName);
    headPara.setHeading(DocumentApp.ParagraphHeading.HEADING2);    

    var contactPara = doc.appendParagraph(' ').setHeading(DocumentApp.ParagraphHeading.NORMAL);
    var homeAddresses = myContacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);

    var homeAddress = '';
    if (homeAddresses.length > 0)
      contactPara.appendText('\n' + homeAddresses[0].getAddress());

    contactPara.appendText('\n' + 'Email: ' + myContacts[i].getPrimaryEmail());

    var mobilePhones = myContacts[i].getPhones(ContactsApp.Field.MOBILE_PHONE);    
    if (mobilePhones.length > 0)
      contactPara.appendText('\n' + 'Mobile phone: ' + mobilePhones[0].getPhoneNumber());        

    var homePhones = myContacts[i].getPhones(ContactsApp.Field.HOME_PHONE);    
    if (homePhones.length > 0)
      contactPara.appendText('\n' + 'Home phone: ' + homePhones[0].getPhoneNumber());            

    var workPhones = myContacts[i].getPhones(ContactsApp.Field.WORK_PHONE);    
    if (workPhones.length > 0)
      contactPara.appendText('\n' + 'Work phone: ' + workPhones[0].getPhoneNumber());         

  } 
Rudolf Manusachi
  • 2,311
  • 2
  • 19
  • 25
  • For some reason I thought it wouldn't look good as two separate paragraphs. But it's ok. Thanks. – cja Jul 02 '13 at 13:21
  • @cja If you want to have everything in one paragraph You can after each appendText() to add the appropriate style. Example: `contactPara.appendText(fullName).setBold(true).setFontSize(20); ... contactPara.appendText(email).setBold(false).setFontSize(10);` – Rudolf Manusachi Jul 02 '13 at 14:11
  • Thanks, but I want to use the defined "Normal text" style to allow for configuring styles in the document rather than in code. – cja Jul 02 '13 at 15:37