A common pattern is traversing a tree, and you find yourself writing the same recursive code over an over. Although it's a very simple problem, people often have trouble with it. Here's a general pattern you can use.
Let's say you have some data that looks like this - a classic tree structure.
Typically you want to traverse this and do something at each node - using a pattern like this we can generalize ...
So in the case of our data we want to just log the name and depth of each tree node
which gives this result
Here's the traverseTree function, which you can copy from here or use directly from the cUseful library.
Many of the snippets in this section of the site are part of the cUseful library. You can find the details below.
You want to learn Google Apps Script?
Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >