JMNewman

Google Apps Script to colour card suit symbols

Nov 3rd, 2019
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function onOpen() {
  2. DocumentApp.getUi()
  3. .createMenu('Utilities')
  4. .addItem('Auto-Replace', 'replaceSuits')
  5. .addToUi();
  6. };
  7.  
  8. function replaceSuits() {
  9. var body = DocumentApp.getActiveDocument().getBody();
  10. var text = body.editAsText();
  11.  
  12.  
  13. var found = body.findText("♥");
  14. while (found) {
  15. var elem = found.getElement();
  16. if (found.isPartial()) {
  17. var start = found.getStartOffset();
  18. var end = found.getEndOffsetInclusive();
  19. elem.setForegroundColor(start, end, "#ff0000");
  20. }
  21. else {
  22. elem.setForegroundColor("#ff0000");
  23. }
  24. found = body.findText("♥", found);
  25. }
  26.  
  27. found = body.findText("♦");
  28. while (found) {
  29. var elem = found.getElement();
  30. if (found.isPartial()) {
  31. var start = found.getStartOffset();
  32. var end = found.getEndOffsetInclusive();
  33. elem.setForegroundColor(start, end, "#ff8100");
  34. }
  35. else {
  36. elem.setForegroundColor("##ff8100");
  37. }
  38. found = body.findText("♦", found);
  39. }
  40.  
  41. found = body.findText("♣");
  42. while (found) {
  43. var elem = found.getElement();
  44. if (found.isPartial()) {
  45. var start = found.getStartOffset();
  46. var end = found.getEndOffsetInclusive();
  47. elem.setForegroundColor(start, end, "#00b700");
  48. }
  49. else {
  50. elem.setForegroundColor("#00b700");
  51. }
  52. found = body.findText("♣", found);
  53. }
  54.  
  55. found = body.findText("♠");
  56. while (found) {
  57. var elem = found.getElement();
  58. if (found.isPartial()) {
  59. var start = found.getStartOffset();
  60. var end = found.getEndOffsetInclusive();
  61. elem.setForegroundColor(start, end, "#0000ff");
  62. }
  63. else {
  64. elem.setForegroundColor("#0000ff");
  65. }
  66. found = body.findText("♠", found);
  67. }
  68. };
Add Comment
Please, Sign In to add comment