Reputation: 91931
I'm using Google Apps Scripting to iterate over a bunch of elements in a Google Doc. I start with a single Element
and I want to recurse over all its children with code similar to the following:
var elements = [rootElement];
while (elements.length > 0) {
var element = elements.pop();
// TODO: process element
if (isContainerElement(element)) {
for (var i = 0; i < element.getNumChildren(); i++) {
elements.push(element.getChild(i));
}
}
}
I don't know how to implement isContainerElement
, though.
If I try the following:
function isContainerElement(element) {
return element instanceof DocumentApp.ContainerElement
}
I get this error:
TypeError: Cannot use instanceof on a non-object.
How can I find out if an Element
is a ContainerElement
?
Upvotes: 1
Views: 301
Reputation: 91931
There are probably better and more robust ways to do this, but this works:
function isContainerElement(element) {
return element.getNumChildren !== undefined;
}
Upvotes: 1