How do I embed a Google Sheet into a google document and keep the Google Sheet format in the document?

I have got this far with the script but it's not working

function updateData() {
  var ssUrl = 'sheet url';
  var sheetName = 'sheet name';  // name of sheet to use
    var rangeName = 'A1:J45';    // range of values to include 

  var values = SpreadsheetApp.openByUrl(ssUrl)

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var ranges = doc.getNamedRanges('embeddedSheet-range');
  if (ranges.length == 0) {
       tableRange = ranges[0];
    table = tableRange.getRange().getRangeElements()[0].getElement();
    var ind = body.getChildIndex(table);
    table = body.insertTable(ind, values);

1 Answer 1


You seem to be missing the bottom section of this from Bookends original:

  var rangeBuilder = doc.newRange();
  doc.addNamedRange('embeddedSheet-range', rangeBuilder.build());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.