0

So I'm pretty new when it comes to scripts. In the past I was able to get a small script to work, but that was an isolated event and it was mostly copy/pasting anyway. I'm working in a google sheets. Here's what I'm trying to accomplish:

I want my sheet to Hide/Show certain columns based on the content of a single cell, C3. I was able to find a script that should be working based on this post, but no matter what I do the script isn't affecting any changes to my sheet. Below is my modified version of the script. The main difference is that Omar only wanted 3 separate views, whereas I want 13, one for each calendar month and one that displays all. I'll include 3 cases for spaces sake.

function onEdit(e) {

var sheet = e.source.getActiveSheet();
if (e.range.getA1Notation() !== 'C3' || sheet.getName() !== 'Overview') return;
switch (e.value) {
case 'All':
    sheet.showColumns(1, sheet.getMaxColumns()-1)
    break;
case 'January':
    sheet.showColumns(1, 2, 3, 4, 5, 26, 27)
    sheet.hideColumns(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)
    break;
case 'February':
    sheet.showColumns(1, 2, 3, 4, 5, 6, 7)
    sheet.hideColumns(8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27)
    break;
}
}

I feel like the script should be working, and perhaps I missed some small implementation step. Any help?

Rubén
  • 34,714
  • 9
  • 70
  • 166

2 Answers2

0

Short answer

onEdit(e) appears that doesn't run but what is happening is that it has errors.

Explanation

The code use unsupported syntax for showColumns and hideColumns, so its execution is interrupted.

The syntax of showColumns is showColumns(columnIndex) or showColumns(columnIndex, numColumns). The syntax for hideColumns is similar. For details and examples see https://developers.google.com/apps-script/reference/spreadsheet/sheet

On How can I test a trigger function in GAS? it's explained how to debug trigger functions.

Cooper mention on his answer that he posted a code that do something similar about what you are trying to achieve and also alert about the restrictions of simple triggers. Note that hiding/showing rows/columns is slow and that a simple trigger has a 30 seconds execution time limit. The way to overcome this limit is to use an installable trigger, but maybe your case is simple enough to be solved without using an auxiliary function and installable trigger.

Here is a change that you could made for the January case.

case 'January':
    sheet.showColumns(1, 5);  // show columns from column A to column E
    sheet.showColumns(26, 2); // show columns from column Z to column AA
    sheet.hideColumns(6, 19);  // hide columns from column F to column X
    break;

I let you to figure out by yourself the February case.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Ok that did it, that was exactly what I needed. I was able to make it work for February as well as the rest of the calendar months. Thanks so much @rubén – Alexander Moore Dec 17 '17 at 03:16
0

I did something similar to this recently you can take a look at it here. Also hiding a lot of columns can take time so be aware of these restrictions.

function onEdit(e) {

var sheet = e.source.getActiveSheet();
if (e.range.getA1Notation() !== 'C3' || sheet.getName() !== 'Overview') return;
switch (e.value) {
case 'All':
    sheet.showColumns(1, sheet.getLastColumn()-1);
    break;
case 'January':
    //refer to my link above for creating a function like this
    break;
case 'February':
    //refer to link again
    break;
}
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks for the reply cooper. I'm afraid I have to say the same thing to your response that I did to Rubens, upon looking closely at the script you recommended for LJW in the other post, I'm not sure exactly what I should be putting into my script. There are a few different elements there that are just outside of my understanding. Would it be possible for you to show me an example of what you would put in the January case? That would give me enough info to fix my script, I think. Thanks again, and sorry for my ignorance. – Alexander Moore Dec 16 '17 at 23:37