4

How can I automate a find & replace function to change the font colour of all suit symbols document wide?

I write bridge articles (card game), and I need to automate this formatting constantly:

Find all "♣" and color the text green (#00b700). Find all "♦" and color the text orange (#ff8100). Find all "♥" and color the text red (#ff0000). Find all "♠" and color the text blue (#0000ff).

Answered: Mark and Michael helped build Google Apps Scripts to do this (see below). I modified the script and it's working perfectly.

  • smarter way is to press LEFTALT+3 (♥), LEFTALT+4 (♦), LEFTALT+5 (♣), LEFTALT+6 (♠) – user0 Jul 29 '18 at 8:11
  • See Extending Docs – Rubén Jul 31 '18 at 13:00
3

To do a find-and-replace with formatting, you will need to use Google Apps Script. I am going to assume you are already familiar with GAS, but if not please reply with a comment on this post.

This script provides an example where you can change the background color of all 4 suits to the same color, by adding a menu to your document that will apply the formatting to all instances. Here is what the new menu will look like after adding the script to your Google Doc:

Utilities > Auto-Replace menu next to the "Help" menu in the Google Docs toolbar

And here is the code:

function onOpen() {
  DocumentApp.getUi()
      .createMenu('Utilities')
      .addItem('Auto-Replace', 'replaceSuits')
      .addToUi();
};

function replaceSuits() {
  var body = DocumentApp.getActiveDocument().getBody();      

  var found = body.findText("(♥|♦|♣|♠)");
  while (found) {
    var elem = found.getElement();
    if (found.isPartial()) {
      var start = found.getStartOffset();
      var end = found.getEndOffsetInclusive();
      elem.setBackgroundColor(start, end, "#ff0000");

    }
    else {
      elem.setBackgroundColor("#ff0000");
    }
    found = body.findText("(♥|♦|♣|♠)", found);
  }
};

This question is similar to Formatting in replaceText(), from which I borrowed code.

Here is a longer example which highlights each suit differently as described in your question (uses 4 separate code blocks for each suit, could definitely be refactored to allow shared code but this was the easiest way for me to code it): https://pastebin.com/7Y1L6BqF

| improve this answer | |
  • Here is a super brief rundown of how to get this to work: (1) In your document, go to Tools > Script Editor (2) Replace what's currently there (a blank function) with the code above (or the code from the pastebin) (3) Save it (name doesn't matter) (4) Refresh your Google Doc window, you should now have a "Utilities" menu as shown above (5) Choose the "Auto-replace" function. The first time you do this you will need to authorize the script. Let me know if you have any further questions! – Mark Aug 30 '18 at 12:37
  • 1
    Thank you so much @Mark. I think I'm 99% of the way there. It's actually the text colour (not highlighting), that I'm after. I changed setBackgroundColor to setFontColor, but I'm getting this error: "TypeError: Cannot find function setFontColor in object Text." Any ideas? Thanks. A link to my script. – J M N Oct 8 '19 at 3:55
  • @JMN It's named "setForegroundColor". The extension I created in the other post maps both {color: X} and {foregroundColor: X} to setForegroundColor(X). The cheat sheet available from the extension references the settable properties: developers.google.com/apps-script/reference/document/text – Mikaël Mayer Oct 15 '19 at 14:11
  • 2
    Thanks Mark & Michael, here is the script, working 100%: pastebin.com/3kZBVz2F – J M N Nov 4 '19 at 2:03
1

Use the Reversible Formulas add-on for your next article.

In your doc, write for example:

c = (["♣", {color: "#00B700"}])
d = (["♦", {color: "#FF8100"}])
s = (["♠", {color: "#0000FF"}])
h = (["♥", {color: "#FF0000"}])

What can you do with =c =d =s and =h?

One =h, with three =d =d =d vs. two =s =s and two =c =c.

Click on "Display values", you'll obtain:

Rendered version of the code above

To hide the definitions from the document, click on "Extract defs", they will be placed in a separated code box above.

Interface of Reversible Formulas - Sketch-n-Script

If you don't remember which letter is which symbol, you can always click on one of the buttons on the sidebar to insert the code for a symbol. Just click on "Display" value so that it's replaced by the symbol itself.

The add-on can be found there: https://chrome.google.com/webstore/detail/formulas-for-google-docs/kgjbgmonlnhglokmfnpnklegakfknbeh

Disclaimer: I'm the author of this add-on

| improve this answer | |
  • Thanks Mikael, the colours I'm after are green (#00b700), orange (#ff8100), red (#ff0000) and blue (#0000ff). I don't want to specify font size, because the symbols appear in headings, small text, etc. – J M N Oct 8 '19 at 4:18
  • 1
    I updated the script to remove the font size and changed the colors. See how well it seamlessly integrates with titles. – Mikaël Mayer Oct 15 '19 at 14:05
  • Thanks for your help. Solved: pastebin.com/3kZBVz2F – J M N Nov 4 '19 at 2:01
  • That's a good way to solve your problem too. – Mikaël Mayer Nov 4 '19 at 10:40
  • That's awesome @MikaëlMayer! Do you accept push requests for your add-on? – Mark Nov 5 '19 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.