Search code examples
google-apps-scriptforeach

Function to hide/unhide non adjacent columns


I would like to create a function to hide/unhide non adjacent columns

I've tried that code (I4ve copied/paste from the forum) but as I don't understand what "hide=>" for example, I'm lost. Is it a variable?

Thanks if you could help me

function myFunction() {
  //list of columns that I want to hide/unhide
  const hideColumns = [0,1,3,7,15];

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const requests = [];
  
  // Create request for hiding/unhiding the hide columns.
  if (hideColumns.length > 0) {
    //unhidding hidden columns
    hideColumns.forEach(show=>{
    sheet.showColumn(sheet.getRange(1,show));
    sheet.getRange(11,1).activate();
  }
  }
  else {
    //hiddenning columns
    hideColumns.forEach(hide=>{
    sheet.hideColumn(sheet.getRange(1,hide));
    })
  }
}

Solution

  • When you use foreach loop for hideColumns array

    show=>
    

    Means the current element of array in loop.

    It means you give a name of that element in array for loop.

    Example;

        hideColumns.forEach(value=>{
      
          sheet.hideColumn(sheet.getRange(1,value)); // as you can see we used "value" in here to represent the elements in loop.
        })
    

    Other example;

    We have an array named "numbers" and there are numbers in it. We want to get all numbers and do something for everynumber such as printing or logging to console.

    numbers.forEach(number => console.log(number))
    

    You should look for arrays and loops to understand better.