-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleAppScript.gs
114 lines (104 loc) · 2.87 KB
/
GoogleAppScript.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
const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
function doPost(request)
{
var command = JSON.parse(request.postData.contents);
let lock = LockService.getScriptLock();
lock.waitLock(6000);
if(command.Operation == "get")
{
let cache = CacheService.getScriptCache();
let data = cache.get("borrit");
if (data == null)
{
data = loadRows();
updateCache(data);
}
else
{
data = JSON.parse(data);
}
return buildResponse({Success: true, Error: "", BorrowedEntries: data }, lock);
}
if(command.Operation == "borrow")
{
if(command.BorrowedEntries == undefined)
{
return buildResponse({Success: false, Error: "missing borrowed entries"}, lock);
}
borrowAssets(command.BorrowedEntries);
updateCache(loadRows());
return buildResponse({Success: true, Error: ""}, lock);
}
if(command.Operation == "return")
{
if(command.ReturnedGuids == undefined)
{
return buildResponse({Success: false, Error: "missing retruned guids"}, lock);
}
returnAssets(command.ReturnedGuids);
updateCache(loadRows());
return buildResponse({Success: true, Error: ""}, lock);
}
return buildResponse({Success: false, Error: "operation unknown"}, lock);
}
function loadRows()
{
SpreadsheetApp.flush();
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0];
let retVal = [];
const data = sheet.getDataRange().getValues();
for(let i = 1; i < data.length; ++i)
{
const row = data[i];
retVal.push({ Guid: row[0], User: row[1], UtcDateTime: row[2] });
}
return retVal;
}
function updateCache(data)
{
let cache = CacheService.getScriptCache();
cache.remove("borrit");
cache.put('borrit', JSON.stringify(data), 21600); // cache for 6 hours(is the max), max 100k data
}
function borrowAssets(borrowedEntries)
{
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0];
for(let i = 0; i < borrowedEntries.length; ++i)
{
const entry = borrowedEntries[i];
const cell = sheet.createTextFinder(entry.Guid).matchEntireCell(true).findNext();
if(cell != null)
{
sheet.deleteRow(cell.getRow());
}
sheet.appendRow([entry.Guid, entry.User, entry.UtcDateTime]);
}
}
function returnAssets(guids)
{
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0];
for(let i = 0; i < guids.length; ++i)
{
const entry = guids[i];
const cell = sheet.createTextFinder(entry).matchEntireCell(true).findNext();
if(cell != null)
{
sheet.deleteRow(cell.getRow());
}
}
}
function buildResponse(obj, lock)
{
lock.releaseLock();
return ContentService.createTextOutput(JSON.stringify(obj));
}
// Run this method if you modify the spreadsheet data manually
function syncCacheWithSheet()
{
updateCache(loadRows());
}
function clearCache()
{
let cache = CacheService.getScriptCache();
cache.remove("borrit");
}