-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
56 lines (52 loc) · 1.69 KB
/
code.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
function onOpen(e){
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Tools Basics', 'showSidebar')
.addToUi();
}
function onInstall(e){
onOpen(e);
}
function showSidebar(){
var ui = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('Tools Basics');
SpreadsheetApp.getUi().showSidebar(ui);
}
function activeCells(mode){
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
if (numRows === 0 || numCols === 0) throw new Error('Please select some cells.');
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var currentValue = range.getCell(i,j).getValue();
range.getCell(i,j).setValue(convertCase(mode, currentValue));
}
}
return true;
}
function convertCase(mode, text){
var textResult;
var textBuffer;
switch(mode){
case 'lower':
textResult = text.toLowerCase();
break;
case 'upper':
textResult = text.toUpperCase();
break;
case 'title':
var textSplit = text.split(" ");
var arrayBuffer = [];
for(var index = 0; index < textSplit.length; index ++){
textBuffer = textSplit[index].toLowerCase();
arrayBuffer.push(textBuffer.charAt(0).toUpperCase() + textBuffer.slice(1));
}
textResult = arrayBuffer.join(" ");
break;
case 'first':
textBuffer = text.toLowerCase();
textResult = textBuffer.charAt(0).toUpperCase() + textBuffer.slice(1);
break;
}
return textResult;
}