Hide multiple columns at once using ColumnDimension

Issue #914 new
Sverker Sjöberg created an issue

I'm working on a project where I'm creating sheets containing a single DataTable and I am hiding all excessive (unused) columns. I do this by looping over each column and hiding them one by one. Unfortunately this produces one xml entry per column like

<col min="7" max="7" hidden="1"/>

<col min="8" max="8" hidden="1"/>

<col min="9" max="9" hidden="1"/>

Is there any reason not to use a ColumnDimension to hide all columns and create an xml entry like this.

<col width="0" max="16384" min="7" hidden="1"/>

This could be implemented by adding the method "hide" to the DimensionHolder

    def hide(self, start, end):
        if end is None:
            end = start

        new_dim = self[start]
        new_dim.hidden = True

        work_sequence = get_column_interval(start, end)[1:]
        for column_letter in work_sequence:
            if column_letter in self:
                del self[column_letter]
        new_dim.min, new_dim.max = map(column_index_from_string, (start, end))

It seems to work fine in my environment.

Awesome package, Sverker

Comments (4)

  1. Sverker Sjöberg reporter

    And using outline_level = 0? It seems to achieve the same result. Great, thank you! In that case a alias and or a clarification of the docs maybe helpful.

        def hide(self, start, end, hidden=True):
           self.group(start, end, outline_level=0, hidden=hidden)
    
  2. CharlieC

    Additions to the documentation are always welcome…

    This isn't a feature I use myself so I'm not that familiar with it.

  3. Sverker Sjöberg reporter

    Ah, when setting a outline_level excel displays the grouped columns with a toggle allowing you to easily hide and unhide them. This is useful in many cases but since the toggle takes up significant screen space you do not want it when you dont intend to use them. (The toggling functionallity also works poorly when sheets are protected).

    Excel presents grouping (toggle) and hiding as two different functions, which is why i think it may be merited to highlight the difference in the python interface. I'll put writing something for the docs on my todo list.

    In the image below columns I-K are grouped while columns M-O are hidden.

    Capture.PNG

  4. Log in to comment