Get highlighted color and remove them - Google Docs

975 views
Skip to first unread message

Irfan Khan

unread,
Aug 4, 2022, 6:30:33 AM8/4/22
to Google Apps Script Community
Hi,

I've big google doc file with over 100 pages and there are some reference text in that document on multiple location that reference text in highlighted with color "grey", I want to have a function which can find those color in table or paragraph and delete it. So Step 1 is finding it, and than deleting it in one go.

Can you guide me how i can achieve this please,

Thanks

Ed Bacher

unread,
Aug 4, 2022, 3:44:16 PM8/4/22
to Google Apps Script Community
Unfortunately, this is not a simple operation -- it would be nice if you could just call a function to find and delete all highlighted sections of text. But, as far as I know, that doesn't exist (though I would be happy to be proven wrong on this).

You'll need to go through all the Text elements (in paragraphs, lists, tables, etc.) and look for text attributes that match your color "grey" (which you might need to specify by number). Then you'll need to figure out the boundaries (index in the Text element) where that attribute applies. Once you figure that out, you should be able to delete each highlighted range of text.

There's some information about Text attributes with an example of setting (not finding) highlighted sections here:

To get the locations of all attributes, you'll need to use https://developers.google.com/apps-script/reference/xml-service/element?hl=en#getattributes. Then you need to check each of the attributes to see if it is the one you want (the specific highlight color), and not some other attribute (bold, italic, font color, etc.).

Ed

Clark Lind

unread,
Aug 4, 2022, 9:18:04 PM8/4/22
to Google Apps Script Community
Is that the only highlight you have in the document? If so, the easiest solution may simply to highlight everything (ctrl+A) then click the highlight button and select none. This obviously won't work if you have other highlights. But eliminate the simplest first! :)

Ed Bacher

unread,
Aug 4, 2022, 9:44:33 PM8/4/22
to google-apps-sc...@googlegroups.com
Oh, I thought you wanted to delete the text, not just the highlighting.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/DKde505zXrA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ea6aa80c-a643-4d13-a519-c5ac8acbdf85n%40googlegroups.com.

Irfan Khan

unread,
Aug 4, 2022, 10:22:16 PM8/4/22
to google-apps-sc...@googlegroups.com
Yes those are only highlighted text, which we refer as reference for table, it's kind of big documents with lot of tables, so those text in grey color are reference for user, how to fill those table. 
We need to have an ability to actually add a menu button in that doc, and when user clicks it, should "remove"/"delete" those texts from the doc which are highlighted as grey. And yes, only those text has background color.
The reason we're looking for this is, we actually moved from Word to Doc, and word has thr similar functionality which we achieved through VBA, and same we want to implement through Apps Script.
Please guide in what way we can achieve this.

Thanks

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAJ8BFc8OUPOd7jM1957uV5USsq%3DdC6otAQ4PghXx%2Bvr_dHP%2BqA%40mail.gmail.com.

Clark Lind

unread,
Aug 5, 2022, 9:50:13 PM8/5/22
to Google Apps Script Community
This will go through the entire Doc and remove the grey from every paragraph. It worked on the one table I played with..  it actually just sets the highlight color to white.

function deleteHighlight() {
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFFFF';
  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  paragraphs.forEach( (para) => {
    para.setAttributes(highlightStyle)
  })
}

Clark Lind

unread,
Aug 5, 2022, 9:53:26 PM8/5/22
to Google Apps Script Community
With menu:

function onOpen(e) {
    DocumentApp.getUi()
        .createMenu('User Actions')
        .addItem('Remove Highlights', 'deleteHighlight')
        .addToUi();
}


function deleteHighlight() {
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFFFF';
  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  paragraphs.forEach( (para) => {
    para.setAttributes(highlightStyle)
  })
}

Irfan Khan

unread,
Aug 6, 2022, 12:39:37 AM8/6/22
to google-apps-sc...@googlegroups.com
Hi, Thanks for sample code.

One more question is there any way I can just search for those texts which are highlighted in grey color? Actually i need to remove them from document, instead of making them white or remove background color

Thanks

Clark Lind

unread,
Aug 6, 2022, 8:19:32 AM8/6/22
to Google Apps Script Community
Now you are into the area Ed was talking about and this becomes much more difficult since you have to essentially text every single character or piece of text for the background color and then remove it.

It is doable, and probably would really take that much time to run except on really large documents. If I have time today, I may try playing with this-- I like challenges :)  this is probably the most tedious task, but probably the one most needing automation to remove the tedium! lol

Ed Bacher

unread,
Aug 6, 2022, 9:17:40 AM8/6/22
to Google Apps Script Community
I like a challenge too :), but I'm not sure I'll have time right now. However, it may be helpful to check out some of this code from my Docs to Markdown add-on: https://github.com/evbacher/gd2md-html/blob/07e536bf5d0b7b7e39f7aebe5ae79634ed311533/addon/gdc.gs#L707. I don't look for highlights here, but you could add a bit to look for the text background color attribute (BACKGROUND_COLOR). Then you can figure out which color it is (getBackground Color). The details may be messy though.

Ed

Clark Lind

unread,
Aug 6, 2022, 11:22:38 AM8/6/22
to Google Apps Script Community
Thanks Ed, that should give me some ideas.

Irfan Khan

unread,
Aug 6, 2022, 12:31:00 PM8/6/22
to google-apps-sc...@googlegroups.com
Thanks guys for all the help, i am also trying from my end too.
How we did it in MS Word is, we created custom styles and assign those styles to those "Remarks Text"(in grey) and in VBA we look for text matching the style name, and if it returns true than we delete those texts. As much i know about doc, there is no option to create custom styles.

Clark Lind

unread,
Aug 6, 2022, 1:51:32 PM8/6/22
to Google Apps Script Community
I'm coming up stumped and getting frustrated.. lol     I did find this about setting custom headings...  https://webapps.stackexchange.com/questions/113920/using-script-to-change-the-look-of-built-in-paragraph-styles-in-google-docs?rq=1

Clark Lind

unread,
Aug 6, 2022, 2:12:53 PM8/6/22
to Google Apps Script Community
Disregard the second sentence... for Excel only.

Clark Lind

unread,
Aug 6, 2022, 2:20:02 PM8/6/22
to Google Apps Script Community
Another quick and easy workaround...  (I don't know how to write a script for this)...   click/select a sample of the greyed notes.  Right-click, and select Format Options --> Select all matching text. Press Delete.  :)

Irfan Khan

unread,
Aug 6, 2022, 2:45:12 PM8/6/22
to google-apps-sc...@googlegroups.com
Ya i also thought of using the convertor, but i guess google was more concerned about Excel first :))

Ed Bacher

unread,
Aug 6, 2022, 2:46:25 PM8/6/22
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages