title: Embed spreadsheet table in google doc with apps-script
body: <p>I'm wondering if there's a way to embed a spreadsheet table into a google doc using apps script.</p>
  
  <p>I am aware that you can do it by copy/pasting the table into the google doc, but is there a way to do it with apps script?</p>
  
  <p><em>Edit</em></p>
  
  <p>Using @Cooper and @Alberto Molina's answers, I came up with the following code, it imports and inserts a table</p>
  
  <pre><code>var sheet = SpreadSheetApp.openById("").getSheets()[0];
  var docBody = DocumentApp.openById("").getBody();
  var values = sheet.getDataRange().getValues();
  var elem = docBody.findText("").getElement();  
  var parent = elem.getParent();
  var idx = docBody.getChildIndex(parent);
  docBody.insertTable(++idx, values);
  </code></pre>
  
  <p>This code inserts a "raw" table like so 
  <a href="https://i.stack.imgur.com/s43Wl.png" rel="nofollow noreferrer">Image</a></p>
  
  <p>I want the changes to the original table to affect the table in the google document. So basically I want to insert it like this <a href="https://i.stack.imgur.com/4p1yH.png" rel="nofollow noreferrer">Image</a></p>
  
  <p>The idea I currently have is : </p>
  
  <pre><code>docBody.insertTable(++idx, values).setLinkUrl(**???**);
  </code></pre>
  
  <p>But I don't know what to set the URL to</p>