Я пытаюсь вставить таблицу 3x3 в документ Google, а затем текст в ячейки таблицы с помощью c #.Я наткнулся на этот поток , который помог мне добавить таблицу и, по крайней мере, получить некоторый текст в одной из ячеек.
В ответе в этой теме сказано добавить 4 к стартовому индексувновь созданная таблица.Я сделал это, и весь мой текст был просто добавлен во 2-ю ячейку первого ряда.
Вы можете видеть из моего кода ниже, что в цикле 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 1est Таким образом, похоже, что индекс не обязательно используется для указания, в какую ячейку записывать содержимое, а какой-то тип функции подстроки.Итак, я явно не понимаю, как выполнять итерацию по каждой строке и ячейке для вставки текста со свойством 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 (запросы);ниже.Из того, что я вижу, он в значительной степени соответствует вашему примеру, но обладает множеством дополнительных свойств.
[{
"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
}]