How to set first line of paragraph HEADING2 and rest normal text

烂漫一生 提交于 2021-02-08 11:21:59

问题


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.


回答1:


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());         

  } 


来源:https://stackoverflow.com/questions/17424563/how-to-set-first-line-of-paragraph-heading2-and-rest-normal-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!