-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_functions.gs
200 lines (175 loc) · 5.13 KB
/
custom_functions.gs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Calculates MD2 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates MD2 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function MD2(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD2, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Calculates MD5 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates MD5 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function MD5(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Calculates SHA1 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates SHA1 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function SHA1(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input_string)
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Calculates SHA256 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates SHA256 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function SHA256(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Calculates SHA384 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates SHA384 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function SHA384(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_384, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Calculates SHA512 of a string or cell (or concatenation of cells)
*
* @param {string} input_string Input string or cell
* @return Calculates SHA512 of a string or cell (or concatenation of cells)
* @customfunction
*
*/
function SHA512(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_512, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
/**
* Function to calculate percent change
*
* @param {number} oldVal Original Value
* @param {number} newVal New Value
* @return The percent change between new and old values.
* @customfunction
*
*/
function PercentChange(oldVal, newVal) {
return (newVal - oldVal) / oldVal;
}
// Indentation function
var ss = SpreadsheetApp.getActiveSpreadsheet();
function moveText(direction) {
var values = ss.getActiveRange().getValues();
var cols = ss.getActiveRange().getNumColumns();
var rows = ss.getActiveRange().getNumRows();
var newValues = new Array();
for (x = 1; x <= rows; x++) {
for (y = 1; y <= cols; y++) {
var cell = ss.getActiveRange().getCell(x, y);
var value = cell.getValue();
var formula = function() {
if (direction == ">>>>>") {
return '=CONCAT(REPT( CHAR( 160 ), 5),"' + value + '")';
} else if (direction == "<<<<<") {
return '=IF(TRIM(LEFT("' + value + '", 5))=CONCAT(REPT( CHAR( 160 ), 5),""), MID("' + value + '", 6, LEN("' + value + '")), TRIM("' + value + '"))';
} else if (direction == '>') {
return '=CONCAT(REPT( CHAR( 160 ), 1),"' + value + '")';
} else if (direction == '<') {
return '=IF(TRIM(LEFT("' + value + '", 1))=CONCAT(REPT( CHAR( 160 ), 1),""), MID("' + value + '", 2, LEN("' + value + '")), TRIM("' + value + '"))';
}
}
Logger.log(formula);
if (value != '') {
cell.setFormula([formula()]);
cell.setValue(cell.getValue());
} else {
cell.setValue(['']);
}
}
}
};
function indentText1() {
moveText(">");
};
function flushLeft1() {
moveText("<");
};
function indentText5() {
moveText(">>>>>");
};
function flushLeft5() {
moveText("<<<<<");
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : ">",
functionName : "indentText1"
},{
name : ">>>>>",
functionName : "indentText5"
},{
name : "<<<<<",
functionName : "flushLeft5"
},{
name : "<",
functionName : "flushLeft1"
}];
sheet.addMenu("Indent Text", entries);
};