-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppRefresh.gs
151 lines (139 loc) · 5.36 KB
/
AppRefresh.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
/**
* @author EdenSkull / https://github.com/Edenskull
*
* Description: Manager script with custom nomenclature.
*
* Usage:
*
* Setup The 4 var by going to the Spreadsheet linked and run the 4 script with setup in the name.
*/
function initFromScratch() {
var sheetName = getSheetName();
var wallSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
wallSheet.clear();
var folderId = getFolder();
var files = DriveApp.getFolderById(folderId).getFiles();
var index = 1;
var prefix = getPrefix();
var formatNumber = getFormatNumber();
var currentFile, fileId, repeatNumber, filename;
var template = String("0");
while (files.hasNext()) {
currentFile = files.next();
fileId = currentFile.getId();
repeatNumber = (index.toString().length <= formatNumber) ? formatNumber - index.toString().length : 0;
filename = prefix + " " + (template.repeat(repeatNumber) + index);
currentFile.setName(filename);
wallSheet.appendRow([index, filename, fileId]);
index++;
}
}
function processNew() {
var sheetName = getSheetName();
var wallSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var entries = wallSheet.getSheetValues(1, 1, wallSheet.getLastRow(), 3);
var allIds = entries.map(function (row) {
return row[2];
});
var folderId = getFolder();
var files = DriveApp.getFolderById(folderId).getFiles();
var prefix = getPrefix();
var formatNumber = getFormatNumber();
var template = String("0");
var index = 0
var currentFile, currentId, currentFileId, filename, repeatNumber, tempFile;
for (index; index < entries.length; index++) {
currentId = entries[index][2];
try {
tempFile = DriveApp.getFileById(currentId);
if (tempFile.isTrashed()) {
while (files.hasNext()) {
currentFile = files.next();
currentFileId = currentFile.getId();
if (allIds.indexOf(currentFileId) == -1) {
DriveApp.getFileById(currentFileId).setName(entries[index][1]);
wallSheet.getRange(index + 1, 3).setValue(currentFileId);
break;
}
}
}
} catch (e) {
while (files.hasNext()) {
currentFile = files.next();
currentFileId = currentFile.getId();
if (allIds.indexOf(currentFileId) == -1) {
DriveApp.getFileById(currentFileId).setName(entries[index][1]);
wallSheet.getRange(index + 1, 3).setValue(currentFileId);
break;
}
}
}
}
index++;
while (files.hasNext()) {
currentFile = files.next();
currentId = currentFile.getId();
if (allIds.indexOf(currentId) == -1) {
repeatNumber = (index.toString().length <= formatNumber) ? formatNumber - index.toString().length : 0;
filename = prefix + " " + (template.repeat(repeatNumber) + index);
currentFile.setName(filename);
wallSheet.appendRow([index, filename, currentId]);
}
}
}
function setSheetName() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Change Sheet", "Provide the name of the sheet you want to use : ", ui.ButtonSet.OK_CANCEL);
if (response.getResponseText() !== "") {
PropertiesService.getScriptProperties().setProperty('_sheetName', response.getResponseText());
}
}
function getSheetName() {
return PropertiesService.getScriptProperties().getProperty('_sheetName');
}
function setPrefix() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Change Prefix", "Provide the prefix you want to put in the name of your wallpapers ([prefix] number; Example: [Wallpaper -] 00001) : ", ui.ButtonSet.OK_CANCEL);
if (response.getResponseText() !== "") {
PropertiesService.getScriptProperties().setProperty('_prefix', response.getResponseText());
}
}
function getPrefix() {
return PropertiesService.getScriptProperties().getProperty('_prefix');
}
function setFolder() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Change Folder", "Provide the id of the target folder : ", ui.ButtonSet.OK_CANCEL);
if (response.getResponseText() !== "") {
PropertiesService.getScriptProperties().setProperty('_folderId', response.getResponseText());
}
}
function getFolder() {
return PropertiesService.getScriptProperties().getProperty('_folderId');
}
function setNumberFormat() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Change Number Format", "Provide the max number of wallpapers (Example : 1000 will change the name to Wallpaper - 0658) : ", ui.ButtonSet.OK_CANCEL);
if (response.getResponseText() !== "") {
PropertiesService.getScriptProperties().setProperty('_formatNumber', response.getResponseText().length);
}
}
function getFormatNumber() {
return parseInt(PropertiesService.getScriptProperties().getProperty('_formatNumber'), 10);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('WallManager')
.addItem('Init', 'initFromScratch')
.addItem('Process New', 'processNew')
.addSeparator()
.addItem('Setup Sheet', 'setSheetName')
.addItem('Setup Folder', 'setFolder')
.addItem('Setup Prefix', 'setPrefix')
.addItem('Setup Number Format', 'setNumberFormat')
.addToUi();
}
String.prototype.repeat = function (num) {
return new Array(num + 1).join(this);
}