如何通过google-docs-api请求将内容添加到表格单元格? [英] How to add content to table cell via google-docs-api request?

查看:172
本文介绍了如何通过google-docs-api请求将内容添加到表格单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向Google doc中的表格单元格添加内容,但是文档中描述的方法不起作用.

I want to add a content to a table cell in google doc, but the way described in the documentation doesn't work.

我的请求出了什么问题?当我为insertText请求的index参数提供1时,它只是将文本粘贴在表格之前.当我提供2作为index参数的值时,我得到一个错误:无效的请求[1] .insertText:插入索引必须在现有段落的范围之内.您仍然可以通过插入换行符来创建新的段落."

What is wrong with my request? When I provide 1 for index parameter of insertText request it just pastes text before table. When I provide 2 as value of index parameter I get an error: "Invalid requests[1].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines."

{
  "requests": [
    {
      "insertTable": {
        "endOfSegmentLocation": {
          "segmentId": ""
        },
        "columns": 1,
        "rows": 1
      }
    },
    {
      "insertText": {
        "location": {
          "index": 1
        },
        "text": "Cell content"
      }
    }
  ]
}

我希望文本必须插入到表格的唯一单元格中.

I expect that text must be inserted into the only cell of the table.

推荐答案

  • 您要将表格(1 x 1)附加到最后一个正文上.
  • 您要在第一个单元格中插入文本.
  • 从您的请求正文中,我可以像上面那样理解.如果我的理解是正确的,那么该流程如何?我认为可能有几种解决方案.因此,请仅考虑其中之一.

    From your request body, I could understand like above. If my understanding is correct, how about this flow? I think that there might be several solutions. So please think of this as just one of them.

    在将新表追加到最后一个主体的情况下("segmentId": ""表示该表追加到了最后一个主体.)首先,需要知道该表的起始索引.那么以下流程如何?

    In the case that new table is appended to the last body ("segmentId": "" means that the table is appended to the last body.), at first, the start index of the table is required to be known. So how about the following flows?

    在此流程中,它假定末尾的索引未知.

    In this flow, it supposes that the index of last body is not known.

    1. 使用以下请求正文附加表.

    1. Append a table using the following request body.

    {
      "requests": [
        {
          "insertTable": {
            "endOfSegmentLocation": {
              "segmentId": ""
            },
            "columns": 1,
            "rows": 1
          }
        }
      ]
    }
    

  • 使用以下端点检索表的起始索引.那时,您还可以检索单元格的开始索引.

  • Retrieve the start index of table using the following endpoint. At that time, you can also retrieve the start index of the cell.

    GET https://docs.googleapis.com/v1/documents/{fileId}?fields=body(content(startIndex%2Ctable))
    

  • 将文本插入到单元格中.在这种情况下,假定附加表的检索起始索引为10.第一个单元格的起始索引是14(我认为第一个单元格的起始索引可以通过start index of table + 4检索.).在这种情况下,用于将文本插入单元格的请求主体如下.

  • Insert the text to the cell. In this case, it supposes that the retrieved start index of the appended table is 10. The start index of the 1st cell is 14 (I think that the start index of the 1st cell can be retrieved by start index of table + 4.). In this case, the request body for inserting the text to the cell is as follows.

    {
      "requests": [
        {
          "insertText":
          {
            "location":
            {
              "index": 14
            },
            "text": "Cell content"
          }
        }
      ]
    }
    

  • 流程2:

    在此流程中,假设最后一个身体的索引已知.例如,当表被追加到新文档时,您可以使用以下请求正文使用文本创建表.在这种情况下,表的起始索引和单元格分别为15.

        {
          "requests": [
            {
              "insertTable":
              {
                "endOfSegmentLocation":
                {
                  "segmentId": ""
                },
                "columns": 1,
                "rows": 1
              }
            },
            {
              "insertText":
              {
                "location":
                {
                  "index": 5
                },
                "text": "Cell content"
              }
            }
          ]
        }
    

    参考文献:

    • 插入或删除表行
    • 线程:在Google Docs API Python中插入表
    • References:

      • Inserting or deleting table rows
      • Thread: Insert table in Google Docs API Python
      • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

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

        这篇关于如何通过google-docs-api请求将内容添加到表格单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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