简体   繁体   中英

How to insertTable from sheet after specific text in google docs using google apps script?

I have got this code from Tanaike. It works but I don't understand the if statement.

Can someone explain this to me?

    var body = somedoc.getBody(); 
    var range = body.findText("#PLACEHOLDER#"); 
    var ele = range.getElement(); 

    if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) { 
      var offset = body.getChildIndex(ele.getParent());
      body.insertTable(offset + 1, data);
    }

Without seeing the rest of your code and question - what the code snippet does is to find a the text "PLACEHOLDER" in your document and to insert a table into the same StructuralElement as the one containing the Paragraph with your text.

It is useful to visualize the structure of a Google Docs document

在此处输入图片说明

在此处输入图片说明

In your case

  • The if statement verifies either the parent of the parent of ele is a BodySection
  • If the condition is fulfilled, it means that ele is a ParagraphElement
  • It also means that the parent of ele is a Paragraph
  • All StructuralElements have a childIndex
  • var offset = body.getChildIndex(ele.getParent()); finds the childIndex of the Paragraph that contains ele
  • The inserted table will have an index one higher than the Paragraph , this means that it will be inserted directly after the Paragraph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM