Imported table from Google sheet to Google doc does not import table (rows,columns) formatting

Viewed 24

Following my previous question, the table is copied from Google sheets to Google Docs with the same text color and Text style but it does not address the table border formatting (rows and columns) and it does not hold when it is imported to Google doc from google sheet. Now I am trying to manipulate the rows and columns formatting. Here is the table in sheet:Table in sheet

I have tried to import this table into Google doc using the following code:

function Table(){

  const ss = SpreadsheetApp.openById("ssid")
  const sh = ss.getSheetByName("sheetName");
  const rg = sh.getRange(3, 1, 14, 3)
  const vs = rg.getValues();
  const bs = rg.getBackgrounds();
  const fws = rg.getFontWeights();
  const fss = rg.getFontSizes();
  const sts = rg.getTextStyles();
  
  var colWidth = [];  // Added
  for (var col = 1; col <= 3; col++) {  // Added
    colWidth.push(sh.getColumnWidth(col) * 3 / 4.75);
  }
  
  const d = DocumentApp.openById('docId');
  const b = d.getBody();
  const tbl = b.appendTable(vs);
  tbl.setBorderWidth(0);
    
  colWidth.forEach(function(e, i) {tbl.setColumnWidth(i, e)});  // Added
  vs.forEach((r,i) => {
    r.forEach((c,j) => {
      let sty = {};
      sty[DocumentApp.Attribute.BACKGROUND_COLOR] = bs[i][j];
      sty[DocumentApp.Attribute.FONT_SIZE] = fss[i][j];
      sty[DocumentApp.Attribute.BOLD] = (fws[i][j] == 'bold') ? true: false;
      sty[DocumentApp.Attribute.FOREGROUND_COLOR] = sts[i][j].getForegroundColorObject().asRgbColor().asHexString();
      tbl.getRow(i).getCell(j).setAttributes(sty);
    });
  });
}

But the formatting of the imported table in Google Docs did not hold and it looks like this:Google Doc Table

I tried to manipulate the table using merge(), setBorderWidth(), getRow(), and getChild() methods, but it messes up the entire formatting of the table.

The end result in Google Docs should be like this:

End Goal

Here is the

link

to the sheet. Any guidance/help would be much appreciated. Thank you

0 Answers
Related