2

UPDATE: I FIGURED IT OUT!!!! YEY!!!!

Im trying to have certain columns in my Google Sheet hide or unhide, depending on some conditions. I realized that this is difficult. But I've managed (I think) to come up with a way to make it work. The set up is, that when certain text in column A appears, I have a formula in another cell (a Heading cell) in another column, column B, that catches the text, and if it's Matched, then text appears (or stays blank if it's not matched).

Next, I have the following code, that reads that cell in column B, and if that cell is NOT BLANK, then the code UNHIDES the column (B); if the cell is BLANK, then the code HIDES the cell.

This works fine. However, it doesn't work in Order, and for some reason, for example, if the the code executes and UNHIDES the column, when running it again, it doesn't HIDE the column (the ELSE Statement)...it only hides it if I run the code from the Script Editor itself...by pressing the Play button...

This is my code:

FIGURED OUT CODE: Hides or Unhides columns based on conditions in cells

function onEdit(){
  var sheet = SpreadsheetApp.getActiveSheet().getSheetName()
  if(sheet === '🧩 CASE ASSEMBLY SHEET') {


 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
 var cell = sheet.getRange('bh6');
 var cellContent = cell.getValue();

 if(cellContent === "") {
     var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
  spreadsheet.hideColumns(60, 1);
}
  else {
    if(cellContent !== "") {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
     spreadsheet.showColumns(60, 1);
    }
}

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
 var cell = sheet.getRange('bi6');
 var cellContentt = cell.getValue();

 if(cellContentt === "") {
     var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
  spreadsheet.hideColumns(61, 1);
}
  else {
    if(cellContentt !== "") {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('🧩 CASE ASSEMBLY SHEET');
     spreadsheet.showColumns(61, 1);
    }
}
  • Welcome. The question is unclear as the it doesn't mention which function is being called and how it's done. Also it' looks that it has a lot functions without any inline documentation so it's hard to follow. Please replace the code by a minimal complete example. – Rubén Oct 29 at 22:02
  • 1
    I edited the code example above. I just repeated the above function and renamed each one, because I have multiple columns I'd like to hide and unhide independently. – Arthur Futoryan Oct 29 at 23:28
  • 1
    It's great that you figured out how make your script does what you were looking for and that you come back to share your solution and joy, but, rather than as a edit to the question, the solution should be posted as an answer. – Rubén Oct 31 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.