onEdit() to exclude header row

43 Views Asked by At

I've been reading through various posts on how to exclude the header row from my onEdit(). I have read examples of offset() and I even use shift() in my other functions. This particular one is to generate a checkbox when a range in a row has value.

I have tried the following, separately:

checkboxCell.offset(1,0)

checkboxCell.shift()

These end up as failures with messages saying they are not functions.

Could someone guide me as to how this can be done?

function onEdit(e) {
  let sheet = e.source.getActiveSheet();
  const checkboxCell = sheet.getRange(e.range.rowStart, 1);
  let editedRow = sheet.getRange(e.range.rowStart, 2, 1, 3).getValues().flat();
  editedRow = editedRow.filter(x => x != '');
  if (editedRow.length == 3) {
    checkboxCell.insertCheckboxes();
  } else {
    checkboxCell.clearDataValidations();
    checkboxCell.clearContent();
  }
}
1

There are 1 best solutions below

1
Century Tuna On BEST ANSWER

Solution:

Add another conditional statement to restrict adding a checkbox if the e.range.rowStart is set to 1 (which is the first row)

Full code:

function onEdit(e) {

  let sheet = e.source.getActiveSheet();
  const checkboxCell = sheet.getRange(e.range.rowStart, 1);
  let editedRow = sheet.getRange(e.range.rowStart, 2, 1, 3).getValues().flat();
  editedRow = editedRow.filter(x => x != '');
  
  if (editedRow.length == 3 && e.range.rowStart != 1) {
    checkboxCell.insertCheckboxes();
  } else {
    checkboxCell.clearDataValidations();
    checkboxCell.clearContent();
  }

}

Modification:

from (editedRow.length == 3) to (editedRow.length == 3 && e.range.rowStart != 1)

Output:

enter image description here