Resolved: Creating a Google Doc with 2 tables in Google Apps Script


Reference to this question see link I would to have the possibility to put 2 tables in my Google Doc from the Spreadsheet. In the code below I can put only 1 table (from col C to col E). My need is to put also a second table from col H to col N.
What I could add to the script?
function generateDoc() {
  var tableColumn = [3,5]; // Table1 header is in column 3C to 5E
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var TEMPLATE_ID = 'xxx';
  var ui = SpreadsheetApp.getUi();
  if (TEMPLATE_ID === '') {        
    ui.alert('TEMPLATE_ID needs to be defined in')
  var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy();
  var copyId = copyFile.getId();
  var copyDoc = DocumentApp.openById(copyId);
  var FILE_NAME = ui.prompt('Insert doc name:', ui.ButtonSet.OK);
  var copyBody = copyDoc.getBody();
  var lastColumn = sheet.getLastColumn();
  var activeRowIndex = sheet.getActiveRange().getRowIndex();
  var activeRow = sheet.getRange(activeRowIndex, 1, 1, lastColumn).getDisplayValues()[0];
  var headerRow = sheet.getRange(1, 1, 1, lastColumn).getDisplayValues()[0];
  for (var columnIndex = 0; columnIndex < headerRow.length; columnIndex++) {
    if (columnIndex === tableColumn[0] - 1) { // Check if column corresponds to table data
      try {
        var tableValues = sheet.getRange(2, tableColumn[0], sheet.getLastRow()-1,tableColumn[1]-tableColumn[0]+1).getDisplayValues();
        var placeholder = `%${headerRow[tableColumn[0]-1]}%`;
        var rangeElement = copyBody.findText(placeholder);
        var element = rangeElement.getElement();
        var childIndex = copyBody.getChildIndex(element.getParent());
        const elementText = copyBody.getChild(childIndex).asText().getText();
        const [beforeText, afterText] = elementText.split(placeholder);
        copyBody.insertParagraph(childIndex, beforeText);
        copyBody.insertTable(childIndex+1, tableValues).setColumnWidth(0, 215).setColumnWidth(1, 85).setColumnWidth(2, 150);
        copyBody.insertParagraph(childIndex+2, afterText);
      } catch(err) {
    } else if (columnIndex < tableColumn[0] - 1 || columnIndex > tableColumn[1] - 1) {
      var nextValue = formatString(activeRow[columnIndex]);
      copyBody.replaceText('%' + headerRow[columnIndex] + '%', nextValue);
  SpreadsheetApp.getUi().alert('doc created!!')     


Become one of us

The fact that you even asked this question is strong evidence that you did not write this code because if you had then you would already know how to answer your own question. But you clearly admit that you did not write the code by referencing the answer from Lamblichus. So are we to assume that you will continue to ask these sort of questions which provide you with the code which you need to accomplish yours task without ever knowing how to build them yourself? If that is the case then it would be better for us not to answer these questions and force you to learn how to answer them yourself. It would be better for you and better for us because there would be more of us to answer more questions because you would become one of us. So I invite you to become one of us. Write you own code. Study the documentation. Feel the freedom that comes from being able to create tools that serve you the unique way that you wish them to serve you.

If you have better answer, please add a comment about this, thank you!