2

How can I automate find & replace, specifically to colour all suit symbols document wide?

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

Find all "♣" and color them green. Find all "♦" and color them orange. Find all "♥" and color them red. Find all "♠" and color them blue.

I would be very grateful for the script! Thanks.

Ps: To get the suit symbols press:

  • LEFTALT + NUMPAD3 ( ♥ )
  • LEFTALT + NUMPAD4 ( ♦ )
  • LEFTALT + NUMPAD5 ( ♣ )
  • LEFTALT + NUMPAD6 ( ♠ )

Thanks User0 for that tip. If anyone wants a macro to do what I'm asking in Word, I can send it to you.

  • 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
1

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

  • Thanks Mark. I'm not familiar with GAS, but I'm keen so I'll see if I can work it all out based on what you've given me. – J M N Aug 30 '18 at 3:09
  • 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
0

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

In your doc, write:

c = (["♣", {color: "#00FF00", fontSize: 16}])
d = (["♦",  {color: "#FF8800", fontSize: 16}])
h = (["♥", {color: "#FF0000", fontSize: 16}])
s = (["♠", {color: "#0009FF", fontSize: 16}])

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

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

rendered content

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

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

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.