Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 使用应用程序脚本格式化文本(谷歌文档)_Google Apps Script_Google Docs - Fatal编程技术网

Google apps script 使用应用程序脚本格式化文本(谷歌文档)

Google apps script 使用应用程序脚本格式化文本(谷歌文档),google-apps-script,google-docs,Google Apps Script,Google Docs,我制作了一些应用程序脚本来格式化谷歌文档中的文本,但是: 我不知道如何在找不到文本时阻止脚本中断 这个脚本看起来效率不高 // Set the whole body to Roboto 10 var FontStyle = {}; FontStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Roboto'; FontStyle[DocumentApp.Attribute.FONT_SIZE] = 10; body.setAttributes(FontStyl

我制作了一些应用程序脚本来格式化谷歌文档中的文本,但是:

  • 我不知道如何在找不到文本时阻止脚本中断
  • 这个脚本看起来效率不高

    // 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 BoldStyle = {};
    BoldStyle[DocumentApp.Attribute.BOLD] = true;
    
    var pattern1 = "Private & Confidential";
    var found1 = body.findText(pattern1);
    found1.getElement().setAttributes(BoldStyle)
    
    var pattern2 = "Your Reference:";
    var found2 = body.findText(pattern2);
    found2.getElement().setAttributes(BoldStyle)
    
    var pattern3 = "Our Reference:";
    var found3 = body.findText(pattern3);
    found3.getElement().setAttributes(BoldStyle)
    
    // Set some elements to right align
    var RightStyle = {};
    RightStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT ] = 
    DocumentApp.HorizontalAlignment.RIGHT;
    
    var pattern4 = "\\[Date\\]";
    var found4 = body.findText(pattern4);
    found4.getElement().getParent().setAttributes(RightStyle);
    
  • 有人能帮忙吗


    p

    我假设您的脚本在找到模式时工作,只有在找不到模式时才会中断。 下面是我建议的解决方法:

    // 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 BoldStyle = {};
    BoldStyle[DocumentApp.Attribute.BOLD] = true;
    
    var pattern1 = "Private & Confidential";
    var found1 = body.findText(pattern1);
    if (found1 != null) {
        found1.getElement().setAttributes(BoldStyle)
    }
    
    var pattern2 = "Your Reference:";
    var found2 = body.findText(pattern2);
    if (found2 != null) {
        found2.getElement().setAttributes(BoldStyle)
    }
    
    var pattern3 = "Our Reference:";
    var found3 = body.findText(pattern3);
    if (found3 != null) {
        found3.getElement().setAttributes(BoldStyle)
    }
    
    // Set some elements to right align
    var RightStyle = {};
    RightStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
        DocumentApp.HorizontalAlignment.RIGHT;
    
    var pattern4 = "\\[Date\\]";
    var found4 = body.findText(pattern4);
    if (found4 != null) {
        found4.getElement().getParent().setAttributes(RightStyle);
    }
    
    这是一个未经测试的代码,如果出现任何问题,请告诉我,我很乐意为我们提供帮助


    谢谢。

    除了一个错误,下面是正确的代码。问题是我仍然无法将日期正确对齐。有什么想法吗

    // 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);
     }
    

    啊,谢谢!我认为这是有效的,尽管我意识到我有一个不同的问题:“粗体”和“右对齐”适用于整封信,而不仅仅是(例如)“私人和机密”和“[日期]”。如何将格式限制为仅包含模式?也许您可以试试这个,find.getElement().getParent().setAttributes(style)<代码>var target4=“[Date]”似乎可以解决您的问题。我刚刚测试成功,所以我编辑了你的答案。