IAmMoonie

On box tick, calculate row

Jan 10th, 2023
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.61 KB | Source Code | 0 0
  1. /**
  2.  * When a checkbox is checked, the function will write the value of the target cell divided by the
  3.  * number of blank cells in the row to the unedited cells
  4.  * @param  - checkBoxColumn - the column number of the checkbox column
  5.  * @returns the value of the cell in the range that is being edited.
  6.  */
  7. function onEdit({ range }) {
  8.   /* These are user defined constants that are used in the function. */
  9.   const checkBoxColumn = 6;
  10.   const targetCell = "G4";
  11.   const rowStart = 2;
  12.   const numCols = 3;
  13.  
  14.   /* This is a guard clause. It is checking to see if the column that was edited is the checkbox
  15.   column. If it is not, then the function will return. */
  16.   if (range.getColumn() != checkBoxColumn) return;
  17.  
  18.   /* This is getting the sheet that was edited, the value of the target cell, the range of the edited
  19.   cells, and the values of the edited cells. */
  20.   const sheet = range.getSheet();
  21.   const targetValue = sheet.getRange(targetCell).getValue();
  22.   const editedCells = range;
  23.   const editedValues = editedCells.getValues();
  24.  
  25.   /* Checking to see if the edited cells contain a boolean value. If they do, it will get the values of
  26.   the row, get the total of the values, get the number of blank cells, and then write the value to
  27.   write in the blank cells. */
  28.   try {
  29.     validateInput(editedValues);
  30.     if (editedValues[0][0] == true) {
  31.       const rowValues = sheet
  32.         .getRange(editedCells.getRow(), rowStart, 1, numCols)
  33.         .getValues()[0];
  34.       const totalValues = getTotalValues(rowValues);
  35.       const numBlankCells = getnumBlankCells(rowValues);
  36.       const valueToWrite = (targetValue - totalValues) / numBlankCells;
  37.       writeNewValuesInNaNCells(sheet, rowValues, editedCells, valueToWrite);
  38.       editedCells.setValue(false);
  39.     }
  40.   } catch (error) {
  41.     console.error(error);
  42.   }
  43. }
  44.  
  45. /**
  46.  * The function throws an error if the edited cells do not contain a boolean value.
  47.  * @param editedValues - The values that the user has entered into the cells.
  48.  */
  49. function validateInput(editedValues) {
  50.   if (typeof editedValues[0][0] != "boolean") {
  51.     throw new Error(`The edited cells must contain a boolean value.`);
  52.   }
  53. }
  54.  
  55. /**
  56.  * It takes an array of numbers and returns the sum of all the numbers in the array.
  57.  * @param rowValues - The values of the row.
  58.  * @returns The total of all the values in the rowValues array.
  59.  */
  60. function getTotalValues(rowValues) {
  61.   return rowValues.reduce(
  62.     (acc, val) => (!isNaN(val) && typeof val === "number" ? acc + val : acc),
  63.     0
  64.   );
  65. }
  66.  
  67. /**
  68.  * It returns the number of blank cells in a row.
  69.  * @param rowValues - The values in the row.
  70.  * @returns The number of blank cells in the row.
  71.  */
  72. function getnumBlankCells(rowValues) {
  73.   return rowValues.filter((value) => isNaN(value) || typeof value !== "number")
  74.     .length;
  75. }
  76.  
  77. /**
  78.  * It takes a sheet, a row of values, the edited cells, and a value to write. It then checks if the
  79.  * values in the row are NaN, and if they are, it writes the value to write in the corresponding cells
  80.  * @param sheet - the sheet object
  81.  * @param rowValues - The values of the row that was edited.
  82.  * @param editedCells - The range of cells that were edited.
  83.  * @param valueToWrite - The value to write in the NaN cells.
  84.  */
  85. function writeNewValuesInNaNCells(sheet, rowValues, editedCells, valueToWrite) {
  86.   const isNaNCell = rowValues.map((value) => isNaN(parseFloat(value)));
  87.   const nanCols = isNaNCell
  88.     .map((val, idx) => (val ? idx : undefined))
  89.     .filter((val) => val !== undefined);
  90.   nanCols.forEach((col) =>
  91.     sheet.getRange(editedCells.getRow(), col + 2).setValue(valueToWrite)
  92.   );
  93. }
Advertisement
Add Comment
Please, Sign In to add comment