Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * When a checkbox is checked, the function will write the value of the target cell divided by the
- * number of blank cells in the row to the unedited cells
- * @param - checkBoxColumn - the column number of the checkbox column
- * @returns the value of the cell in the range that is being edited.
- */
- function onEdit({ range }) {
- /* These are user defined constants that are used in the function. */
- const checkBoxColumn = 6;
- const targetCell = "G4";
- const rowStart = 2;
- const numCols = 3;
- /* This is a guard clause. It is checking to see if the column that was edited is the checkbox
- column. If it is not, then the function will return. */
- if (range.getColumn() != checkBoxColumn) return;
- /* This is getting the sheet that was edited, the value of the target cell, the range of the edited
- cells, and the values of the edited cells. */
- const sheet = range.getSheet();
- const targetValue = sheet.getRange(targetCell).getValue();
- const editedCells = range;
- const editedValues = editedCells.getValues();
- /* Checking to see if the edited cells contain a boolean value. If they do, it will get the values of
- the row, get the total of the values, get the number of blank cells, and then write the value to
- write in the blank cells. */
- try {
- validateInput(editedValues);
- if (editedValues[0][0] == true) {
- const rowValues = sheet
- .getRange(editedCells.getRow(), rowStart, 1, numCols)
- .getValues()[0];
- const totalValues = getTotalValues(rowValues);
- const numBlankCells = getnumBlankCells(rowValues);
- const valueToWrite = (targetValue - totalValues) / numBlankCells;
- writeNewValuesInNaNCells(sheet, rowValues, editedCells, valueToWrite);
- editedCells.setValue(false);
- }
- } catch (error) {
- console.error(error);
- }
- }
- /**
- * The function throws an error if the edited cells do not contain a boolean value.
- * @param editedValues - The values that the user has entered into the cells.
- */
- function validateInput(editedValues) {
- if (typeof editedValues[0][0] != "boolean") {
- throw new Error(`The edited cells must contain a boolean value.`);
- }
- }
- /**
- * It takes an array of numbers and returns the sum of all the numbers in the array.
- * @param rowValues - The values of the row.
- * @returns The total of all the values in the rowValues array.
- */
- function getTotalValues(rowValues) {
- return rowValues.reduce(
- (acc, val) => (!isNaN(val) && typeof val === "number" ? acc + val : acc),
- 0
- );
- }
- /**
- * It returns the number of blank cells in a row.
- * @param rowValues - The values in the row.
- * @returns The number of blank cells in the row.
- */
- function getnumBlankCells(rowValues) {
- return rowValues.filter((value) => isNaN(value) || typeof value !== "number")
- .length;
- }
- /**
- * It takes a sheet, a row of values, the edited cells, and a value to write. It then checks if the
- * values in the row are NaN, and if they are, it writes the value to write in the corresponding cells
- * @param sheet - the sheet object
- * @param rowValues - The values of the row that was edited.
- * @param editedCells - The range of cells that were edited.
- * @param valueToWrite - The value to write in the NaN cells.
- */
- function writeNewValuesInNaNCells(sheet, rowValues, editedCells, valueToWrite) {
- const isNaNCell = rowValues.map((value) => isNaN(parseFloat(value)));
- const nanCols = isNaNCell
- .map((val, idx) => (val ? idx : undefined))
- .filter((val) => val !== undefined);
- nanCols.forEach((col) =>
- sheet.getRange(editedCells.getRow(), col + 2).setValue(valueToWrite)
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment