App script not setting table border attributes

1,475 views
Skip to first unread message

Warren Berey

unread,
Nov 6, 2022, 10:20:57 PM11/6/22
to Google Apps Script Community
var cellStyle = {};
cellStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
cellStyle[DocumentApp.Attribute.BORDER_WIDTH] = 1;
for (var f=1; f<mxcl; f++) {
hdr3[f] = sht2.getRange(1, f).getValue();
}
var g=0;
var tables = doc.getBody().getTables();
tables.forEach(function(table) {
g++;
if(g==2){
var hdr2 = new Array(10);
for (var n=0; n<10; n++) {
hdr2[n] = table.getCell(1,n).getText();
}
for (var d=3; d<mxrw+1; d++) {
for (var c=1; c<mxcl; c++) {
var vlu = sht2.getRange(d, c).getValue();
body.replaceText('{{'+hdr3[c]+'}}', vlu);
}
if (d < mxrw){
var tr = table.appendTableRow();
for (var m=0; m<10; m++) {
var td = tr.appendTableCell(hdr2[m]);
td.setAttributes(cellStyle);
}
}
}
}
})
doc.saveAndClose();

Everything else in script working perfect.  Appreciate any help.  Thanks!

Clark Lind

unread,
Nov 7, 2022, 6:17:11 AM11/7/22
to Google Apps Script Community
I played around a little with this, and the only Border Atrib I was able to actually affect was at the table level, never the Row or Cell. 

Warren Berey

unread,
Nov 7, 2022, 10:23:14 AM11/7/22
to Google Apps Script Community
Thanks.  How would I set the table border in my code?  I tried that too using table.setAttributes(cellStyle); but that didn't work for me either.

Tanaike

unread,
Nov 7, 2022, 8:47:37 PM11/7/22
to Google Apps Script Community

Clark Lind

unread,
Nov 8, 2022, 6:58:30 AM11/8/22
to Google Apps Script Community
If Tanaike's helpful links didn't help, this is the code I used to experiment. After you add your content to the table, then you modify the entire table:

function tableAtrb() {
  var doc = DocumentApp.getActiveDocument()
  var table = doc.getBody().getTables()[0];

  var cellStyle = {};
  cellStyle[DocumentApp.Attribute.BORDER_COLOR] = '#cccccc';
  cellStyle[DocumentApp.Attribute.BORDER_WIDTH] = 1;

  var tr = table.appendTableRow();

    for (var m=0; m<10; m++) {
    var td = tr.appendTableCell(`Some Heading${m}`);
    }

table.setAttributes(cellStyle)
}

Clark Lind

unread,
Nov 8, 2022, 7:01:52 AM11/8/22
to Google Apps Script Community
p.s., my method modifies the entire table. If you are targeting specific cells, you will need to use Tanaike's code and use the Docs Advanced methods via the API s he describes.

Warren Berey

unread,
Nov 13, 2022, 9:51:43 PM11/13/22
to Google Apps Script Community
Appreciate all the help.  I'm attaching my entire script because I tried both methods above and neither seems to add borders to the new rows created in my second table.  Again, appreciate any help.  PS  I am trying to add a black border to the entire table.  Would even be fine to add to all tables in the doc.

function certgen() {
  var sprd = SpreadsheetApp.getActive();
  var sht = sprd.getSheetByName('Opportunity');
  var tst = sht.getRange(15, 3).getValue();
  if (tst == "") {
    const googleDocTemplate = DriveApp.getFileById('xxx');  
    const destinationFolder = DriveApp.getFolderById('xxx')
    const copy = googleDocTemplate.makeCopy(`Certificate:` + Date() , destinationFolder)
    const doc = DocumentApp.openById(copy.getId())
    const body = doc.getBody();
    var rws = sht.getDataRange();
    var nmRws = rws.getNumRows();
    for (var h=2; h<nmRws; h++) {
      var hdr = sht.getRange(h, 1).getValue();
      var vle = sht.getRange(h, 3).getValue();
      body.replaceText('{{'+hdr+'}}', vle);
    }
    var sht2 = sprd.getSheetByName('Program');
    var rws2 = sht2.getDataRange();
    var mxrw = rws2.getNumRows();
    var mxcl = sht2.getMaxColumns()
    var hdr3 = new Array(mxcl);
    for (var f=1; f<mxcl; f++) {
      hdr3[f] = sht2.getRange(1, f).getValue();
    }    
    var g=0;
    var tables = doc.getBody().getTables();    
    var cellStyle = {};
      cellStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
      cellStyle[DocumentApp.Attribute.BORDER_WIDTH] = 1;
    tables.forEach(function(table) {
      g++;
      if(g==2){
        var hdr2 = new Array(10);
        for (var n=0; n<10; n++) {
          hdr2[n] = table.getCell(1,n).getText();
        }
        for (var d=3; d<mxrw+1; d++) {
          for (var c=1; c<mxcl; c++) {
            var vlu = sht2.getRange(d, c).getValue();
            body.replaceText('{{'+hdr3[c]+'}}', vlu);
          }
          if (d < mxrw){
            var tr = table.appendTableRow();
            for (var m=0; m<10; m++) {
              var td = tr.appendTableCell(hdr2[m]);
            }          
          }  
        }
      }
    table.setAttributes(cellStyle)
    }
    )
    doc.saveAndClose();
    const url = doc.getUrl();
    sht.getRange(15, 3).setValue(url);
    Browser.msgBox('Certificate Generated');
  }
  else {
    Browser.msgBox('Certificate Exists');
  }
}

cwl...@gmail.com

unread,
Nov 14, 2022, 3:47:31 PM11/14/22
to Google Apps Script Community
What if you simply move the border style outside the loop and set it all afterwards? Something like:
   //remove from here, to outside the loop
    }
    )
    tables[1].setAttributes(cellStyle)

Warren Berey

unread,
Nov 14, 2022, 6:12:27 PM11/14/22
to Google Apps Script Community
Thanks but that didn't work.  This is actually driving me a little crazy.  Any other thoughts?  When I go to the document and format the table changing the border to 1pt it wraps the table in borders no problem.  Could there be something wrong in the doc?

Warren Berey

unread,
Nov 14, 2022, 9:54:13 PM11/14/22
to Google Apps Script Community
Strangest thing.  I recreated the document and all works now.  There must have been something wrong with my original template.  Thanks for everyone's help.

cwl...@gmail.com

unread,
Nov 14, 2022, 10:49:45 PM11/14/22
to Google Apps Script Community
What I am finding is that, if you manually edit the table border, and then try to change it via script, it doesn't work. But any default table can be changed if the border hasn't been changed from the default. 

This may be worth submitting a bug report.
Reply all
Reply to author
Forward
0 new messages