3

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)
                             .getSheetByName(sheetName)
                             .getRange(rangeName)
                             .getValues();

  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);
    tableRange.remove();
    body.removeChild(table);
    table = body.insertTable(ind, values);
  }
}
2
  • 1
    This appears to be from my script here, but some lines are missing from if statement. I'm not surprised that it's not working if some lines are skipped.
    – user79865
    Apr 20, 2016 at 19:19
  • Welcome to Web Applications. Please take the tour and checkout How to Ask.
    – Rubén
    Apr 20, 2016 at 21:59

1 Answer 1

1

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

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

Your Answer

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

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