2

I have made some apps script to format text in a Google Doc, but:

  1. I can't figure out how to stop the script breaking when the text isn't found.
  2. The script doesn't look very efficient.

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

Can anyone help?

P

2 Answers 2

2

Except for one bug, the correct code is below. The bug is that I still can't get the date to right align. Any ideas?

// 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);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

var target4 = "[Date]"; seems to solve your problem. I just tested successfully, so I edited your answer.
1

I am assuming your script is working when patterns is found and breaking only when patterns are not found. So here is my suggested way around :

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

This is an un-tested code, let me know if any issue arises, I'll be happy to assists us.

Thanks.

2 Comments

Ah, thanks! I think it's working, although I realise I have a different problem: "bold" and "right align" is being applied to the whole letter, not just to (eg) "Private and Confidential" and "[Date]". How do I limit the formatting to just the patterns?
Maybe you can try this ,found.getElement().getParent().setAttributes(style); stackoverflow.com/questions/46675577/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.