1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | /** * @OnlyCurrentDoc Limits the script to only accessing the current sheet. */ /** * A special function that runs when the spreadsheet is open, used to add a * custom menu to the spreadsheet. */ function onOpen() { SpreadsheetApp.getUi() .createAddonMenu() .addItem('Rainbow color selected cells', 'rainbowTextAllCells') .addToUi(); } function onInstall(e) { onOpen(e); } /** * Color the text in all selected cells in rainbow colors * * @author Christian Weiske <weiske@mogic.com> */ function rainbowTextAllCells() { var ranges = SpreadsheetApp.getActiveSheet().getActiveRangeList().getRanges(); var alltext = ""; for (var rangeId = 0; rangeId < ranges.length; rangeId++) { var richtexts = ranges[rangeId].getRichTextValues(); for (var rangeRow = 0; rangeRow < richtexts.length; rangeRow++) { for (var rangeCol = 0; rangeCol < richtexts[rangeRow].length; rangeCol++) { alltext += richtexts[rangeRow][rangeCol].getText(); } } } var steplength = 360 / alltext.length; var rainbowPos = 0; for (var rangeId = 0; rangeId < ranges.length; rangeId++) { var range = ranges[rangeId]; var richtexts = range.getRichTextValues(); for (var rangeRow = 0; rangeRow < richtexts.length; rangeRow++) { for (var rangeCol = 0; rangeCol < richtexts[rangeRow].length; rangeCol++) { var cellText = richtexts[rangeRow][rangeCol].getText(); var richtext = SpreadsheetApp.newRichTextValue().setText(cellText); for (var pos = 0; pos < cellText.length; pos++) { var color = hsl2rgb(rainbowPos * steplength, 100, 50); var colorCode = '#' + toHex(color.r) + toHex(color.g) + toHex(color.b) var style = SpreadsheetApp.newTextStyle().setForegroundColor(colorCode); richtext.setTextStyle(pos, pos + 1, style.build()); rainbowPos++; } range.getCell(rangeRow + 1, rangeCol + 1).setRichTextValue(richtext.build()); } } } } /** * Color the text in the currently active cell in rainbow colors * * @author Christian Weiske <weiske@mogic.com> */ function rainbowTextCurrentCell() { var cell = SpreadsheetApp.getCurrentCell(); var text = cell.getRichTextValue().getText() var richtext = SpreadsheetApp.newRichTextValue().setText(text); var steplength = 360 / text.length; for (var pos = 0; pos < text.length; pos++) { var color = hsl2rgb(pos * steplength, 100, 50); var colorCode = '#' + toHex(color.r) + toHex(color.g) + toHex(color.b) var style = SpreadsheetApp.newTextStyle().setForegroundColor(colorCode); richtext.setTextStyle(pos, pos + 1, style.build()); } cell.setRichTextValue(richtext.build()); } function toHex(num) { var str = num.toString(16); if (str.length < 2) { str = "0" + str; } return str; } // http://hsl2rgb.nichabi.com/javascript-function.php function hsl2rgb (h, s, l) { var r, g, b, m, c, x if (!isFinite(h)) h = 0 if (!isFinite(s)) s = 0 if (!isFinite(l)) l = 0 h /= 60 if (h < 0) h = 6 - (-h % 6) h %= 6 s = Math.max(0, Math.min(1, s / 100)) l = Math.max(0, Math.min(1, l / 100)) c = (1 - Math.abs((2 * l) - 1)) * s x = c * (1 - Math.abs((h % 2) - 1)) if (h < 1) { r = c g = x b = 0 } else if (h < 2) { r = x g = c b = 0 } else if (h < 3) { r = 0 g = c b = x } else if (h < 4) { r = 0 g = x b = c } else if (h < 5) { r = x g = 0 b = c } else { r = c g = 0 b = x } m = l - c / 2 r = Math.round((r + m) * 255) g = Math.round((g + m) * 255) b = Math.round((b + m) * 255) return { r: r, g: g, b: b } } |