Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onEdit({ range }) {
- // Define user-defined variables so they can easily be edited
- let rows = [2, 3, 4]; // array of rows to process
- let columns = [2, 3, 4]; // array of columns to process
- let targetCell = "G4"; // target cell to
- // Get the edited cell and its value
- let editedCell = range;
- let editedColumn = editedCell.getColumn();
- let editedRow = editedCell.getRow();
- let editedValue = editedCell.getValue();
- // Check if edited cell is within the specified rows and columns
- if (!rows.includes(editedRow) || !columns.includes(editedColumn)) {
- return; // exit if not in the correct range
- }
- try {
- // Validate that the edited cell contains a numeric value
- if (isNaN(parseFloat(editedValue))) {
- throw new Error(`The edited cell must contain a numeric value.`);
- }
- // Get the values of the row that was edited
- let rowValues = editedCell
- .getSheet()
- .getRange(editedRow, columns[0], 1, columns.length)
- .getValues()[0];
- // Calculate the price minus the value of the edited cell
- let x = editedCell.getSheet().getRange(targetCell).getValue() - editedValue;
- // Calculate the number of cells in the row that were not edited
- let numUneditedCells = rowValues.length - 1;
- // Calculate the value to be written to the unedited cells
- let valueToWrite = x / numUneditedCells;
- // Write the value to the unedited cells
- for (let i = 0; i < rowValues.length; i++) {
- if (rowValues[i] !== editedValue) {
- editedCell
- .getSheet()
- .getRange(editedRow, columns[i])
- .setValue(valueToWrite);
- }
- }
- } catch (error) {
- console.error(error);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment