如何将文档中特定单词的首字母大写?

时间:2017-07-10 13:16:36

标签: javascript google-apps-script google-docs

我正在修改Google-apps脚本,以便从Google日历活动中创建会议纪要(谷歌文档)。出于原因,我不必导入参与者的名字,而是导入他们的电子邮件并修改文本以使其显示为名称。这是结果的链接(无法发布图片):Both the first name and last name are completely in lowercase我不知道如何将名字和姓氏的第一个字母大写。以下是我的代码的相关部分:

function createMeetingNotesNextTimePeriod() {

    // variables for text format
    var emailString = '@companyname';
    var deleteString = '';
    var repEmail = 'test';
    var dot = '\[.]';
    var spaceString = ' ';
    var repDot = 'test2'; 

    // retrieve all calendar events for time period with @notes string
    var events = CalendarApp.getDefaultCalendar().getEvents(now, period_from_now, {search: '@notes'}) ; 
    // loop through each event an get meeting attributes, 
    for (var i=0;i<events.length;i++) {
      var title = events[i].getTitle();
      var eventstart = events[i].getStartTime();
      var eventend = events[i].getEndTime();
      var location = events[i].getLocation();
      var owner = events[i].getCreators();
      var ordredujour = events[i].getDescription();
      var guestlist = events[i].getGuestList();

      // create a google doc with the meeting name as the title       
      var doc = DocumentApp.create(title);

      //write the description of the meeting 
      doc.getBody().editAsText()
      .insertText(0, '\n' + 'Ordre du jour:  ' + ordredujour + '\n\n');               

      //writes the email adresses of the guests
      for (var j=0;j<guestlist.length;j++) {
      var name = guestlist[j].getEmail();
      var guestStatus = guestlist[j].getGuestStatus();
      doc.getBody().editAsText()
      .insertText(0, name + ': ' + guestStatus +'\n')
      }

      //Write Guests
      doc.getBody().editAsText()
      .insertText(0, 'Invités:\n');

      //Write the name of the organiser 
      doc.getBody().editAsText()
      .insertText(0, 'Organisateur:  ' + owner + '\n\n' );

      //writes the location of the meeting
      doc.getBody().editAsText()
      .insertText(0, 'Location:  ' + location + '\n\n');

      //writes the meeting ending time
      doc.getBody().editAsText()
      .insertText(0, 'Fin:  ' + eventend + '\n\n');

      //writes the meeting starting time
      doc.getBody().editAsText()
      .insertText(0, 'Début:  ' + eventstart + '\n');

      //writes the title of the meeting
      doc.getBody().editAsText()
      .insertText(0, '\n\n' + title + '\n\n')
      .setFontSize(0, title.length+1, 14)
      .setBold(0, title.length+1, true)
      .setForegroundColor(0, title.length+1, '#cc0000');

      //replacement of "@companyname.com" and "." 
      Logger.log(repEmail.replace(emailString, deleteString)); 
      Logger.log(repEmail.replace(new RegExp(emailString), deleteString)); 
      doc.replaceText(emailString,deleteString);
      Logger.log(doc.getText());

      Logger.log(repDot.replace(dot, spaceString)); 
      Logger.log(repDot.replace(new RegExp(dot), spaceString)); 
      doc.replaceText(dot,spaceString);

    }  // for loop for each event 

}  // functionCreateMeetingNotesNextTimePeriod

1 个答案:

答案 0 :(得分:1)

请尝试下面的代码

doc.getBody().editAsText()
      .insertText(0, name.split(" ")[0].charAt(0).toUpperCase() + name.split(" ")[0].slice(1) + ' ' + name.split(" ")[1].charAt(0).toUpperCase() + name.split(" ")[1].slice(1)  + ': ' + guestStatus +'\n')
      }

而不是

 doc.getBody().editAsText()
      .insertText(0, name + ': ' + guestStatus +'\n')
      }

它可能适合你。