Insert table in Google Docs API Python

北城余情 提交于 2019-12-12 21:26:10

问题


From Working with tables  |  Google Docs API  |  Google Developers

requests = [{'insertTable': {"table": {
"columns": 2,
"rows": 2,
"tableRows": [
    { "tableCells": [
            {
                "content": [ { "paragraph": { ...  }, } ],
            },
            {
                "content": [ { "paragraph": { ... }, } ],
            }
        ],
    },
    {
        "tableCells": [
            {
                "content": [ { "paragraph": { ... }, } ],
            },
            {
                "content": [ { "paragraph": { ... }, } ],
            }
        ],
    }
]}}}]
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

I'm getting a TypeError: Object of type set is not JSON serializable


回答1:


  • You want to insert a table in a Google Document using Google Docs API.

If my understanding is correct, how about this modification? The object using as a request body is the returned object from docs.documents.get method. In this answer, I would like to show you 3 samples.

Sample script 1:

This sample script is from the official document.

The following example inserts text into the first table cell of a table and adds a table row.

As an important point, before you run the script, please create new Google Document and put a table. Then, please use the script to the created Document. By this, the text of Hello is put in "A1" of the table and one row is added to the table.

Script:

requests = [{
      'insertText': {
        'location': {
          'index': 5
        },
        'text': 'Hello'
    }
  },
  {
    'insertTableRow': {
        'tableCellLocation': {
            'tableStartLocation': {
                    'index': 2
            },
            'rowIndex': 1,
            'columnIndex': 1
        },
        'insertBelow': 'true'
    }
  }
]

result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

Sample script 2:

In this sample script, a new table with 2 rows and 2 columns is created.

Script:

requests = [
    {
        "insertTable":
        {
            "rows": 2,
            "columns": 2,
            "location":
            {
                "index": 1
            }
        }
    }
]

result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

Sample script 3:

Unfortunately, although I had been looking for the method for creating a table and putting the values in each cell at the official document, I couldn't find. So I experimented about it. In this sample script, I show you the method for creating a table and putting the values in each cell.

The flow of this sample script is as follows.

  1. Create a table with 2 rows and 2 columns.
  2. Put a text of A1, B1, A2 and B2 to the cells "A1:B2" of the table.
    • From my experiment, the following results were obtained.
      • For the row, the index is required to set every 5 index.
      • For the column, the index is required to set every 2 index.
      • As an important point, when the values are put in cells, please put in order of "B2", "A2", "B1" and "A1". Because when "A1" is firstly put, the indexes for other cells are changed.

Script:

requests = [
    {
        "insertTable":
        {
            "rows": 2,
            "columns": 2,
            "location":
            {
                "index": 1
            }
        }
    },
    {
        "insertText":
        {
            "text": "B2",
            "location":
            {
                "index": 12
            }
        }
    },
    {
        "insertText":
        {
            "text": "A2",
            "location":
            {
                "index": 10
            }
        }
    },
    {
        "insertText":
        {
            "text": "B1",
            "location":
            {
                "index": 7
            }
        }
    },
    {
        "insertText":
        {
            "text": "A1",
            "location":
            {
                "index": 5
            }
        }
    }
]

result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

Note:

  • These sample scripts use the scope of https://www.googleapis.com/auth/documents. Please be careful this.
  • In these sample scripts, it supposes that you have already used Google Docs API. If the error occurred when you run the script, please check the Quickstart.
  • Google Docs API is growing now. So I think that in the future, more simple method for this situation might be added.

References:

  • Inserting or deleting table rows
  • Python Quickstart
  • InsertTableRequest

If I misunderstood your question and this was not the result you want, I apologize.



来源:https://stackoverflow.com/questions/56246486/insert-table-in-google-docs-api-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!