Topic
  • 4 replies
  • Latest Post - ‏2014-04-01T17:38:06Z by CMusicFan
CMusicFan
CMusicFan
22 Posts

Pinned topic Deleting empty table rows

‏2014-03-31T15:24:45Z |

I am trying to delete empty rows in tables throughout a module. My code is below:

// Look for empty rows in tables
for obj in current Module do
{
    Object ro, ce, cellRef
        bool blank
        
        // Look for tables
        if(table(obj))
        {
                // If a table is found, look at each row
                for ro in table(obj) do
                {
                        // Look at each cell in the row
                        blank = true
                        for ce in row(ro) do
                        {
                                // If the current cell has text, set blank to false
                                if(ce."Object Text" "" != "")
                                {
                                        blank = false
                                }
                                else
                                {
                                        cellRef = ce
                                }
                        }
                        
                        // If blank is still true, the row is empty so we can delete it
                        if(blank)
                        {
                                deleteRow(cellRef)
                        }
                }
        }
}

My question is simple...what am I doing wrong here? it seems to me like it should work, and the script DOES run, but nothing seems to be different in the module. If you can help me, I would really appreciate it!

Thanks!

  • GregM_dxler
    GregM_dxler
    166 Posts

    Re: Deleting empty table rows

    ‏2014-03-31T16:00:55Z  

    Usually it is not a good idea to delete something inside a loop that is using it.

    So instead of deleting a row when looping through the rows, save it in a skip list and when you all down with the table, then deleted the rows in the skip list.

    Hope this helps,

    Greg

  • CMusicFan
    CMusicFan
    22 Posts

    Re: Deleting empty table rows

    ‏2014-03-31T16:25:35Z  

    Usually it is not a good idea to delete something inside a loop that is using it.

    So instead of deleting a row when looping through the rows, save it in a skip list and when you all down with the table, then deleted the rows in the skip list.

    Hope this helps,

    Greg

    That's a fair point. I had not considered that. I'll give it a shot and post how it turns out.

    Thanks

  • Tony_Goodman
    Tony_Goodman
    271 Posts

    Re: Deleting empty table rows

    ‏2014-04-01T09:02:06Z  

    To find table objects use "for o in document current Module do"

     

  • CMusicFan
    CMusicFan
    22 Posts

    Re: Deleting empty table rows

    ‏2014-04-01T17:38:06Z  

    To find table objects use "for o in document current Module do"

     

    Thank you both for your help! I have been able to get my script working. Here it is in full:

    // Search for empty table rows
    Skip rowsToDelete = create
    for o in document DocumentModule do
    {
            Object ro, ce, cellRef
            bool rowIsEmpty
            
            // Look for tables
            if(table(o))
            {
                    // If a table is found, look at each row
                    for ro in table(o) do
                    {
                            // Initialize rowIsEmpty
                            rowIsEmpty = true
                            
                            // Look at each cell in the row
                            for ce in row(ro) do
                            {
                                    // If the current cell has text, set blank to false
                                    if(ce."Object Text" "" != "")
                                    {
                                            rowIsEmpty = false
                                            break
                                    }
                            }
                                    
                            if(rowIsEmpty)
                            {
                                    put(rowsToDelete, number(ce), ce)
                            }
                    }
            }
                    
            // Increment the progress bar and the count variable
            progressStep ++count
    
            // Halt the script if the cancel button is pressed
            if (progressCancelled) HaltScript
    }
            
    // Delete the rows
    for o in rowsToDelete do
    {
            deleteRow o
    }
    delete(rowsToDelete)