1

I want to replace a word "allowance" with "Some text", after running the code, It will remove word allowance and apply "Some text" with same formatting as that of "allowance" but foreground color property is not getting set as that of original.I want Some text also in red color as shown in the screenshot

enter image description here

enter image description here

function retainFormatting() {
  var doc  = DocumentApp.getActiveDocument();
  var textToHighlight = 'allowance';
  var highlightStyle;
  var paras = doc.getParagraphs();
  var textLocation = {};
  var i;

  for (i=0; i<paras.length; ++i) {
    textLocation = paras[i].findText(textToHighlight);
    if (textLocation != null && textLocation.getStartOffset() != -1) {
      highlightStyle = textLocation.getElement().getAttributes(textLocation.getStartOffset());
      textLocation.getElement().deleteText(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive());
      textLocation.getElement().insertText(textLocation.getStartOffset(),"Some text");      
      textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);      
    }
  }
}

before setting attribute at offset

enter image description here

after setting attribute it turns out to be

enter image description here

user3436029
  • 75
  • 1
  • 2
  • 13

4 Answers4

1

getForegroundColor(offset)

Retrieves the foreground color at the specified character offset.

And

setForegroundColor(startOffset, endOffsetInclusive, color)

Sets the foreground color for the specified character range.

Here is a sample code :

Getting Color from text

highlightColor = textLocation.getElement().getForegroundColor(textLocation.getStartOffset());

Applying color to text

textLocation.getElement().setForegroundColor(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);

I hope it helps. Goodluck :)

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
0

Try

textLocation.getElement().editAsText().deleteText(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive());
textLocation.getElement().editAsText().insertText(textLocation.getStartOffset(),"Some text");

The .editAsText() puts you into editing the contents of the rich text leaving the existing attributes as a 'wrapper'

Alternatively, try replacing the text rather than deleting and inserting

paras[i].replaceText("allowance", "some text") // the first attribute is a regular expression as string
JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
  • Hi Jonathon, But adding **editAsText** doesn't work, help! – user3436029 Apr 30 '16 at 06:12
  • Try replacing instead of deleting and inserting. Answer edited. – JSDBroughton Apr 30 '16 at 06:37
  • Problem is focused on setting the attributes and not replacing text, Replacing text has no control over startOffset and endOffset. Anybody Please focus on setting the attributes and set right. – user3436029 Apr 30 '16 at 07:02
  • Those offsets are only defined by findText. The reason I was looking at alternatives was to investigate if the specific methods used contributed to the observed problem. i.e. Isolated text not having the attributes of the parent paragraph such that there are none can apply etc. If there is a way to achieve what you want which avoids such a restriction all the better. Have you checked the contents of the `highlightStyle` to see if FOREGROUND_COLOR is in there and red? – JSDBroughton Apr 30 '16 at 07:10
  • As you said, Yes, but replaceText will replace in a paragraph, I want to limit at specific position only governed by selection with some criteria. Hence I do not want to use replaceText, – user3436029 Apr 30 '16 at 07:18
  • I observed the contents of the object before setting and after setting but after setting attributes it turns to null – user3436029 Apr 30 '16 at 07:18
  • I have attached the contents after setting – user3436029 Apr 30 '16 at 07:20
0

I have just tested this and it seems that setting LINK_URL alongside other attributes interferes with FOREGROUND_COLOR.

The following results in a black text color:

      var attrs = {
        "FOREGROUND_COLOR": "#ff0000", // should be red
        "LINK_URL": null
      };
      text.setAttributes(start, end, attrs);

The following results in a red text color:

      var attrs = {
        "FOREGROUND_COLOR": "#ff0000" // should be red
      };
      text.setAttributes(start, end, attrs);

In effect, if you don't need to set the link, remove the LINK_URL from the list of formatting options.

0

@JSDBroughton Gave me an idea, which worked.
Try setting the attributes of the rich text object you get when calling editAsText. So instead of:

highlightStyle = textLocation.getElement().getAttributes(textLocation.getStartOffset());
textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);

Do:

// Make sure you replace `asParagraph` with what you actually need
highlightStyle = textLocation.getElement().asParagraph().editAsText().getAttributes(textLocation.getStartOffset());
textLocation.getElement().asParagraph().editAsText().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);

Edit: after playing around with this, seems like this only sometimes works. I still haven't figured out the pattern for when it does work and when it doesn't.

Yuval.R
  • 1,182
  • 4
  • 15