2

I've a word document which I convert to Google Docs, but after the conversion as expected table of content break down in google docs, with no links to headers.

So I am trying to build the script to loop through the TOC and search the appropriate header in doc and change the style to "Header", while restoring the font size and style as it is.,

But it is not working, as when script runs it change style of TOC to header, instead of actual header, I want to exclude the TOC.

Here is the script:-

function toc() {
     const body = DocumentApp.getActiveDocument().getBody()
     const toc = body.findElement(DocumentApp.ElementType.TABLE_OF_CONTENTS).getElement().asTableOfContents()
     for(var i =  0 ; i < toc.getNumChildren() ; i++)
     {
        var tocElem = toc.getChild(i)
        var text = tocElem.getText()
        text = text.replace(/[\d\.]/g, "").trim() //.replace(/[\d\.]+$/, '')removing pagination of TOC
        console.log(text)
       if(text)
       {
         try{
        var elem = body.editAsText().findText(text)
        
        elem = elem.getElement().asText()
        console.log(elem)
        var font = elem.getFontFamily()
        var fontSize = elem.getFontSize()
        var style = {};
        style[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING6;
        var found =   body.findText(text)
        found.getElement().getParent().setAttributes(style)
         }
         catch(err)
         {
            //do nothing
         }
       }
     }
}

This is the script I build, let me know if there is other way to do it please.

Thanks

TheMaster
  • 45,448
  • 6
  • 62
  • 85
vector
  • 1,032
  • 1
  • 4
  • 16
  • I don't work with Word documents. My question is how does Google translate Word to Doc? If the paragraphs don't have the proper heading to be included in the TOC where does the TOC come from, or is it just plain text? – TheWizEd Aug 28 '22 at 13:22
  • @TheWizEd it just simple Google Conversion which google allows while uploading document in Drive, what happens is it just break the header-TOC link, but there is a table of content data in Google Doc, which you can see using this find element statement `body.findElement(DocumentApp.ElementType.TABLE_OF_CONTENTS).getElement().asTableOfContents()` , we can see all the TOC exist in the plain text(correct here), that I wanted to link with actual header using search. – vector Aug 28 '22 at 13:46
  • Can you provide a sample document, in order to test this? – Iamblichus Aug 29 '22 at 09:57

1 Answers1

0

Since the TOC paragraphs are included in the body you need to find the first occurance of the TOC text and use that as a starting point to look for the second occurance.

var elem = body.editAsText().findText(text);
elem = body.editAsText().findText(text,elem);
if( !elem ) throw "paragraph not found";
TheWizEd
  • 7,517
  • 2
  • 11
  • 19
  • Thanks for the answer, but It didn't worked actually. After script just doesn't found anything. – vector Aug 28 '22 at 16:35
  • I have Word for another job and I made a moke up of a document with a TOC and Outline with Header 1 and 2 and some text inbetween. I was able to get the TOC section text and then search for the second occurance of that text and found the paragraphs containing that text outside of the TOC so it should work. I'm not sure what the first part is for `var elem = body.editAsText().findText(text)` but the second part maybe should be `var found = body.findText(text,elem)`? – TheWizEd Aug 28 '22 at 22:09