Files
gists/config/visidataplugins/hidecol.py
2023-05-04 08:09:27 +02:00

38 lines
1.2 KiB
Python

__version__ = "0.1.0"
__author__ = "Your Name <your.email@example.com>"
from visidata import Sheet, BaseSheet, vd
@Sheet.api
def hide_empty_and_superfluous_cols(sheet):
"""
Given a sheet, hides columns that are empty or superfluous (contain the same
value in every row).
"""
# Create a dictionary to keep track of which columns to hide
cols_to_hide = {}
# Check each column for being empty or superfluous
for col in sheet.visibleCols:
# Check if the column is empty (all cells are empty or None)
if all([col.getValue(row) in (None, "") for row in sheet.rows]):
cols_to_hide[col] = True
continue
# Check if the column is superfluous (contains the same value in every row)
values = set([col.getValue(row) for row in sheet.rows if col.getValue(row) not in (None, "", "null")])
if len(values) == 1:
cols_to_hide[col] = True
# Hide the columns that were flagged
if cols_to_hide:
for col in cols_to_hide:
col.width=0
vd.status(f"Hid {len(cols_to_hide)} empty/superfluous columns.")
else:
vd.status("No empty/superfluous columns to hide.")
Sheet.addCommand(None, "tke-hidecol", "sheet.hide_empty_and_superfluous_cols()")