0

我正在使用 Google Apps 脚本遍历我的联系人并将姓名、地址、电子邮件和电话号码打印到文档中。

我想要样式为“标题 2”的名称,其余样式为“普通文本”。

我怎样才能做到这一点?

这是我到目前为止所得到的,但它使整个段落成为标题 2,而不仅仅是名称。

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

}

或者,如果有更好的方法来做我想要的,请提出建议。

4

1 回答 1

0

您最初在整个段落中设置样式,然后添加回文本。

您可以创建 2 个部分:对于 fullName

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

以及内容样式正常的新段落

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

结果

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

  } 
于 2013-07-02T13:00:43.643 回答