如何通过 google-docs-api 请求向 table 单元格添加内容?
How to add content to table cell via google-docs-api request?
我想在 google 文档中的 table 单元格中添加内容,但是文档中描述的方法不起作用。
我的请求有什么问题?当我为 insertText
请求的 index
参数提供 1 时,它只是在 table 之前粘贴文本。当我提供 2 作为 index
参数的值时,出现错误:"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"
}
}
]
}
我希望文本必须插入到 table 的唯一单元格中。
- 您想在最后一个正文后附加 table (1 x 1)。
- 您想在第一个单元格中插入文本。
从你的请求正文中,我可以像上面那样理解。如果我的理解是正确的,那么这个流程呢?我认为可能有几种解决方案。所以请把这当作其中之一。
在new table追加到最后一个body的情况下("segmentId": ""
表示table追加到最后一个body。),首先,起始索引的 table 需要知道。那么下面的流程呢?
流程 1:
在这个流程中,假设最后一个body的索引是未知的。
使用以下请求正文附加 table。
{
"requests": [
{
"insertTable": {
"endOfSegmentLocation": {
"segmentId": ""
},
"columns": 1,
"rows": 1
}
}
]
}
使用以下端点检索 table 的起始索引。届时,您还可以检索单元格的起始索引。
GET https://docs.googleapis.com/v1/documents/{fileId}?fields=body(content(startIndex%2Ctable))
将文本插入单元格。在这种情况下,假设检索到的附加 table 的起始索引是 10
。第一个单元格的起始索引是14
(我认为第一个单元格的起始索引可以通过start index of table + 4
来检索)。在这种情况下,将文本插入单元格的请求正文如下。
{
"requests": [
{
"insertText":
{
"location":
{
"index": 14
},
"text": "Cell content"
}
}
]
}
流程 2:
在这个流程中,假设最后一个body的索引是已知的。例如,当 table 附加到新文档时,您可以使用以下请求正文创建带有文本的 table。在本例中,table 和单元格的起始索引分别为 1
和 5
。
{
"requests": [
{
"insertTable":
{
"endOfSegmentLocation":
{
"segmentId": ""
},
"columns": 1,
"rows": 1
}
},
{
"insertText":
{
"location":
{
"index": 5
},
"text": "Cell content"
}
}
]
}
参考文献:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
这是对已经发布的答案的跟进。 @ANewb 暗示流程在第 5 个单元格之后结束。确保负载中的 values/entries 不是空字符串。对于遇到相同挑战的任何人,您可以尝试使用 tenary 条件 - for php $value == "" ? "N/A : $value;
,for js let value = checkValue == "" ? "N?A": checkValue;
我想在 google 文档中的 table 单元格中添加内容,但是文档中描述的方法不起作用。
我的请求有什么问题?当我为 insertText
请求的 index
参数提供 1 时,它只是在 table 之前粘贴文本。当我提供 2 作为 index
参数的值时,出现错误:"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"
}
}
]
}
我希望文本必须插入到 table 的唯一单元格中。
- 您想在最后一个正文后附加 table (1 x 1)。
- 您想在第一个单元格中插入文本。
从你的请求正文中,我可以像上面那样理解。如果我的理解是正确的,那么这个流程呢?我认为可能有几种解决方案。所以请把这当作其中之一。
在new table追加到最后一个body的情况下("segmentId": ""
表示table追加到最后一个body。),首先,起始索引的 table 需要知道。那么下面的流程呢?
流程 1:
在这个流程中,假设最后一个body的索引是未知的。
使用以下请求正文附加 table。
{ "requests": [ { "insertTable": { "endOfSegmentLocation": { "segmentId": "" }, "columns": 1, "rows": 1 } } ] }
使用以下端点检索 table 的起始索引。届时,您还可以检索单元格的起始索引。
GET https://docs.googleapis.com/v1/documents/{fileId}?fields=body(content(startIndex%2Ctable))
将文本插入单元格。在这种情况下,假设检索到的附加 table 的起始索引是
10
。第一个单元格的起始索引是14
(我认为第一个单元格的起始索引可以通过start index of table + 4
来检索)。在这种情况下,将文本插入单元格的请求正文如下。{ "requests": [ { "insertText": { "location": { "index": 14 }, "text": "Cell content" } } ] }
流程 2:
在这个流程中,假设最后一个body的索引是已知的。例如,当 table 附加到新文档时,您可以使用以下请求正文创建带有文本的 table。在本例中,table 和单元格的起始索引分别为 1
和 5
。
{
"requests": [
{
"insertTable":
{
"endOfSegmentLocation":
{
"segmentId": ""
},
"columns": 1,
"rows": 1
}
},
{
"insertText":
{
"location":
{
"index": 5
},
"text": "Cell content"
}
}
]
}
参考文献:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
这是对已经发布的答案的跟进。 @ANewb 暗示流程在第 5 个单元格之后结束。确保负载中的 values/entries 不是空字符串。对于遇到相同挑战的任何人,您可以尝试使用 tenary 条件 - for php $value == "" ? "N/A : $value;
,for js let value = checkValue == "" ? "N?A": checkValue;