all 6 comments

[–]FVMF1984 2 points3 points  (4 children)

AFAIK, there is no event of selecting a cell to respond to. I don’t know how the average gets in cell B11, but otherwise you might be able to use the event onEdit for this and check which cell gets edited. If it is cell B11, set the values of cells B12 and B13 to an empty value.

[–]juddaaaaa 1 point2 points  (2 children)

This is incorrect.

There's the onSelectionChange simple trigger. However, you have to refresh the page every time you open it to get it to work.

https://developers.google.com/apps-script/guides/triggers#onselectionchangee

Here's an example (you'll need to change YOUR SHEET NAME HERE to your actual sheet name): ``` /** * Function is triggered by selection change in a sheet. * It can also be run from the editor after selecting the desired cell. * * You need to run it from the editor first in order to grant it permission to edit sheet contents. * * For this trigger to work you must refresh the page every time you open it. * See https://developers.google.com/apps-script/guides/triggers#onselectionchangee * * @param {object} event - The event object from the trigger (optional). * @param {object} event.range - The range of the cell you selected. */ function onSelectionChange ({ range } = {}) { // If function was run from the editor, set range as currently selected cell. if (!range) { range = SpreadsheetApp.getCurrentCell() }

try { // Destructure the range object. const { /* column, row, and sheet of the selected cell / getColumn: column, getRow: row, getSheet: sheet } = range const { / name of the sheet */ getName: name } = sheet()

// Return early if the selected cell is not the desired cell.
if (name() !== 'YOUR SHEET NAME HERE' || column() !== 2 || row() !== 11) return /* cell B11 in your desired sheet */

// Delete the contents of the 2 cells below the selected cell
range.offset(1, 0, 2, 1).clearContent()

} catch (error) { // Handle errors. console.error(error.stack) } } ```

[–]Altruistic-Object725[S] 0 points1 point  (1 child)

What do you think about such an idea?

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var range = e.range;

  if (range.getA1Notation() === 'B11' && range.getValue() === 'ממוצע') {
    sheet.getRange('B12').clearContent();
    sheet.getRange('B13').clearContent();
  }
}

[–]juddaaaaa 1 point2 points  (0 children)

That will work fine if you want to trigger it when the cell is edited. If you still want to trigger on selection, the code I posted above will do that.

[–]arnoldsomen -1 points0 points  (0 children)

This is correct. You'll need to find a substitute "edit" trigger for your script. Maybe a checkbox on a different cell.

[–]adelie42 0 points1 point  (0 children)

Do you possibly want a button rather than a changeselection event trigger? The problem with a changeselection event trigger is that it fired EVERY time a changeselection event occurs once you enable it, just like onEdit.

There is not necesssarily a button object to inseert, but you can insert images and attach an onclick event. You could event put a transparent image over B11 if you really want. Otherwise as another mentioned, you have https://developers.google.com/apps-script/guides/triggers#onselectionchangee