Home > Software design >  I am trying to apply a script to the rest of the columns in the google sheet. Goal is that anytime a
I am trying to apply a script to the rest of the columns in the google sheet. Goal is that anytime a

Time:11-06

At the moment I am using the following code so that every time a check box is marked off in a column then the whole row is hidden. At the moment the script I have only works for one column and not for any other column. I would like the script to work for any column I choose. I am using Google Sheets.

function onEdit(e){
if (e.range.columnStart != 6 || e.value != "TRUE") return;
SpreadsheetApp.getActiveSheet().hideRows(e.range.rowStart);
 }

CodePudding user response:

I believe your goal is as follows.

  • You want to hide the row when the checkbox is checked. In this case, the checkboxes are put not only in the column "F" but also in other columns. You want to use the other checkboxes.

In this case, how about the following modification?

Modified script:

This modified script uses the checkboxes of all columns.

function onEdit(e) {
  const {range} = e;
  if (!range.isChecked()) return;
  range.getSheet().hideRows(range.rowStart);
}

This modified script uses the checkboxes of the columns "B", "D" and "F".

function onEdit(e) {
  const columns = [2, 4, 6]; // In this case, the columns "B", "D" and "F".

  const {range} = e;
  if (!columns.includes(range.columnStart) || !range.isChecked()) return;
  range.getSheet().hideRows(range.rowStart);
}

  • When you use this script, please check the checkboxes. By this, the script is run by a simple trigger. When you directly run the script, an error occurs. Please be careful about this.

CodePudding user response:

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == 'Sheet0' && e.range.columnStart == 6 && e.value == "TRUE") {
    sh.hideRows(e.range.rowStart)
  }
}
  • Related