Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Last active March 1, 2020 22:25
Show Gist options
  • Select an option

  • Save freddiefujiwara/875c52cb2d54a4d15b1043001d8f1124 to your computer and use it in GitHub Desktop.

Select an option

Save freddiefujiwara/875c52cb2d54a4d15b1043001d8f1124 to your computer and use it in GitHub Desktop.
function doGet(e) {
//parameter validation
if(!e || !e.parameter || !e.parameter.id){
return redirect();
}
var user = Session.getActiveUser().getEmail();
var devices = SpreadsheetApp.openById("[Your spread sheet]").getSheetByName("devices");
var history = SpreadsheetApp.openById("[Your spread sheet]").getSheetByName("history");
var historyLastRow = history.getDataRange().getLastRow();
var values = devices.getDataRange().getValues();
var headers = values.shift();
var idColumn = -1;
var userColumn = -1;
var targetRow = -1;
//find id column and user column
headers.forEach(function(header,i){
if(String(header) == "id"){
idColumn = i;
}else if(String(header) == "user"){
userColumn = i;
}
});
//find target row
values.forEach(function(value,i){
if(e.parameter.id == value[idColumn]){
targetRow = i;
}
});
//validate
if(idColumn == -1 || userColumn == -1 || targetRow == -1){
return redirect();
}
//edit the sheet
var range = devices.getRange(targetRow+2,userColumn+1);
if(range.getValue() == user){
range.setValue("");
range.setBackground("white");
history.insertRowAfter(historyLastRow);
history.getRange(historyLastRow+1,1,1,4).setValues([[
e.parameter.id,user,(new Date()).toLocaleString(),"RETURN"
]]);
}else if(range.getValue() == ""){
range.setValue(user);
range.setBackground("red");
history.insertRowAfter(historyLastRow);
history.getRange(historyLastRow+1,1,1,4).setValues([[
e.parameter.id,user,(new Date()).toLocaleString(),"LOAN"
]]);
}
return redirect();
}
//redirect to glideapp
function redirect(){
return HtmlService.createHtmlOutput(
"<script>window.top.location.href='https://[Your glide app].glideapp.io/';</script>"
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment