Skip to content

Instantly share code, notes, and snippets.

@asciimike
Created February 4, 2022 17:09
Show Gist options
  • Save asciimike/79c39624c1b166bcf3486e9d4c091bcb to your computer and use it in GitHub Desktop.
Save asciimike/79c39624c1b166bcf3486e9d4c091bcb to your computer and use it in GitHub Desktop.
function convertToML(val, unit, conversions) {
var conversionFactors = {};
for (i = 1; i < conversions.length; i++) {
conversionFactors[conversions[i][0]] = conversions[i][1]
}
return val * conversionFactors[unit];
}
function calculateAverageValueByVolume(volumes, values) {
var totalVolume = 0.0;
var averageVolume = 0.0;
for (i = 0; i < volumes.length; i++) {
if (typeof values[i][0] !== 'number' || typeof volumes[i][0] !== 'number') {
return averageVolume/totalVolume;
}
totalVolume += volumes[i][0];
averageVolume += volumes[i][0] * values[i][0];
}
return averageVolume/totalVolume;
}
function calculateDilutionPercent(structure, abv) {
switch (structure) {
case "Built":
return 0.25;
case "Stirred":
return -1.21 * Math.pow(abv, 2) + 1.246 * abv + 0.145;
case "Shaken":
return -1.567 * Math.pow(abv, 2) + 1.742 * abv + 0.203;
case "Blended":
return 0.9;
default:
return 0;
}
}
function checkRangeForStructure(structure, ranges, type, value) {
var IV_LOW = 1, IV_HI = 2, IABV_LOW = 3, IABV_HI = 4, IBRIX_LOW = 5, IBRIX_HI = 6, IACID_LOW = 7, IACID_HI = 8;
var FV_LOW = 9, FV_HI = 10, FABV_LOW = 11, FABV_HI = 12, FBRIX_LOW = 13, FBRIX_HI = 14, FACID_LOW = 15, FACID_HI = 16;
var STRUCTURE = 0;
switch (structure) {
case "Built":
STRUCTURE = 1;
break;
case "Shaken":
STRUCTURE = 3;
break;
case "Stirred":
STRUCTURE = 2;
break;
case "Blended":
STRUCTURE = 4;
case "Carbonated":
STRUCTURE = 5;
break;
default:
STRUCTURE = 0;
break;
}
var low = 0, hi = 0;
switch(type) {
case "initial_volume":
low = ranges[STRUCTURE][IV_LOW];
hi = ranges[STRUCTURE][IV_HI];
break;
case "initial_abv":
low = ranges[STRUCTURE][IABV_LOW];
hi = ranges[STRUCTURE][IABV_HI];
break;
case "initial_brix":
low = ranges[STRUCTURE][IBRIX_LOW];
hi = ranges[STRUCTURE][IBRIX_HI];
break;
case "initial_acid":
low = ranges[STRUCTURE][IACID_LOW];
hi = ranges[STRUCTURE][IACID_HI];
break;
case "final_volume":
low = ranges[STRUCTURE][FV_LOW];
hi = ranges[STRUCTURE][FV_HI];
break;
case "final_abv":
low = ranges[STRUCTURE][FABV_LOW];
hi = ranges[STRUCTURE][FABV_HI];
break;
case "final_brix":
low = ranges[STRUCTURE][FBRIX_LOW];
hi = ranges[STRUCTURE][FBRIX_HI];
break;
case "final_acid":
low = ranges[STRUCTURE][FACID_LOW];
hi = ranges[STRUCTURE][FACID_HI];
break;
default:
break;
}
if (value < low) {
return "low";
} else if (value > hi) {
return "high";
} else {
return "acceptable";
}
}
function getRecipeNames(recipes) {
var names = [];
for(i = 0; i < recipes.length; i++) {
if (typeof recipes[i][0] === 'string' && recipes[i][0] !== '' && recipes[i][1] === '' && recipes[i][2] === '') {
names.push(recipes[i][0]);
}
}
return names;
}
function getRecipeForName(recipes, name) {
var recipe = [];
var isRecipe = false;
for(i = 0; i < recipes.length; i++) {
if (recipes[i][0] !== name && isRecipe === false) {
continue;
}
if (recipes[i][0] === name) {
isRecipe = true;
i++;
}
if (recipes[i][0] === '') {
break;
}
recipe.push(recipes[i]);
}
return recipe;
}
function onEdit(e) {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var calculatorSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculator");
var recipeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Recipes");
var activeCell = activeSheet.getActiveCell();
// Handle recipe changes
console.log(activeCell.getColumn());
console.log(activeCell.getRow());
if(activeCell.getColumn() == 2 && activeCell.getRow() == 19){
var recipeName = activeCell.getValue();
console.log(recipeName);
var recipe = getRecipeForName(recipeSheet.getSheetValues(1, 1, 1000, 3), recipeName);
console.log(recipe);
calculatorSheet.getRange(7, 2, 10, 3).clearContent();
calculatorSheet.getRange(7, 2, recipe.length, 3).setValues(recipe);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment