📜  使用脚本查找和格式化谷歌文档中的特定文本 - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:50.445000             🧑  作者: Mango

代码示例1
// Set the whole body to Roboto 10
  var FontStyle = {};
  FontStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Roboto';
  FontStyle[DocumentApp.Attribute.FONT_SIZE] = 10;

  body.setAttributes(FontStyle);

  // Set some elements to bold
  var target1 = "Private & Confidential"
  var searchResult1 = body.findText(target1);
  if (searchResult1 !== null) {
    var thisElement1 = searchResult1.getElement();
    var thisElement1Text = thisElement1.asText();
    thisElement1Text.setBold(searchResult1.getStartOffset(), searchResult1.getEndOffsetInclusive(), true);
  }

  var target2 = "Your Reference:"
  var searchResult2 = body.findText(target2);
  if (searchResult2 !== null) {
    var thisElement2 = searchResult2.getElement();
    var thisElement2Text = thisElement2.asText();
    thisElement2Text.setBold(searchResult2.getStartOffset(), searchResult2.getEndOffsetInclusive(), true);
  }

  var target3 = "Our Reference:"
  var searchResult3 = body.findText(target3);
  if (searchResult3 !== null) {
    var thisElement3 = searchResult3.getElement();
    var thisElement3Text = thisElement3.asText();
    thisElement3Text.setBold(searchResult3.getStartOffset(), searchResult3.getEndOffsetInclusive(), true);
  }

  // Right align date
  var searchType = DocumentApp.ElementType.PARAGRAPH
  var target4 = "\\[Date\\]";
  var searchResult4 = body.findText(target4);

  while (searchResult4 = body.findElement(searchType, searchResult4)) {
   var par = searchResult4.getElement().asParagraph();
   if (searchResult4 != null) {
     par.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
  }
  }
  //  Update date
  var date = Utilities.formatDate(new Date(), "GMT", "d MMMMM yyyy");
  body.replaceText("\\[Date\\]", date);
 }