Home > Net >  Can I export my data to a Google Doc from Google Sheets using Apps Script?
Can I export my data to a Google Doc from Google Sheets using Apps Script?

Time:09-15

My question may be a little hard to explain, but hopefully someone knows how to help! The co-op that I live in uses enter image description here

And the Doc before the table has been inserted

enter image description here

In the Google Doc script editor I have this script.

function addTable() {
  try {
    let doc = DocumentApp.getActiveDocument();
    let spread = SpreadsheetApp.openById("1xxxxxxxxxxxxxxxxxxxxxxxxxxA");
    let sheet = spread.getSheetByName("Sheet1");
    let values = sheet.getDataRange().getDisplayValues();
    let body = doc.getBody();
    let paras = body.getParagraphs();
    let index = paras.findIndex( para => para.findText("{{table1}}"));
    index = body.getChildIndex(paras[index]);
    body.insertTable(index,values);
    paras[index].removeFromParent();
  }
  catch(err) {
    console.log(err);
  }
}

The Doc after running the script

enter image description here

Reference

  • Related