Hide or unhide Header in schedule (Nested For Loop)

Hi,

Got 2 columns in schedule that I need to turn on or off, cant get it to work.

Error message:Index not defined

schedule = UnwrapElement(IN[0])
scheduleDefinition = schedule.Definition
countColumns = scheduleDefinition.GetFieldCount()

for i in range(countColumns):
    colName = ["Discipline", "Status"]
    colNameCount = len(colName)
    for y in range(colNameCount):
        scheduleColumns = scheduleDefinition.GetField(y).ColumnHeading
    if scheduleColumns == colName:
        index = y
    fields = schedule.Definition.GetField(index)
fields.IsHidden = IN[1]
print(countColumns)

you’re maybe having an error because the index variable cannot reach the y.

maybe check your indentation then try again

for y in range(colNameCount):
        scheduleColumns = scheduleDefinition.GetField(y).ColumnHeading
        if scheduleColumns == colName:
            index = y

Hi jmark,
That is correct, I’ve tried different indentations.
I dont have any programs like visual studio code or pycharm to help and correct the error.
Cant check if the nested for loop is correct…

Any help and suggestion how to fix it?

check this proper formatting

schedule = UnwrapElement(IN[0])
scheduleDefinition = schedule.Definition
countColumns = scheduleDefinition.GetFieldCount()

for i in range(countColumns):
    colName = ["Discipline", "Status"]
    colNameCount = len(colName)
    for y in range(colNameCount):
        scheduleColumns = scheduleDefinition.GetField(y).ColumnHeading
        if scheduleColumns in colName:
            index = y
            fields = schedule.Definition.GetField(index)
            fields.IsHidden = IN[1]
print(countColumns)

also I changed some line in the if statement of scheduleColumns, see if it’ll work

Hi,
I’ve adjusted the indention and gets no errors, but the code dont hide or unhide the columns.
Need your help, tanks in advance

schedule = UnwrapElement(IN[0])

scheduleDefinition = schedule.Definition
countColumns = scheduleDefinition.GetFieldCount()

for i in range(countColumns):
    colName = ["Sheet Number", "Current Revision"]
    colNameCount = len(colName)
    for y in range(colNameCount):
        scheduleColumns = scheduleDefinition.GetField(i).ColumnHeading
    if scheduleColumns == colName:
        index = i
        fields = schedule.Definition.GetField(index)
        fields.IsHidden = IN[1]
OUT = countColumns

Hi jmark,
I’ve tried the code, no errors occurs but nothing changes to field hidden field for columns

Can fields be reached, whats your opinion?

Hi, theoretically the IsHidden method can get hold of that, if it still doesn’t work then i guess we already hit a wall and let’s wait for the others to chime in, for the meantime have you tried to close and open the graph again after changing the script just to refresh it? or at least use the force run script from data-shapes.

1 Like

Is there someone who has a solution to this?!

Thanks!

Hello,

try this version

image

import clr
import sys
import System

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument


def hide_Field(paraName, scheduleDef, hide = True):
    fieldCount = scheduleDef.GetFieldCount()
    for idx_field in range(fieldCount):
        field = scheduleDef.GetField(idx_field)
        if field.GetName() == paraName or field.ColumnHeading == paraName:
            field.IsHidden = hide


lstParaName = IN[0]
schedule = UnwrapElement(IN[1])

scheduleDef = schedule.Definition

TransactionManager.Instance.EnsureInTransaction(doc)
for paraName in lstParaName:
    hide_Field(paraName, scheduleDef, True)
TransactionManager.Instance.TransactionTaskDone()
2 Likes

@c.poupin this is great, thank you so much!!!