-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUploader.gs
132 lines (106 loc) · 3.97 KB
/
Uploader.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
function openImageUploader() {
var html = HtmlService.createTemplateFromFile('image-uploader')
.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE)
.setWidth(540)
.setHeight(540 * 5);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Image Uploader');
}
function openImageUploaderSidebar() {
var html = HtmlService.createTemplateFromFile('image-uploader')
.evaluate()
.setTitle('Image Uploader')
.setWidth(540)
.setHeight(540 * 5);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
//==========A1 notations==========
function getSelectedCellNames() {
var ranges = [];
var sel = SpreadsheetApp.getActive().getSelection().getActiveRangeList().getRanges();
for (var i = 0; i < sel.length; i++) {
ranges.push(sel[i].getA1Notation())
}
// Logger.log(ranges);
// Logger.log(getUniqueCells(ranges));
return getUniqueCells(ranges);
}
function getUniqueCells(inputArray) {
var uniqueCells = [];
inputArray.forEach(function(value) {
var range = value.split(':');
if (range.length === 1) {
// Single cell
uniqueCells.push(range[0]);
} else {
// Range of cells
var startCell = parseA1Notation(range[0]);
var endCell = parseA1Notation(range[1]);
for (var row = startCell.row; row <= endCell.row; row++) {
for (var col = startCell.col; col <= endCell.col; col++) {
uniqueCells.push(generateA1Notation(row, col));
}
}
}
});
return uniqueCells;
}
function parseA1Notation(a1Notation) {
var matches = a1Notation.match(/([A-Z]+)(\d+)/);
var colString = matches[1];
var row = parseInt(matches[2], 10);
var col = 0;
for (var i = 0; i < colString.length; i++) {
col += (colString.charCodeAt(i) - 65 + 1) * Math.pow(26, colString.length - i - 1);
}
return {
row: row,
col: col
};
}
function generateA1Notation(row, col) {
var colString = '';
while (col > 0) {
var remainder = (col - 1) % 26;
colString = String.fromCharCode(65 + remainder) + colString;
col = Math.floor((col - remainder) / 26);
}
return colString + row;
}
//==========Files==========
function updateCellWithImage(fileBlob, cellNotation) {
// var dummyImageBlob = Utilities.newBlob("Dummy Image", "image/jpeg", "dummy.jpg");
// fileBlob = dummyImageBlob
if (fileBlob != null) {
var imageFile = DriveApp.createFile(Utilities.newBlob(...fileBlob));
// Set the access to anyone with the link
imageFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var imageUrl = imageFile.getDownloadUrl();
var image = SpreadsheetApp
.newCellImage()
.setSourceUrl(imageUrl)
.setAltTextTitle("")
.setAltTextDescription("")
.toBuilder()
.build();
// Set the value of the specified cell with the image
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(cellNotation);
range.setValue(image);
// console.log(cellNotation);
// DriveApp.getFileById(imageFile.getId()).setTrashed(true); // Set the file's trashed attribute to true (moves it to the trash folder)
deleteDriveItem(imageFile.getId()); // Permanently delete the file from Google Drive using the Drive REST API as an advanced service.
}
}
function deleteDriveItem(fileId) {
var file = Drive.Files.get(fileId);
if (file.mimeType === MimeType.FOLDER) {
// Possibly ask for confirmation before deleting this folder.
}
Drive.Files.remove(file.id); // "remove" in Apps Script client library, "delete" elsewhere
}
//==========Helpers==========
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}