-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-image-by-asset-id.gs
More file actions
92 lines (73 loc) · 3.07 KB
/
Copy pathupdate-image-by-asset-id.gs
File metadata and controls
92 lines (73 loc) · 3.07 KB
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
/*
-----------------------------------------------------
=====================================================
SETUP
Assumes your Asset Id's starts in column A2 and its
corresponding image URL in column B.
Example:
A B
1 ASSET ID | IMAGE URL
2 1 | http://example.com/image.jpg
3 2 | http://example.com/image2.jpg
** Only tested on images with .jpg file extension **
=====================================================
-----------------------------------------------------
*/
function updateSelectedAssetTags() {
// START customization ============================
var api_baseurl = 'https://your-url/api/v1'; // fully qualified url to the base api - should end in /v1, no trailing slash"
var api_bearer_token = 'Bearer YOUR-BEARER-TOKEN'; // should start with "Bearer "
// END customization =============================
var sheet = SpreadsheetApp.getActive().getActiveSheet()
var lastRow = sheet.getLastRow();
// Get the range for columns A and B
var range = sheet.getRange("A2:B" + lastRow);
// Get the values from the range
var data = range.getValues();
let i = 0;
data.forEach(function(row) {
if (i > 1) {
let asset_id = Math.floor(row[0]);
let imageUrl = row[1];
let url = api_baseurl + '/hardware/' + asset_id;
var base64 = convertBase64(imageUrl)
var options = {
method: 'patch',
muteHttpExceptions: true,
headers: {
Accept: 'application/json',
Authorization: api_bearer_token,
'Content-Type': 'application/json',
},
payload: JSON.stringify({
image: base64,
}),
};
// Make the http request and collect and parse the response
var response = UrlFetchApp.fetch(url, options);
var response_msg = JSON.parse(response.getContentText());
Logger.log('Payload: ' + options.payload);
Logger.log('Endpoint: ' + url);
Logger.log('Row Array: ' + row);
Logger.log('HTTP Response Code: ' + response.getResponseCode());
Logger.log('Messages: ' + response_msg.messages);
if ((response.getResponseCode() === 200) && (response_msg.status == 'success')) {
var status_color = '#b6d7a8';
} else {
var status_color = '#f4cccc';
}
// Put the status in the third column for each row
sheet.getRange("C" + Math.floor(i)+1).setValue(response_msg.messages).setBackground(status_color);
}
i++;
// This is used for very large imports so as not to exceed API throttle limits
Utilities.sleep(1000);
});
// end foreach
}
//Encodes jpg to base64
function convertBase64(imageUrl) {
const blob = UrlFetchApp.fetch(imageUrl).getBlob();
const base64String = Utilities.base64Encode(blob.getBytes());
return `data:image/jpg;base64,${base64String}`;
};