Insert table in Google Docs API Python

后端 未结 1 935
心在旅途
心在旅途 2021-01-20 22:55

From Working with tables  |  Google Docs API  |  Google Developers

requests = [{\'insertTable\': {\"table\": {
\"columns\": 2,
\"rows\": 2,
\"tableRows\": [
         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 23:17

    • 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.

    0 讨论(0)
提交回复
热议问题