简体   繁体   中英

Hide column based on column value

I would like to hide columns which have value 0 in say row 5.

I have tried the following code but it does not seem to work.

function myFunction() {
 
}
function onEdit(){
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var lastColumn = sheet.getLastColumn();
 
  for(i=1 ; i<=lastColumn ; i++){
    var status = sheet.getRange(5+i).getValue(); //change "5" to desired column
    if (status == "0"){//change "0" to desired text
      sheet.hideColumns(i);//hide row if true
    }  
    else{
      sheet.showColumns(i);//unhide row if false
    }
  }
}

This is the error

Exception: Range not found at onEdit(Copy of Best:10:24)

Any suggestions?

i guess you are not using get range function correctly.

getRange(a1Notation) -- As per doc this a1Notation is string like A1 , A2 ..

and you are passing a integer. or if you know column change it to (A or B as per need)

getRange( 'A' + (5 + i))

I guess you need to check other implementation of getRange that fullfill your need.

Please check

https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangea1notation

[EDIT]

function onEdit(){
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var lastColumn = sheet.getLastColumn();
 
  for(i=1 ; i<=lastColumn ; i++){
    var status = sheet.getRange(5,i).getValues(); //.getValue(); //change "5" to desired column
    if (status == "0"){//change "0" to desired text
      sheet.hideColumns(i);//hide row if true
    }  
    else{
      sheet.showColumns(i);//unhide row if false
    }
  }
}

this script will hide all the column in row 5 -- have values 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM