forked from teachduttonteach/GSuiteObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drive.gs
56 lines (54 loc) · 1.89 KB
/
Drive.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
var Drive = function() {
this.getRandomPicture = function(folder) {
if (null != folder) {
var dailyPicturesFolder = DriveApp.getFolderById(folder);
if (null != dailyPicturesFolder) {
var dailyPictures = dailyPicturesFolder.getFiles();
var allPictures = [];
while (dailyPictures.hasNext()) {
var pic = dailyPictures.next();
if (["image/png", "image/gif", "image/jpeg"].indexOf(pic.getMimeType()) >= 0) {
allPictures.push(pic.getAs("image/png"));
}
}
if (allPictures.length > 0) {
return chosenPicture = allPictures[Math.floor(Math.random() * Math.floor(allPictures.length))];
} else {
throw new Error("No pictures found in Drive.getRandomPicture");
}
} else {
throw new Error("Could not find folder in Drive.getRandomPicture");
}
} else {
throw new Error("Folder must be defined in Drive.getRandomPicture");
}
};
this.getImageBlob = function(name) {
if (null != name) {
if (DriveApp.getFileById(name)) {
switch (DriveApp.getFileById(name).getMimeType()) {
case "image/jpeg":
case "image/png":
case "image/gif":
return DriveApp.getFileById(name).getBlob();
}
}
return null;
} else {
throw new Error("Name needs to be defined for Drive.getImageBlob");
}
};
this.getOrCreateFile = function(fileName, templateName) {
if ((null != fileName) && (null != templateName)) {
var fileObject = DriveApp.getFilesByName(fileName);
if (!fileObject.hasNext()) {
fileObject = DriveApp.getFilesByName(templateName).next().makeCopy(fileName);
} else {
fileObject = fileObject.next();
}
return fileObject;
} else {
throw new Error("File name and template name need to be defined for Drive.getOrCreateFile");
}
};
};