-
Notifications
You must be signed in to change notification settings - Fork 1
/
DriveImage2Sheets.js
90 lines (75 loc) · 3.63 KB
/
DriveImage2Sheets.js
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
/*
Created by: RemcoE33
https://github.com/RemcoE33/apps-script-codebase
*/
function onOpen(e) {
const ui = SpreadsheetApp.getUi();
ui.createMenu("Drive images")
.addItem("Setup", "setup")
.addItem("Run preconfigured", "preconfigured")
.addItem("Run manual", "manual")
.addItem(`Download url's`, 'downloadUrls')
.addToUi();
}
function setup() {
const ui = SpreadsheetApp.getUi();
const driveFolder = ui.prompt("Enter google drive folder id").getResponseText().trim()
const imageType = `image/${ui.prompt("Enter image type: (png / jpeg / gif / svg").getResponseText().toLowerCase().trim()}`
const mode = Number(ui.prompt("Image mode ( https://support.google.com/docs/answer/3093333?hl=en )").getResponseText().trim());
const onOff = ui.prompt("If you want a on / off switch enter a cell notation (A1) if not leave blank").getResponseText().trim();
const propertyService = PropertiesService.getScriptProperties();
propertyService.setProperties({ 'folder': driveFolder, 'image': imageType, 'mode': mode, 'onOff': onOff });
}
function preconfigured() {
const propertyService = PropertiesService.getScriptProperties();
const driveFolder = propertyService.getProperty('folder');
const imageType = propertyService.getProperty('image');
const mode = Number(propertyService.getProperty('mode'));
const onOff = propertyService.getProperty('onOff');
const images = DriveApp.getFolderById(driveFolder).getFilesByType(imageType);
_processImages(images, mode, onOff);
}
function manual() {
const ui = SpreadsheetApp.getUi();
const driveFolder = ui.prompt("Enter google drive folder id").getResponseText().trim()
const imageType = `image/${ui.prompt("Enter image type: (png / jpeg / gif / svg").getResponseText().toLowerCase().trim()}`
const mode = Number(ui.prompt("Image mode ( https://support.google.com/docs/answer/3093333?hl=en )").getResponseText().trim());
const onOff = ui.prompt("If you want a on / off switch enter a cell notation (A1) if not leave blank").getResponseText().trim();
const images = DriveApp.getFolderById(driveFolder).getFilesByType(imageType);
_processImages(images, mode, onOff);
}
function _processImages(images, mode, onOff) {
const output = [];
while (images.hasNext()) {
const file = images.next();
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
const downloadUrl = file.getDownloadUrl();
if (onOff) {
output.push([`=IF(${onOff} = TRUE,IMAGE("${downloadUrl}",${mode}),)`])
} else {
output.push([`=IMAGE("${downloadUrl}",${mode})`])
}
}
if (onOff) {
SpreadsheetApp.getActiveSheet().getRange(1, 1).insertCheckboxes();
SpreadsheetApp.getActiveSheet().getRange(2, 1, output.length, 1).setFormulas(output);
} else {
SpreadsheetApp.getActiveSheet().getRange(1, 1, output.length, 1).setFormulas(output);
}
SpreadsheetApp.getUi().alert(`Processed ${output.length} images`)
}
function downloadUrls(){
const ui = SpreadsheetApp.getUi();
const driveFolder = ui.prompt("Enter google drive folder id").getResponseText().trim()
const imageType = `image/${ui.prompt("Enter image type: (png / jpeg / gif / svg").getResponseText().toLowerCase().trim()}`
const images = DriveApp.getFolderById(driveFolder).getFilesByType(imageType);
const output = [['Filename',['Download url']]];
while (images.hasNext()) {
const file = images.next();
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
const fileName = file.getName();
const downloadUrl = file.getDownloadUrl();
output.push([fileName,downloadUrl])
}
SpreadsheetApp.getActiveSheet().getRange(1,1,output.length,2).setValues(output);
}