google apps script - How to use .findElement(DocumentApp.ElementType.TABLE_OF_CONTENTS) to get and parse a Document's Table of Contents Element -
my goal parse tableofcontents element in google document , write one. want every document in folder. having gone bother of converting each document type generated docslist can use method [ document generated documentapp not have. why, don't understand, because otherwise 2 'documents' similar when comes finding parts. ], find searchresult. how elusive construction used? i've tried converting tableofcontents element [ ele = searchresult.astableofcontents() ], not error out, nothing allows me parse through child elements recover text works. interestingly enough, if tableofcontents element parsing through document's paragraphs it, let's parse toc.
would speak question. sure appreciate code snippet because i'm getting nowhere, , have put hours this.
the astableofcontents()
method there editor's autocomplete function. has no run-time impact, , cannot used cast different type. (see containerelement documentation.)
to parse table of contents, start retrieving element searchresult. below example goes through items in document's table of contents produce array of item information.
example document
parsing results
on simple document few headings , table of contents, here's produced:
[13-08-20 16:31:56:415 edt] [ {text=heading 1.0, linkurl=#heading=h.50tkhklducwk, indentfirstline=18.0, indentstart=18.0}, {text=heading 1.1, linkurl=#heading=h.ugj69zpoikat, indentfirstline=36.0, indentstart=36.0}, {text=heading 1.2, linkurl=#heading=h.xb0y0mu59rag, indentfirstline=36.0, indentstart=36.0}, {text=heading 2.0, linkurl=#heading=h.gebx44eft4kq, indentfirstline=18.0, indentstart=18.0} ]
code
function test_parsetoc() { var fileid = '--doc-id--'; logger.log( parsetoc( fileid ) ); } function parsetoc( docid ) { var contents = []; var doc = documentapp.openbyid(docid); // define search parameters. var searchelement = doc.getbody(); var searchtype = documentapp.elementtype.table_of_contents; // search toc. assume there's one. var searchresult = searchelement.findelement(searchtype); if (searchresult) { // toc found var toc = searchresult.getelement().astableofcontents(); // parse entries in toc. toc contains child paragraph elements, // , each of has child text element. attributes of both // paragraph , text combine make toc item functional. var numchildren = toc.getnumchildren(); (var i=0; < numchildren; i++) { var iteminfo = {} var tocitem = toc.getchild(i).asparagraph(); var tocitemattrs = tocitem.getattributes(); var tocitemtext = tocitem.getchild(0).astext(); // set iteminfo attributes toc item, first paragraph iteminfo.text = tocitem.gettext(); // displayed text iteminfo.indentstart = tocitem.getindentstart(); // toc indentation iteminfo.indentfirstline = tocitem.getindentfirstline(); // ... child text iteminfo.linkurl = tocitemtext.getlinkurl(); // url link in document contents.push(iteminfo); } } // return array of objects containing toc info return contents; }
bad news
the bad news limited in can table of contents script. cannot insert toc or add new items existing one.
see issue 2502 in issue tracker, , star updates.
if can post code or explain issue docslist vs documentapp, looked at. elements of google document can manipulated via documentapp.
Comments
Post a Comment