Dominic Saavedra
Dominic Saavedra

Reputation: 11

Is there a way I can clear docs table formatting in a table using google apps script?

I am auto generating a google doc using google app script and when i insert a table it has some formatting that makes the cells too tall.

I have tried setting minimum height and that doesn't work beyond a point and its still too tall. The function is available in the ui to clear all table formatting and this gives the desired result. Is there a way to do this using Google app script? I can't seem to find a solution.

Formatting for other parts of the document in case that's relevant:

  var headerAttributes = {}; 

       headerAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#BFBFBF';
       headerAttributes[DocumentApp.Attribute.FONT_SIZE] = 25;
       headerAttributes[DocumentApp.Attribute.BOLD] = true;
       headerAttributes[DocumentApp.Attribute.UNDERLINE] = true;
       headerAttributes[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = 
            DocumentApp.HorizontalAlignment.CENTER;

  var dateAttributes = {};

       dateAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
       dateAttributes[DocumentApp.Attribute.FONT_SIZE] = 18;
       dateAttributes[DocumentApp.Attribute.BOLD] = false;
       dateAttributes[DocumentApp.Attribute.UNDERLINE] = false;
       dateAttributes[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = 
            DocumentApp.HorizontalAlignment.LEFT;

  var bodyAttributes = {};

       bodyAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';

Table creation:

 var cells = [['XXX','Zip','XXX','Bootstrap'],
              ['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],
              ['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],['','','','',]];

  var DLTable = body.appendTable(cells);  

  var headRow = DLTable.getRow(0);

  headRow.setMinimumHeight(10);

  for(var i = 0 ;i < 4; i++){

       headRow.getCell(i).setBackgroundColor('#BDBDBD');

  }

Upvotes: 1

Views: 391

Answers (1)

Andres Duarte
Andres Duarte

Reputation: 3340

You can reach the minimum height allowed for the rows in the table by using the setMinimumHeight() function with 0 as parameter:

  for (var i=0; i<cells.length; i++) {
    var row = DLTable.getRow(i);
    row.setMinimumHeight(0);   
  }

From the UI you can use the "clear formatting" button, which will clear the text formatting of the selection, among these changes is the line spacing going from the default 1.15 to single (1). This will make the cells less tall. You can change this from Apps Scrip using Docs Advanced Service [1]. You need to make a updateParagraphStyle request [2][3], and set the attribute "lineSpacing" to a 100 (Single) inside the paragraphStyle body. Here is the code you need to add:

  var requests = [
    {
      'updateParagraphStyle': {
        'range': {
          'startIndex': 1,
          'endIndex':  139
        },
        'paragraphStyle': {
          'lineSpacing': 100,
        },
        'fields': 'lineSpacing'
      }
    }];
  Docs.Documents.batchUpdate({'requests': requests}, 'DOC-ID');

You will need to change/find the right range for your table. You can check this guide about the document structure [4].

[1] https://developers.google.com/apps-script/advanced/docs

[2] https://developers.google.com/docs/api/how-tos/format-text

[3] https://developers.google.com/docs/api/reference/rest/v1/documents/request#UpdateParagraphStyleRequest

[4] https://developers.google.com/docs/api/concepts/structure

Upvotes: 1

Related Questions