Home > Software engineering >  2 script hide columns based a cell value
2 script hide columns based a cell value

Time:10-13

how to make this script works on different sheet with different trigger? simple is make same script run twice

original script works on many sheets and hiding many columns on the 2nd script i make it simple, and reverse function. with current condition, original script works fine, and 2nd script doesnt work

Script Source


function onEdit() {
  onEdit1();
  onEdit2();
}


function onEdit1() // Original Script
{

var ss = SpreadsheetApp.getActiveSpreadsheet(); 

["Calc", "Sheet1", "Sheet2", "Sheet3","Sheet4"].forEach(function (s) { var sheet = ss.getSheetByName(s); /// works on many sheets

  var st =sheet.getRange("B2").getValue(); //trigger cell
  
  if(st == false) //trigger word
  {
      sheet.hideColumn(sheet.getRange("W:Z")); 
      sheet.hideColumn(sheet.getRange("AA:AK"));
      sheet.hideColumn(sheet.getRange("AL:AN"));
      sheet.hideColumn(sheet.getRange("AO:AO"));
      sheet.hideColumn(sheet.getRange("AP:AP"));

  }

  else
  {
      sheet.unhideColumn(sheet.getRange("W:Z"));
      sheet.unhideColumn(sheet.getRange("A:K"));
      sheet.unhideColumn(sheet.getRange("AL:AN"));
      sheet.unhideColumn(sheet.getRange("AO:AO"));
      sheet.unhideColumn(sheet.getRange("AP:AP"));  
  }

})}


function onEdit2() /// 2nd script
{

var ss = SpreadsheetApp.getActiveSpreadsheet(); 

["Duo","Sheet1","Sheet2"].forEach(function (s) { var sheet = ss.getSheetByName(s); /// bisa di banyak sheet

  var st =sheet.getRange("AC5").getValue(); //trigger cell
  
  if(st == True) //trigger word
  {
      sheet.unhideColumn(sheet.getRange("AU:BH")); // i Reverse hide unhide
    
  }

  else
  {
      sheet.hideColumn(sheet.getRange("AU:BH")); //here too
  
  }

})}

CodePudding user response:

Issue:

In JavaScript (and therefore, in Apps Script), the boolean values are true and false.

You are using True instead, with capital T, so most probably it's throwing an error because it's undefined (you can take a look at the Executions tab in your script in order to check this).

Solution:

In your onEdit2 function, change this:

if(st == True) //trigger word

To this:

if(st == true) //trigger word

Reference:

  • Related