GetCellText – Contents of Schedule Cells in 2014

In 2013 we could read the contents of a schedule through a bit of a workaround – export the schedule to a text file with ViewSchedule.Export and read the file back in with StreamReader.

Now in 2014 we can read the cell values directly with ViewSchedule.GetCellText as shown below with a macro that reads the schedule and shows the data in a TaskDialog.

GetCellText
publicvoid Schedule2014GetCellText()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    ViewSchedule viewSchedule = doc.ActiveView as ViewSchedule;
    SectionType sectionType = SectionType.Body;

    string data = "";
    for (int i = 0; i < getRowColumnCount(viewSchedule, sectionType, true); i++)
    {
        for (int j = 0; j <  getRowColumnCount(viewSchedule, sectionType, false); j++)
        {
            data += viewSchedule.GetCellText(sectionType, i, j) + ",";
        }
        // remove the trailing "," after the last cell and add a newline for the end of this row
        data = data.TrimEnd(',') + "\n";
    }
    TaskDialog.Show(viewSchedule.Name, data);
}

private int getRowColumnCount(ViewSchedule view, SectionType sectionType, bool countRows)
{
    int ctr = 1;
    // loop over the columns of the schedule
    while (true)
    {
        try // GetCellText will throw an ArgumentException is the cell does not exist
        {
            if (countRows)
                view.GetCellText(sectionType, ctr, 1);
            else
                view.GetCellText(sectionType, 1, ctr);
            ctr++;
        }
        catch (Autodesk.Revit.Exceptions.ArgumentException)
        {
            return ctr;
        }
    }
}

13 thoughts on “GetCellText – Contents of Schedule Cells in 2014

  1. Nice. So it looks like you can also use SetCellText so that you could capitalize the text in the cell which I need.

  2. I’m reeeeeaaally new to all of this, but I’m having a blast. Your Udemy class in great (so far) and I’m learning more everyday. Lessons have a place, but I feel like I learn the most when I wander off the reservation, so to speak.

    I pared down the getCellText to just get a single hard coded cell:
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    ViewSchedule viewSchedule = doc.ActiveView as ViewSchedule;
    SectionType sectionType = SectionType.Body;

    string data = “”;
    {
    data += viewSchedule.GetCellText(sectionType, 0, 0);
    }
    TaskDialog.Show(viewSchedule.Name, data);
    }

    That done, I tried to use the above example as a jumping off point to get the setCellText to work. I know it’s wrong, and I think I know why it’s wrong, I just have no idea which direction to go next.
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    TableSectionData tsd = new TableSectionData();
    //ViewSchedule viewSchedule = doc.ActiveView as ViewSchedule;
    SectionType sectionType = SectionType.Body;

    //viewSchedule.GetCellText(sectionType, 0, 0);

    tsd.SetCellText(0,0,”HELLO WORLD”);

    }

    The error I get is:
    The type ‘Autodesk.Revit.DB.TableSectionData’ has no constructors defined (CS0143)

    I’m pretty sure this is because the TableSectionData isn’t a “child of” UIDocument. Is that correct?

  3. I see it’s late and I gave the reason I thought TableSectionData tsd = doc.ActiveView as UIDocument)

    In this case I know I don’t have the constructors defined, I just can’t find what they would be. I also know I could be waaaay off base, so I figured I’d ask for some guidance.

    Anyway, night all.

    Thanks again for the creating this site and making all of this so accessible.

  4. Hi Harry,
    I have tried the above code in Revit 2017 and it is working fine without any issues. But it is taking long time to get the desired output (10 to 15 seconds). Is there any alternate way to get the values of schedule data in C#?
    Thanks in advance. – Philip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s