【问题标题】:How to add content to google doc table cell in c#如何在 C# 中向 google doc 表格单元格添加内容
【发布时间】:2019-07-03 18:58:41
【问题描述】:

我正在尝试将 3x3 表格插入到谷歌文档中,然后使用 c# 在表格单元格中插入文本。我遇到了这个thread,它帮助我添加了表格,并至少在其中一个单元格中获得了一些文本。

该线程中的答案是在新创建的表的 startindex 中添加 4。我这样做了,我的所有文本都刚刚添加到第一行的第二个单元格中。

您可以从下面的代码中看到,在 for 循环中,我希望用一些测试文本填充每个单元格。所以,我不确定为什么所有文本都被放入一个单元格中。

var docId = "mydocid";

List<Request> requests = new List<Request>
{
    new Request()
    {
        InsertTable = new InsertTableRequest()
        {
            EndOfSegmentLocation = new EndOfSegmentLocation
            {
                SegmentId = ""
            },
            Columns = 3,
            Rows = 3
        }
    }
};

BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest {Requests = requests};

service.Documents.BatchUpdate(body, docId).Execute();

var doc = service.Documents.Get(docId).Execute();

var index = doc.Body.Content.FirstOrDefault(x => x.Table != null).StartIndex + 4;

requests = new List<Request>();

for (var i = 0; i < 3; i++)
{
    index++;

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = "test 1",
            Location = new Location()
            {
                Index = index
            }
        }
    });

    index++;

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = "test 2",
            Location = new Location()
            {
                Index = index
            }
        }
    });

    index++;

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = "test 3",
            Location = new Location()
            {
                Index = index
            }
        }
    });
}

body = new BatchUpdateDocumentRequest { Requests = requests };

service.Documents.BatchUpdate(body, docId).Execute();

编辑:在测试了更多之后,我意识到我最初并没有注意到一个单元格中的文本最终是这样的 - tttttttttest 3est 2est 1est 3est 2est 1est 3est 2est 1 所以,看起来索引不一定用于表示将内容写入哪个单元格,但是是某种类型的子字符串函数。所以,很明显我不明白如何遍历每一行和单元格来插入带有 index 属性的文本。

根据要求,这里是请求体对象到json的转换。

{
    "requests": [{
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 60,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 1",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 61,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 2",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 62,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 3",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 63,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 1",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 64,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 2",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 65,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 3",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 66,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 1",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 67,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 2",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }, {
        "createNamedRange": null,
        "createParagraphBullets": null,
        "deleteContentRange": null,
        "deleteNamedRange": null,
        "deleteParagraphBullets": null,
        "deletePositionedObject": null,
        "deleteTableColumn": null,
        "deleteTableRow": null,
        "insertInlineImage": null,
        "insertPageBreak": null,
        "insertTable": null,
        "insertTableColumn": null,
        "insertTableRow": null,
        "insertText": {
            "endOfSegmentLocation": null,
            "location": {
                "index": 68,
                "segmentId": null,
                "ETag": null
            },
            "text": "test 3",
            "ETag": null
        },
        "replaceAllText": null,
        "updateParagraphStyle": null,
        "updateTableColumnProperties": null,
        "updateTableRowStyle": null,
        "updateTextStyle": null,
        "ETag": null
    }],
    "writeControl": null,
    "ETag": null
}

按照田池建议的答案进行编辑。

所以这是代码的更新版本。

public class AddendumRow
{
    public string Title {get;set;}
    public string Value {get;set;}
    public string Description {get;set;}
}


var tableRows = new List<AddendumRow>()
{
    new AddendumRow(){Title = "Row 1 Title", Value = "Row 1 Value", Description = "Row 1 Description" },
    new AddendumRow(){Title = "Row 2 Title", Value = "Row 2 Value", Description = "Row 2 Description" },
    new AddendumRow(){Title = "Row 3 Title", Value = "Row 3 Value", Description = "Row 3 Description" }
};

List<Request> requests = new List<Request>
{
    new Request()
    {
        InsertTable = new InsertTableRequest()
        {
            EndOfSegmentLocation = new EndOfSegmentLocation
            {
                SegmentId = ""
            },
            Columns = 3,
            Rows = tableRows.Count
        }
    }
};

BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest {Requests = requests};

service.Documents.BatchUpdate(body, docId).Execute();

var doc = service.Documents.Get(docId).Execute();

var index = doc.Body.Content.FirstOrDefault(x => x.Table != null).StartIndex + 3;

requests = new List<Request>();

foreach (var row in tableRows){

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = row.Title,
            Location = new Location()
            {
                Index = index
            }
        }
    }); 
    index += 2;

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = row.Value,
            Location = new Location()
            {
                Index = index
            }
        }
    }); 
    index += 2;

    requests.Add(new Request()
    {
        InsertText = new InsertTextRequest()
        {
            Text = row.Description,
            Location = new Location()
            {
                Index = index
            }
        }
    }); 

    index += 3;
}


body = new BatchUpdateDocumentRequest { Requests = requests };

var json = JsonConvert.SerializeObject(requests);

当我运行我认为与您的示例匹配的代码时,它会将所有内容扔到第一行的第一个单元格中。而那个单元格的内容是……

