Fixed issue with tke-hide-col plugin

This commit is contained in:
tke
2024-05-08 16:28:21 +02:00
parent 4a4ae0e95c
commit bb09183da6

View File

@@ -1,5 +1,5 @@
__version__ = "0.1.0" __version__ = "0.1.0"
__author__ = "Your Name <your.email@example.com>" __author__ = "Tabledevil <tabledevil@gmail.com>"
from visidata import Sheet, BaseSheet, vd from visidata import Sheet, BaseSheet, vd
@@ -10,26 +10,30 @@ def hide_empty_and_superfluous_cols(sheet):
Given a sheet, hides columns that are empty or superfluous (contain the same Given a sheet, hides columns that are empty or superfluous (contain the same
value in every row). value in every row).
""" """
# Create a dictionary to keep track of which columns to hide # Count how many columns were hidden
cols_to_hide = {} counter = 0
# Check each column for being empty or superfluous # Check each column for being empty or superfluous
for col in sheet.visibleCols: for col in sheet.visibleCols:
# If the column is not hidden
if col.width and col.width > 0:
# Check if the column is empty (all cells are empty or None) # Check if the column is empty (all cells are empty or None)
if all([col.getValue(row) in (None, "") for row in sheet.rows]): if all([col.getValue(row) in (None, "", "null") for row in sheet.rows]):
cols_to_hide[col] = True # Hide the column
col.width = 0
counter += 1
# Skip the next check for same values since all cells are empty or None
continue continue
# Check if the column is superfluous (contains the same value in every row) # 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")]) # collect all values in the column as strings
values = set([f'{col.getValue(row)}' for row in sheet.rows if col.getValue(row) not in (None, "", "null")])
# If there is only one value in the column
if len(values) == 1: if len(values) == 1:
cols_to_hide[col] = True # Hide the column
col.width = 0
counter += 1
# Hide the columns that were flagged vd.status(f"Hid {counter} empty/superfluous columns.")
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: else:
vd.status("No empty/superfluous columns to hide.") vd.status("No empty/superfluous columns to hide.")