在 Google 文档中插入 table API Python

Insert table in Google Docs API Python

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

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

  • 您想使用 Google 文档 API.
  • 在 Google 文档中插入 table

如果我的理解是正确的,这个修改怎么样?用作请求主体的对象是 docs.documents.get 方法返回的对象。在这个答案中,我想向您展示 3 个示例。

示例脚本 1:

此示例脚本来自 the official document

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

重要的一点是,在您 运行 脚本之前,请创建新的 Google 文件并放置一个 table。然后,请使用脚本来创建文档。这样,Hello 的文本被放入 table 的 "A1" 中,并且在 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 列的新 table。

脚本:

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

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

示例脚本 3:

很遗憾,虽然我一直在官方文档中寻找创建table并将值放入每个单元格的方法,但我找不到。所以我对此进行了实验。在此示例脚本中,我向您展示了创建 table 并将值放入每个单元格的方法。

该示例脚本的流程如下。

  1. 创建 2 行 2 列的 table。
  2. A1B1A2B2 的文本放入 table 的单元格 "A1:B2"。
    • 根据我的实验,得到了以下结果。
      • 对于行,索引需要每5个索引设置一次
      • 对于列,索引需要设置每2个索引
      • 重要的一点是,在单元格中输入值时,请按"B2"、"A2"、"B1"和"A1"的顺序排列。因为第一次放"A1"时,其他单元格的索引都变了。

脚本:

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。如果您运行脚本时出现错误,请检查the Quickstart
  • Google 文档 API 现在正在增长。所以我觉得以后可能会增加针对这种情况的更简单的方法。

参考文献:

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