-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebApp.gs
55 lines (50 loc) · 1.61 KB
/
WebApp.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
/**
* Processes a POST request. The request body should contain a command that is one of the following:
*
* - insertImage
* - removeAllImages
*/
function doPost(e) {
const params = JSON.parse(e.postData.contents);
if (params.command === "insertImage") {
insertImage(params);
} else if (params.command === "removeAllImages") {
removeAllImages(params);
}
}
/**
* Add an image to a sheet. The params object should contain the following fields:
*
* spreadsheetId: ID of spreadsheet
* sheetId: ID of sheet
* url: url of image, e.g. "https://www.google.com/images/srpr/logo3w.png"
* column: 1-indexed column, e.g. 1
* row: 1-indexed row, e.g. 1
* offsetX: x-offset from cell position, e.g. 0
* offsetY: y-offset from cell position, e.g. 0
* width: width of image in sheet, e.g. 100
* height: height of image in sheet, e.g. 100
*/
function insertImage(params) {
const sheet = getSheet(params.spreadsheetId, params.sheetId);
const image = sheet.insertImage(params.url, params.column, params.row, params.offsetX, params.offsetY);
image.setWidth(params.width);
image.setHeight(params.height);
}
/**
* Removes all overlay images from a sheet. The params object should contain the following fields:
*
* spreadsheetId: ID of spreadsheet
* sheetId: ID of sheet
*/
function removeAllImages(params) {
const sheet = getSheet(params.spreadsheetId, params.sheetId);
sheet.getImages().map(image => image.remove());
}
function getSheet(spreadsheetId, sheetId) {
for (const sheet of SpreadsheetApp.openById(spreadsheetId).getSheets()) {
if (sheet.getSheetId() === sheetId) {
return sheet;
}
}
}