Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RyanRusnak/773a93562511a6f6bfa7a069ae3f0f19 to your computer and use it in GitHub Desktop.
Save RyanRusnak/773a93562511a6f6bfa7a069ae3f0f19 to your computer and use it in GitHub Desktop.
SDSW google sheets scripting
// ========== Sheet setup ==========
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculator");
var dimensionSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("dimensions");
// ========== Getters ==========
function fromAddress(){
return sheet.getRange("B2").getCell(1, 1).getValue();
}
function toAddress(){
return sheet.getRange("B3").getCell(1, 1).getValue();
}
function orderWeight(){
return sheet.getRange("B6").getCell(1, 1).getValue();
}
function surge(){
return sheet.getRange("B8").getCell(1, 1).getValue();
}
// ========== Vehicle Type Calculations ==========
function orderMetrics(dimensions){
var volume = dimensions.reduce(function(a,b){return a*b;});
return dimensions.concat(volume).concat(orderWeight());
}
function orderDimensions(){
return sheet.getRange("B6").getCell(1, 1).getValue().split(',')
.sort(function(a, b){return b-a});
}
// returns 1 for car, 2 for hatchback and 3 for van, 4 for truck
function vehicleType(orderMetrics){
var matrix = dimensionSheet.getRange("B2:F4").getDisplayValues();
for(var i=0; i<matrix.length; i++){
// if it is less than iteration deminsions, max volume and max weight
if (dimensionalFit(orderMetrics, matrix[i])){
return i + 1;
}
}
// use a truck if it got this far
return 4
}
function vehicleName(){
var vehicles = ["car", "hatchback", "van", "truck"];
return vehicles[vehicleType(orderMetrics(orderDimensions())) -1];
}
function dimensionalFit(dim1, dim2){
for(var i = 0; i < dim1.length; i++){
if(dim1[i] > dim2[i]){
return false;
}
}
// if it got here, all the metrics are smaller
return true;
}
// ========== End Vehicle Type Calculations ==========
// ========== Geocoding ==========
function geocodeAddress(address){
var geocoder = Maps.newGeocoder().setRegion('us');
location = geocoder.geocode(address);
if (location.status == 'OK') {
var lat = location["results"][0]["geometry"]["location"]["lat"];
var lng = location["results"][0]["geometry"]["location"]["lng"];
return [lat, lng];
}
}
// dist((x, y), (a, b)) = √(x - a)² + (y - b)² for anyone that forgot!
function euclidianDistance(fromAddress, toAddress){
var fromCoords = geocodeAddress(fromAddress);
var toCoords = geocodeAddress(toAddress);
var radical = Math.pow(fromCoords[0] - toCoords[0], 2) + Math.pow(fromCoords[1] - toCoords[1], 2)
return Math.sqrt(radical)
}
function driveDistance(fromAddress, toAddress) {
var directions = Maps.newDirectionFinder()
.setOrigin(fromAddress).setDestination(toAddress)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
var d = directions.routes[0].legs[0].distance.text;
return parseInt(d.split(" ")[0].replace(",", ""));
}
function starWarsCharacter(index){
var response = UrlFetchApp.fetch("http://swapi.co/api/people/" + index);
var data = JSON.parse(response);
return data.name;
}
function bestDriver(distance, surge){
var index = Math.round(distance * surge);
return starWarsCharacter(parseInt(index));
}
// ========== Setters ==========
function onEdit(e){
sheet.getRange("B9").getCell(1, 1).setValue(vehicleName());
sheet.getRange("B4").getCell(1, 1).setValue(euclidianDistance(fromAddress(), toAddress()));
sheet.getRange("B5").getCell(1, 1).setValue(driveDistance(fromAddress(), toAddress()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment