user1063287
user1063287

Reputation: 10869

How to apply padding attribute values to a whole table in Document Service?

Desired Behaviour

I want to be able to apply padding attribute values to a whole table, and not just individual cells.

Actual Behaviour

I can apply padding attribute values to individual cells, but not the whole table.

What I've Tried

I create a table with:

var header_table = body.insertTable(0, cells);

This is using the insertTable(childIndex, cells) method of the Body class and returns a Table.

The Table class has a method called setAttributes(attributes) which "sets the element's attributes".

I then try and set the table's attributes with:

// BEGIN define style
var header_table_style = {};
header_table_style[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
header_table_style[DocumentApp.Attribute.PADDING_BOTTOM] = 0;
header_table_style[DocumentApp.Attribute.PADDING_LEFT] = 0;
header_table_style[DocumentApp.Attribute.PADDING_RIGHT] = 0;
header_table_style[DocumentApp.Attribute.PADDING_TOP] = 0;
// END define style

// apply the style to the table
header_table.setAttributes(header_table_style);

The result is that the font-family is applied but not the padding, even thought padding is in the list of Enum Attributes.

If I just try and apply the attributes to individual cells, however, the padding is applied:

header_table.getCell(0, 0).setAttributes(header_table_style);
header_table.getCell(0, 1).setAttributes(header_table_style);

Questions

01) How can I apply attributes to a whole table?

02) Bonus Question: Why do all the official code examples for Table methods not specifically relate to scenarios using the Table class? (example)

03) Bonus Question: Why is there a getRow(rowIndex) Table method and not a getColumn(columnIndex) method? How can you center all the contents of a column?


Edit:

As an aside, other similar attempts I make to apply styles don't work - I include them below as they may all point to a singular misunderstanding about how certain methods work.

a) Trying to add 5 point space before paragraph through MARGIN_TOP - (result: all other styles are applied, except for MARGIN_TOP):

var title_text = body.insertParagraph(2,"Hello");
title_style = {};
title_style[DocumentApp.Attribute.FONT_FAMILY] = 'Roboto Condensed';
title_style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#999999';
title_style[DocumentApp.Attribute.FONT_SIZE] = 18;
title_style[DocumentApp.Attribute.MARGIN_TOP] = 5;  // DOESN'T WORK  
title_text.setAttributes(title_style);

However, this works:

title_text.setSpacingBefore(5);

b) Trying to change the background color of a table (result: the background color of the paragraph changes, not the table):

another_table_style = {};
another_table_style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#cfe2f3";
var another_table = body.appendTable(some_cells);
another_table.setAttributes(another_table_style);

Upvotes: 3

Views: 2206

Answers (1)

Brian
Brian

Reputation: 4344

You need to pay attention to the docs - some attributes are only for certain elements. For instance, the padding Enum is only for table cells, not entire tables.

To apply changes to entire tables, you can loop through the table cell elements and apply a calculated padding (or static).

Bonus 1 - Not sure. Attributes in sheets are set individually on Range elements.

Bonus 2 - Columns are children of rows. So, to iterate, you need to use two loops. You can set the background of a TABLE_CELL element using the Enum.

function table() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var cells = [['Row 1.1', 'Row 1.2'], ['Row 2.1','Row 2.2']];

  var header_table = body.insertTable(0, cells);

  var styles = {};
  styles[DocumentApp.Attribute.PADDING_BOTTOM] = 20; // only applies to table cells
  styles[DocumentApp.Attribute.BACKGROUND_COLOR] = "#00ffff"


  Logger.log(header_table.getType());
  var rows = header_table.getNumRows();

  Logger.log(rows); // check the number of rows in your table

  // for each row...
  for(var i=0; i<rows; i++) {

    // for each column...
    var cols = header_table.getRow(i).getNumChildren();

    for(var j=0; j<cols; j++) {
      var cell = header_table.getRow(i).getCell(j);

      // Set the TableCell attributes to each
      cell.setAttributes(styles)
    }
  }
}

I tested the MARGIN Enums and I think they only apply to the document Body, which contradicts the docs a little bit.

function setMargins() {
  var body = DocumentApp.getActiveDocument().getBody();

  var margins = {};

  margins[DocumentApp.Attribute.MARGIN_TOP] = 150;
  margins[DocumentApp.Attribute.MARGIN_LEFT] = 150;

  // uncomment the next line to set margins on the document body.
//  body.setAttributes(margins);

  // this returns all text elements in the doc, even those in tables
  var pars = body.getParagraphs(); 

  // According to the docs, this should work, but it doesn't.
  for(var i=0; i<pars.length; i++) {
    pars[i].setAttributes(margins);
  }
}

Upvotes: 2

Related Questions