I get #NUM! error if I edit the cells in row 2 and 3. Error will be in the corresponding row below that certain edited cell.
Here is the script:
Main:
function onEdit(e) {
doFirstThing_(e);
doSecondThing_(e);
}
Then the first function
// Define the original quantities of the ingredients for the recipe
var originalQuantitiesFirst = [1, 1, 2, 2, 1, 1]; // Couscous, Teff, Fig, Date, Hamburg
function doFirstThing_(e) {
// Get the active spreadsheet
var spreadsheet = e.source;
// Get the sheet named 'Valencia'
var sheet = spreadsheet.getSheetByName('Valencia');
// Check if the edited cell is in the 'Quantity' column (Column B, D, F, H, or J in this case)
if (["B3", "D3", "F3", "H3", "J3", "F2"].includes(e.range.getA1Notation())) {
// Get the new quantity
var newQuantity = e.value;
// Log the new quantity
console.log('New quantity in doFirstThing_: ' + newQuantity);
// Calculate the ratio of new quantity to original quantity of the changed ingredient
var ingredientIndex = ["B3", "D3", "F3", "H3", "J3", "F2"].indexOf(e.range.getA1Notation());
var ratio = newQuantity / originalQuantitiesFirst[ingredientIndex];
// Define the cells with the quantities
var quantityCells = ["B3", "D3", "F3", "H3", "J3", "F2"];
// Loop through each quantity cell
for (var i = 0; i < quantityCells.length; i++) {
// Update the quantity based on the ratio and the original quantity
var newQuantity = originalQuantitiesFirst[i] * ratio;
// Update the cell with the new quantity
sheet.getRange(quantityCells[i]).setValue(newQuantity);
}
}
}
Last function:
// Define the original quantities of the ingredients for all recipes
var originalQuantities = [[1, 3, 6, 4, 1], [1, 1, 3, 1, 1], [5, 3, 2, 3, 1], [4, 5, 1, 2, 1], [3, 2, 4, 4, 1]]; // Stew, Nutmeg, Dough, Vegetables, Bread, Scorpion, Sauce, Stew, Fig, Sugar, Oil, Dough, Agent, Date, Sugar, Liquor, Nutmeg, Pickle, Meat, Bread
function doSecondThing_(e) {
// Get the active sheet
var sheet = e.source.getActiveSheet();
// Check if the edited cell is in the 'Quantity' column (Column B, D, F, H, or J in this case)
// and the sheet is 'Valencia'
if ([2, 4, 6, 8, 10].includes(e.range.columnStart) && sheet.getName() == 'Valencia') {
// Get the new quantity
var newQuantity = e.value;
// Log the new quantity
console.log('New quantity in doSecondThing_: ' + newQuantity);
// Calculate the ratio of new quantity to original quantity of the changed ingredient
var recipeIndex = Math.floor(e.range.columnStart / 2) - 1;
var ratio = newQuantity / originalQuantities[recipeIndex][e.range.rowStart - 4]; // Adjusted for the new row start
// Get the range of cells with the quantities
var startColumn = String.fromCharCode(66 + recipeIndex * 2); // 66 is the ASCII value for 'B'
var quantityRange = sheet.getRange(startColumn + "4:" + startColumn + "8"); // Adjusted for the new row start and end
// Get the values of the cells in the range
var quantities = quantityRange.getValues();
// Loop through each quantity
for (var i = 0; i < quantities.length; i++) {
// Update the quantity based on the ratio and the original quantity
quantities[i][0] = originalQuantities[recipeIndex][i] * ratio;
}
// Update the cells in the range with the new quantities
quantityRange.setValues(quantities);
}
}
If I edit the cells from row 4 and below, no error on top of that cell. I tried logging it but the error is
TypeError: Cannot read properties of undefined (reading
‘getA1Notation’)
at doFirstThing_(Script1:12:61) at onEdit(Main:2:3)