Search and delete empty cells

54 views
Skip to first unread message

Alexey Tikhonov

unread,
Jan 21, 2021, 2:07:13 AM1/21/21
to TreeSheets
Hi, TreeSheets creator and users!

Could anybody suggest a way to search for empty sub-cells in a sheet? They often appear after operations Hierarchify/Flatten. I suspect that one could use a Lobster script for that but I'm a beginner in the language.

Alexey

Alexey Tikhonov

unread,
Jan 21, 2021, 4:00:35 AM1/21/21
to trees...@googlegroups.com
I played a little with a script that should just count void cells in a sheet but it makes TreeSheets stop instead.
Where did I make a mistake?

// =======================================================
// Count Void Cells.lobster

import std

// Number of the cells which are a single child of their parent
// and have no text or other subcells
var num_cells_void_cells = 0

// Checks whether the current cell has a _single_ empty subcell
def has_empty_child_cell():
    var result = false
    if ts_num_children() == 1:
        ts_goto_child(1)
        if ts_get_text() == "" and not ts_num_children():
            result = true
        // Go back
        ts_goto_parent()
    return result

// Recursively traverse the tree, counting void cells.
def count_void_cells():
    if has_empty_child_cell():
        num_cells_void_cells = num_cells_void_cells + 1
    else:
        for(ts_num_children()) i:
            ts_goto_child(i)
            count_void_cells()
            // count_void_cells always returns the focus to the parent
            // cell (see the line below) so we don't need to call ts_goto_parent
            // in this cycle
    // Activate the parent cell
    ts_goto_parent()

// Traverse the children of the root:
for(ts_num_children()) i:
    ts_goto_child(i)
    count_void_cells()

var msg = "No empty cells found"

if num_cells_void_cells:
    msg = "Found " + num_cells_void_cells + " empty cells."

ts_set_status_message(msg)
// =======================================================

image.pngimage.png

--
You received this message because you are subscribed to a topic in the Google Groups "TreeSheets" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/treesheets/9pS_lWL63wo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to treesheets+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/treesheets/8dae4943-fb34-4bdd-858c-36d8b746206bn%40googlegroups.com.
TreeSheets.exe.exp.log

Wouter van Oortmerssen

unread,
Jan 21, 2021, 12:48:15 PM1/21/21
to trees...@googlegroups.com
The crash you ran into was a stack overflow, and the script implementation wasn't handling that well in this case. Just fixed that, now it will tell you there was a stack overflow in the status bar.

The actual stack overflow in in ts_goto_child(1) which should have used index 0 instead. Because it is indexing out of range, it does NOT actually go to a child. But because there is still a goto parent happening after, it will now keep repeating some part of the tree forever.

So clearly what needs to happen is for it to give an actual error rather than silently ignoring your index. But I left that for a future fix. At least now you know :)

Tested it, and it now tells me I have N empty cells in the status bar (once I click in the doc again, another odd behavior).

I am not sure why for you these operations result in empty cells btw.. I have not had that happen.


You received this message because you are subscribed to the Google Groups "TreeSheets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to treesheets+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/treesheets/CAJEUK_unACEArSh-R1dLa5AmK6a6kxopbD0L2giFU1X4zXMBsg%40mail.gmail.com.

Alexey Tikhonov

unread,
Jan 22, 2021, 12:23:15 AM1/22/21
to trees...@googlegroups.com
Thanks, Wouter! It works!

I might have switched mentally to VBA mode when I wrote the script - I'm used to developing in languages where arrays and collections are 1-based (Fortran, Lua, VBA for Excel).

Wouter van Oortmerssen wrote:

I am not sure why for you these operations result in empty cells btw.. I have not had that happen.
 
TreeSheets produces hierarchies with empty cells as expected when I 'hierarchify' a not filled table - it is absolutely reasonable and expectable behaviour.  I just want sometimes beatify the resulting tree - by removing the void cells (I know it damages the hierarchical relationships), or by putting in some default text.

image.png
By the way, how could I make TreeSheets redraw from a Lobster script? I would expect something like ts_redraw() for this case. Currently, I see the modifications (like deleted cells) only when I, for example, fold/unfold the cell.

Alexey


Reply all
Reply to author
Forward
0 new messages