-
Notifications
You must be signed in to change notification settings - Fork 22
/
script.gs
145 lines (131 loc) · 4.17 KB
/
script.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
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetsCount = ss.getNumSheets();
var sheets = ss.getSheets();
function onOpen() {
// Try New Google Sheets method
try{
var ui = SpreadsheetApp.getUi();
ui.createMenu('Spreadsheet Cleanup')
.addItem('Show Sheets', 'showSheets')
.addItem('Hide Sheets', 'hideSheets')
.addItem('Delete Sheets All Sheets From Sheet #', 'deleteAllSheetsFrom')
.addItem('Delete Sheets', 'deleteSheets')
.addItem('Copy Sheets', 'copySheets')
.addToUi();
}
// Log the error
catch (e){Logger.log(e)}
// Use old Google Spreadsheet method
finally{
var items = [
{name: 'Hide Sheets', functionName: 'hideSheets'},
{name: 'Show Sheets', functionName: 'showSheets'},
{name: 'Delete Sheets All Sheets From Sheet #', functionName: 'deleteAllSheetsFrom'},
{name: 'Delete Sheets', functionName: 'deleteSheets'},
{name: 'Copy Sheets', functionName: 'copySheets'},
];
ss.addMenu('Spreadsheet Cleanup', items);
}
}
function deleteSheets() {
var deleteSheetsContaining = Browser.inputBox("Delete sheets with names containing:");
deleteSheetsContaining = deleteSheetsContaining.toLowerCase();
if (sheetMatch(deleteSheetsContaining)){
for (var i = 0; i < sheetsCount; i++){
var sheet = sheets[i];
var sheetName = sheet.getName();
sheetName = sheetName.toLowerCase();
Logger.log(sheetName);
if (sheetName.indexOf(deleteSheetsContaining.toString()) !== -1){
Logger.log("DELETE!");
ss.deleteSheet(sheet);
}
}
} else {
noMatchAlert();
}
}
function deleteAllSheetsFrom() {
var deleteSheetsFrom = Browser.inputBox("Delete all sheet from sheet number:");
for (var i = deleteSheetsFrom -1 ; i < sheetsCount; i++)
{
ss.deleteSheet(sheets[i]);
}
}
function hideSheets() {
var hideSheetsContaining = Browser.inputBox("Hide sheets with names containing:");
if (sheetMatch(hideSheetsContaining)){
for (var i = 0; i < sheetsCount; i++){
var sheet = sheets[i];
var sheetName = sheet.getName();
Logger.log(sheetName);
if (sheetName.indexOf(hideSheetsContaining.toString()) !== -1){
Logger.log("HIDE!");
sheet.hideSheet();
}
}
} else {
noMatchAlert();
}
}
function showSheets() {
var showSheetsContaining = Browser.inputBox("Show sheets with names containing:");
if (sheetMatch(showSheetsContaining)){
for (var i = 0; i < sheetsCount; i++){
var sheet = sheets[i];
var sheetName = sheet.getName();
Logger.log(sheetName);
if (sheetName.indexOf(showSheetsContaining.toString()) !== -1){
Logger.log("SHOW!");
sheet.showSheet();
}
}
} else {
noMatchAlert();
}
}
function copySheets() {
var copySheetsContaining = Browser.inputBox("Copy sheets with names containing:");
var destinationId = Browser.inputBox("Enter the destination spreadsheet ID:");
if (sheetMatch(copySheetsContaining)){
for (var i = 0; i < sheetsCount; i++){
var sheet = sheets[i];
var sheetName = sheet.getName();
Logger.log(sheetName);
if (sheetName.indexOf(copySheetsContaining.toString()) !== -1){
Logger.log("COPY!");
var destination = SpreadsheetApp.openById(destinationId);
sheet.copyTo(destination);
}
}
successAlert('copied')
} else {
noMatchAlert();
}
}
// determine if any sheets match the user input
function sheetMatch(sheetMatch){
for (var i = 0; i < sheetsCount; i++){
var sheetName = sheets[i].getName();
if (sheetName.indexOf(sheetMatch.toString()) !== -1){
return true
}
}
return false
}
// alert if no sheets matched the user input
function noMatchAlert() {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'No Sheets Matched Your Input',
"Try again and make sure you aren't using quotes.",
ui.ButtonSet.OK);
}
// alert after succesful action (only used in copy)
function successAlert(action) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'Success!',
"You're sheets were " + action + " successfully.",
ui.ButtonSet.OK);
}