在Google Docs API Python中插入表格 [英] Insert table in Google Docs API Python

查看:230
本文介绍了在Google Docs API Python中插入表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自使用表格

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()

我得到一个TypeError: Object of type set is not JSON serializable

推荐答案

  • 您要使用Google Docs API在Google文档中插入表格.
  • 如果我的理解是正确的,那么该修改如何?用作请求正文的对象是docs.documents.get方法返回的对象.在此答案中,我想向您展示3个示例.

    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.

    此示例脚本来自官方文档.

    下面的示例将文本插入到表的第一个表单元格中,并添加一个表行.

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

    重要的一点是,在运行脚本之前,请创建新的Google文档并放置一个表格.然后,请使用脚本来创建文档.这样,将Hello的文本放在表的"A1"中,并将一行添加到表中.

    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.

    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()
    

    示例脚本2:

    在此示例脚本中,创建了一个具有2行2列的新表.

    Sample script 2:

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

    requests = [
        {
            "insertTable":
            {
                "rows": 2,
                "columns": 2,
                "location":
                {
                    "index": 1
                }
            }
        }
    ]
    
    result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
    

    示例脚本3:

    不幸的是,尽管我一直在寻找用于创建表并将值放在每个单元格中的方法的官方文档,但我找不到.因此,我对此进行了试验.在此示例脚本中,我向您展示了创建表并将值放在每个单元格中的方法.

    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. 创建一个包含2行2列的表格.
    2. A1B1A2B2的文本放入表格的单元格"A1:B2".
      • 从我的实验中获得了以下结果.
        • 对于该行,需要每5个索引设置索引.
        • 对于该列,索引需要每2个索引设置一次.
        • 重要的是,将值放入单元格时,请按"B2","A2","B1"和"A1"的顺序排列.因为当第一次放置"A1"时,其他单元格的索引也会更改.
    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.

    脚本:

    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()
    

    注意:

    • 这些示例脚本使用https://www.googleapis.com/auth/documents的范围.请注意这一点.
    • 在这些示例脚本中,假定您已经使用过Google Docs API.如果在运行脚本时发生错误,请检查快速入门.
    • Google Docs API现在正在增长.因此,我认为将来可能会为这种情况添加更简单的方法.
    • 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.
        • Inserting or deleting table rows
        • Python Quickstart
        • InsertTableRequest

        如果我误解了您的问题,而这不是您想要的结果,我深表歉意.

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

        这篇关于在Google Docs API Python中插入表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