-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
161 lines (148 loc) · 5.38 KB
/
Code.js
File metadata and controls
161 lines (148 loc) · 5.38 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @OnlyCurrentDoc
*
* The above comment directs Apps Script to limit the scope of file
* access for this add-on. It specifies that this add-on will only
* attempt to read or modify the files in which the add-on is used,
* and not all of the user's files. The authorization request message
* presented to users will reflect this limited scope.
*/
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
/**
* Runs when the add-on is installed.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall(e) {
onOpen(e);
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*/
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('controlpanel')
.setTitle('Rainbow Text');
DocumentApp.getUi().showSidebar(ui);
}
/**
* Hard coded rgb values for the rainbow spectrum. Can
* be replaced with any object array with the format:
* [{'r': [int value], 'g': [int value], 'b': [int value]}, ....]
**/
var colors = [
{r: 255, g: 0, b:0, name: 'red'},
{r: 255, g: 165, b:0, name: 'orange'},
{r: 255, g: 255, b: 0, name: 'yellow'},
{r: 0, g: 128, b: 0, name: 'green'},
{r: 0, g: 0, b: 255, name: 'blue'},
{r: 75, g: 0, b: 130, name: 'indigo'},
{r: 128, g: 0, b: 128, name: 'violet'}
]
/**
* generate a color spectrum with X steps between each hard-coded
* value in the colors array.
* @param {int} steps Number of colors between each hardcoded value in the colors array.
**/
function generateSpectrumArray (steps) {
var getHex = function (color) {
var decimal = (color.r * 65536) + (color.g * 256) + color.b;
var hex = decimal.toString(16);
while (hex.length < 6) hex = '0' + hex;
hex = '#' + hex;
return hex;
}
var hexColorsToReturn = [];
for (var i = 0; i < colors.length - 1; i++) {
var c1 = colors[i];
var c2 = colors[i+1];
hexColorsToReturn.push(getHex(c1));
for (var step = 1; step <= steps; step++ ){
var tempColor = {};
var stepFraction = 1 / (steps + 1);
tempColor.r = Math.floor(c1.r + ((c2.r - c1.r) * stepFraction * step));
tempColor.g = Math.floor(c1.g + ((c2.g - c1.g) * stepFraction * step));
tempColor.b = Math.floor(c1.b + ((c2.b - c1.b) * stepFraction * step));
hexColorsToReturn.push(getHex(tempColor));
}
}
hexColorsToReturn.push(getHex(colors[colors.length -1]));
return hexColorsToReturn;
}
/**
* Gets the full text of the document and changes it to rainbow colors
*
* @param {number} steps Number of colors between each hardcoded value in the colors array.
* @param {boolean} savePref Whether to automatically change text to rainbow on opening the add-on
* @return {boolean} Returns true upon success.
*/
function changeToRainbow (steps) {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.editAsText();
var spectrum = generateSpectrumArray(steps);
var spec_index = {i: 0, countUp: true};
for (var i = 0; i < text.getText().length; i++) {
text.setForegroundColor(i, i, spectrum[spec_index.i]);
if (spec_index.countUp) spec_index.i += 1;
else spec_index.i -=1;
if (spec_index.i == spectrum.length - 1) spec_index.countUp = false;
if (spec_index.i == 0) spec_index.countUp = true;
}
body.editAsText().setBackgroundColor('#ddddff')
if (!text.getText().length) throw new Error('Type something in the document.');
return true;
}
/**
* Gets the full text of the document and gives it the color black
*
* @param {boolean} savePref Whether to automatically change text to rainbow on opening the add-on
* @return {boolean} Returns true upon success.
*/
function changeToBlack() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.editAsText();
text.setForegroundColor(0, text.getText().length - 1, '#000000');
body.editAsText().setBackgroundColor('#ffffff');
return true;
}
/**
* @param {string} name Name of the preference to save
* @param {string} value Value of the preference to save
* @return {boolean} True upon sucess
**/
function savePref(name, value) {
PropertiesService.getUserProperties()
.setProperty(name, value);
return true;
}
/**
* Gets the stored user preference for converting to rainbowText by default
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @return {Boolean} The user's default rainbow text preference, if it exists.
*/
function getPreferences() {
var userProperties = PropertiesService.getUserProperties();
return userProperties.getProperty('autoRainbow');
}