1
votes

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.

1
In the current stage, unfortunately, the column width and row height cannot be modified, yet. Although until recently, new table had not been able to be created, new table can be created now. From this, it is found that Google Docs API is growing. So in near future, the column width and row height might be able to be modified. Now, I apologize that I have to comment this cannot be achieved. I think that a current workaround is to create an API using Web Apps with Google Apps Script, and request to the API using Node.js.Tanaike
Thanks for the comment! Ah, that's a shame, but hopefully it will be added soon; yes - I think being able to adjust table widths is vital for some of the main use cases, such as being able to produce invoices and so on... Thanks once again. I'll give Google Apps Script a try as an alternative to the API and see how I get on...Tom Kerswill

1 Answers

1
votes

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