RoRoRowRoRoRowRoRoRow 3 Descriptionw 3 Valuew 3 Title 2 Descriptionw 2 Valuew 2 Title 1 Descriptionw 1 Valuew 1 Title

您可以看到它似乎将索引用作某种子字符串方法,用于在现有文本中插入文本,而不是移动到下一个单元格。

这一行生成的 json - var json = JsonConvert.SerializeObject(requests);在下面。据我所知,它与您的示例非常匹配,但只有很多附加属性。

[{
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 58,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 1 Title",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 60,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 1 Value",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 62,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 1 Description",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 65,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 2 Title",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 67,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 2 Value",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 69,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 2 Description",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 72,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 3 Title",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 74,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 3 Value",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}, {
    "createNamedRange": null,
    "createParagraphBullets": null,
    "deleteContentRange": null,
    "deleteNamedRange": null,
    "deleteParagraphBullets": null,
    "deletePositionedObject": null,
    "deleteTableColumn": null,
    "deleteTableRow": null,
    "insertInlineImage": null,
    "insertPageBreak": null,
    "insertTable": null,
    "insertTableColumn": null,
    "insertTableRow": null,
    "insertText": {
        "endOfSegmentLocation": null,
        "location": {
            "index": 76,
            "segmentId": null,
            "ETag": null
        },
        "text": "Row 3 Description",
        "ETag": null
    },
    "replaceAllText": null,
    "updateParagraphStyle": null,
    "updateTableColumnProperties": null,
    "updateTableRowStyle": null,
    "updateTextStyle": null,
    "ETag": null
}]

【问题讨论】:

  • 为了正确了解您目前的情况,能否提供您使用的请求正文作为 JSON 数据?
  • 添加了 json 的原始问题。
  • 感谢您的回复。我可以理解您的请求正文。所以我发布了一个修改后的请求正文。你能确认一下吗?如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

标签: c# google-docs google-docs-api


【解决方案1】:
  • 您想将具有 3 行和 3 列的新表附加到 Google 文档。
  • 您想将值放到所有单元格中。

我可以这样理解。如果我的理解是正确的,那么这个修改呢?

修改点:

  • 在您的情况下,这些值被放入新表中。在这种情况下,每个值都可以放在每个单元格的起始索引中。

  • 从您的请求正文中,我认为这个修改后的请求是以下情况。

    • 包含 3 行和 3 列的新表已附加到 Google 文档。并且表格的所有单元格都是空的。
    • 值被放入所有单元格。
    • 附表的startIndexbody.content[].startIndex)为57
    • 在这种情况下,第 1 行第 1 列的startIndex (body.content[].table.tableRows[0].tableCells[0].content[0].startIndex) 为“60”。而startIndex的其他单元格如下。

      • 从上图中可以看出,列单元格和行单元格的偏移量分别为23
  • 当通过一个API调用使用batchUpdate方法将值放入所有单元格时,有一点很重要。

    • 例如,将“sample”值放入第1行第1列的单元格(startIndex60.),第1行第2列的单元格startIndex变为@987654338 @。 62 的值分别是字符串 sample 的长度和下一个单元格的偏移量。
    • 在您的脚本中,偏移量是1。这样一来,所有值都将放在一个单元格中。
    • 作为将值放入空单元格的简单方法,对于具有 3 行和 3 列的表格,它通过将第 3 行和第 3 列的单元格中的值反向来创建请求正文订购。

当以上几点反映到您的请求正文中时,它变成如下。

修改后的请求正文:

{"requests":[
    {"insertText":{"location":{"index":78},"text":"c3"}},
    {"insertText":{"location":{"index":76},"text":"b3"}},
    {"insertText":{"location":{"index":74},"text":"a3"}},
    {"insertText":{"location":{"index":71},"text":"c2"}},
    {"insertText":{"location":{"index":69},"text":"b2"}},
    {"insertText":{"location":{"index":67},"text":"a2"}},
    {"insertText":{"location":{"index":64},"text":"c1"}},
    {"insertText":{"location":{"index":62},"text":"b1"}},
    {"insertText":{"location":{"index":60},"text":"a1"}}
]}

结果:

当上面的请求体用于表的startIndex57的新表时,可以得到如下结果。如果您使用上述请求正文,请注意index 的值。

参考资料:

【讨论】:

  • 不幸的是,它不起作用。它只是将所有内容放入第一个单元格并破坏它。我已经用更新的代码和 json 请求更新了我的原始问题。据我所知,它基本上与您的示例相匹配,但不起作用。
  • @geoff swartz 感谢您的回复。我带来的不便表示歉意。不幸的是,如果您测试了更新的请求正文,我认为您更新的请求正文没有反映我的回答。这是因为我的英语水平不好。所以我对此深表歉意。在您的请求正文中,insertText.location.index 按升序使用。在这种情况下,不能将每个值放入每个单元格,因为索引会根据内容的长度而改变。请按降序创建请求体数组,然后重新测试。
  • 啊哈!好的谢谢。我不明白为什么您必须反向执行,但是当我将循环切换为反向运行时,效果很好。非常感谢!
  • @geoff swartz 感谢您的回复。我很高兴你的问题得到了解决。我可以从你的问题中学习。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2018-07-31
  • 2021-11-18
  • 2014-06-15
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多