Tom Kerswill
Tom Kerswill

Reputation: 439

Google Docs API - adjust width of table column

I am able to create a table using the Google Docs Api. By default, each column is of equal width.

How is it possible to adjust the width of the columns using the Google Docs API?

I can see the updateTextStyle and updateParagraphStyle request types (batchUpdate methods) but there's currently no updateTable method, or any similar method that looks like it could do this. I'm using the reference documentation, here:

https://developers.google.com/docs/api/reference/rest/v1/documents/request

I'm using NodeJS / Javascript to interact with the API currently.

Upvotes: 1

Views: 959

Answers (1)

Milind Deore
Milind Deore

Reputation: 3053

This is possible, in fact all the table operation that you can do on UI are possible.

1) Create table with following request:

def create_loc_table(doc_id, r, c, idx):
         """
         r = number of rows,
         c = number of columns,
         idx = Start index where table needs to be created. 
         """
         request = [{
               'insertTable': {
                   'rows': r,
                   'columns': c,
                   'location': {
                     'segmentId':'',
                     'index': idx
                   }
               },
           }
           ]

         result = self.docs_service.documents().batchUpdate(documentId=doc_id, body={'requests': request}).execute()
         print('Result {0}'.format(json.dumps(result, indent=4)))

2) Modify/update the table:

def modify_table(self, t_idx):
    """
    t_idx = Table start index.
    """
    request = [{
              'updateTableColumnProperties': {
              'tableStartLocation': {'index': t_idx},
              'columnIndices': [0],
              'tableColumnProperties': {
                'widthType': 'FIXED_WIDTH',
                'width': {
                  'magnitude': 100,
                  'unit': 'PT'
               }
             },
             'fields': '*'
           }
        }
        ]

    result = self.docs_service.documents().batchUpdate(documentId=doc_id, body={'requests': request}).execute()
    print('Result {0}'.format(json.dumps(result, indent=4)))

3) Output would be

enter image description here

Upvotes: 1

Related Questions