Google Docs API, tablo içeriklerini düzenlemenize olanak tanır. Gerçekleştirebileceğiniz işlemler şunlardır:
- Satır, sütun veya tabloların tamamını ekleyip silebilirsiniz.
- Tablo hücrelerine içerik ekleme
- Tablo hücrelerindeki içeriği okuma
- Sütun özelliklerini ve satırların stilini değiştirebilirsiniz.
Google Dokümanlar'daki tablolar, dokümanda StructuralElement türü olarak temsil edilir. Her tablo, her satırında tablo hücrelerinin listesini içeren tablo satırlarının listesini içerir. Tüm yapısal öğelerde olduğu gibi, tablonun da dokümanda konumunu belirten başlangıç ve bitiş dizeleri vardır. Dizine ekleme hakkında daha fazla bilgi için yapıya bakın. Tablo özellikleri, sütun genişlikleri ve dolgu gibi birçok stil öğesini içerir.
Aşağıdaki JSON parçasında, ayrıntıların çoğunun kaldırıldığı basit bir 2x2 tablo gösterilmektedir:
"table": {
"columns": 2,
"rows": 2,
"tableRows": [
{ "tableCells": [
{
"content": [ { "paragraph": { ... }, } ],
},
{
"content": [ { "paragraph": { ... }, } ],
}
],
},
{
"tableCells": [
{
"content": [ { "paragraph": { ... }, } ],
},
{
"content": [ { "paragraph": { ... }, } ],
}
],
}
]
}
Tablo ekleme ve silme
Bir dokümana yeni tablo eklemek için InsertTableRequest'i kullanın. Tablo eklerken aşağıdakileri belirtmeniz gerekir:
- Tablo boyutları satır ve sütunlarda.
- Yeni tablonun ekleneceği konum: Bu, bir segment içindeki bir dizin veya bir segmentin sonu olabilir. Her iki durumda da, belirtilen sekmenin kimliği belirtilmelidir.
Tabloları silmeyle ilgili açık bir yöntem yoktur. Bir tabloyu dokümandan silmek için diğer içeriklerle aynı şekilde işlem yapın: Tablonun tamamını kapsayan bir aralık belirterek DeleteContentRangeRequest'i kullanın.
Aşağıdaki örnekte, boş bir dokümanın sonuna 3x3 boyutunda bir tablo eklenmiştir:
Java
// Insert a table at the end of the body. // (An empty or unspecified segmentId field indicates the document's body.) List<Request> requests = new ArrayList<>(); requests.add( new Request() .setInsertTable( new InsertTableRequest() .setEndOfSegmentLocation( new EndOfSegmentLocation().setTabId(TAB_ID)) .setRows(3) .setColumns(3))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
Python
# Insert a table at the end of the body. # (An empty or unspecified segmentId field indicates the document's body.) requests = [{ 'insertTable': { 'rows': 3, 'columns': 3, 'endOfSegmentLocation': { 'segmentId': '', 'tabId': TAB_ID } }, } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Bu örnekte, daha önce eklenen tablonun nasıl silineceği gösterilmektedir:
Java
// Delete a table that was inserted at the start of the body of the first tab. // (The table is the second element in the body: // documentTab.getBody().getContent().get(2).) Document document = docsService.documents().get(DOCUMENT_ID).setIncludeTabsContent(true).execute(); String tabId = document.getTabs()[0].getTabProperties().getTabId(); DocumentTab documentTab = document.getTabs()[0].getDocumentTab(); StructuralElement table = documentTab.getBody().getContent().get(2); List<Request> requests = new ArrayList<>(); requests.add( new Request() .setDeleteContentRange( new DeleteContentRangeRequest() .setRange( new Range() .setStartIndex(table.getStartIndex()) .setEndIndex(table.getEndIndex()) .setTabId(tabId)))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
Python
# Delete a table that was inserted at the start of the body of the first tab. # (The table is the second element in the body: ['body']['content'][2].) document = service.documents().get(documentId=DOCUMENT_ID, includeTabsContent=True).execute() tab_id = document['tabs'][0]['tabProperties']['tabId'] document_tab = document['tabs'][0]['documentTab'] table = document_tab['body']['content'][2] requests = [{ 'deleteContentRange': { 'range': { 'segmentId': '', 'startIndex': table['startIndex'], 'endIndex': table['endIndex'], 'tabId': tab_id } }, } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Bir tabloyu, başlangıç ve bitiş dizinlerini belirterek normal içerik olarak sildiğiniz için bu dizinleri bir yerden almanız gerekir. Örnekte, bu dizinlerin doküman içeriğinden nasıl ayıklanacağı gösterilmektedir.
Satır ekleme ve silme
Dokümanınızda zaten bir tablo varsa Google Dokümanlar API'si, tablo satırları eklemenize ve silmenize olanak tanır. Belirli bir tablo hücresinin üstüne veya altına satır eklemek için InsertTableRowRequest'i, belirtilen hücre konumunu kapsayan bir satırı kaldırmak için DeleteTableRowRequest'ı kullanın.
Aşağıdaki örnekte, bir tablonun ilk tablo hücresine metin eklenip bir tablo satırı eklenmektedir.
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setInsertText(new InsertTextRequest() .setText("Hello") .setLocation(new Location().setIndex(5).setTabId(TAB_ID)))); requests.add(new Request().setInsertTableRow(new InsertTableRowRequest() .setTableCellLocation(new TableCellLocation() .setTableStartLocation(new Location() .setIndex(2).setTabId(TAB_ID)) .setRowIndex(1) .setColumnIndex(1)) .setInsertBelow(true))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [{ 'insertText': { 'location': { 'index': 5, 'tabId': TAB_ID }, 'text': 'Hello' } }, { 'insertTableRow': { 'tableCellLocation': { 'tableStartLocation': { 'index': 2, 'tabId': TAB_ID }, 'rowIndex': 1, 'columnIndex': 1 }, 'insertBelow': 'true' } } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Sütun ekleme ve silme
Mevcut bir tabloya sütun eklemek için InsertTableColumnRequest'i kullanın. Aşağıdakileri belirtmeniz gerekir:
- Yanına yeni bir sütun eklenmesini istediğiniz hücre.
- Yeni sütunun hangi tarafa (sol veya sağ) ekleneceğini belirtir.
Aşağıdaki örnekte, daha önce gösterilen 2x2 tabloya nasıl sütun ekleyeceğiniz gösterilmektedir:
Java
List<Request> requests = new ArrayList<>(); requests.add( new Request() .setInsertTableColumn( new InsertTableColumnRequest() .setTableCellLocation( new TableCellLocation() .setTableStartLocation( new Location().setIndex(2).setTabId(TAB_ID)) .setRowIndex(0) .setColumnIndex(0)) .setInsertRight(true))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [{ 'insertTableColumn': { 'tableCellLocation': { 'tableStartLocation': { 'segmentId': '', 'index': 2, 'tabId': TAB_ID }, 'rowIndex': 0, 'columnIndex': 0 }, 'insertRight': True }, } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Bir sütunu silmek için DeleteTableColumnRequest'i kullanın. Hedef sütundaki hücre konumunu, sütun ekleme işleminde daha önce gösterildiği gibi belirtirsiniz.
Tablo hücrelerindeki içeriği okuma
Tablo hücresi, yapısal öğelerin bir listesini içerir. Bu yapısal öğelerin her biri metin içeren bir paragraf veya başka bir yapı türü (hatta başka bir tablo) olabilir. Tablo içeriğini okumak için Metin Ayıklama bölümünde gösterildiği gibi her öğeyi yinelemeli olarak inceleyebilirsiniz.
Tablo hücrelerine içerik ekleme
Bir tablo hücresine yazmak için güncellemek istediğiniz hücredeki bir dizin için InsertTextRequest kullanın. Tablo dizinleri, güncellenen metni hesaba katacak şekilde ayarlanır. Aynı durum, DeleteContentRangeRequest ile hücre metnini silme için de geçerlidir.
Sütun özelliklerini değiştirme
UpdateTableColumnPropertiesRequest, bir tablodaki bir veya daha fazla sütunun özelliklerini değiştirmenize olanak tanır.
Tablonun başlangıç dizini ile birlikte bir TableColumnProperties nesnesi sağlamanız gerekir. Yalnızca seçili sütunları değiştirmek için isteğe sütun numaralarının listesini ekleyin; tablodaki tüm sütunları değiştirmek için boş bir liste sağlayın.
Aşağıdaki örnekte, tüm sütunların genişliği 100 piksel, ilk sütunun genişliği ise 200 piksel olacak şekilde ayarlanarak bir tablonun sütun genişlikleri güncellenmektedir:
Java
List<Request> requests = new ArrayList<>(); requests.add( new Request() .setUpdateTableColumnProperties( new UpdateTableColumnPropertiesRequest() .setTableStartLocation( new Location() .setIndex(2) .setTabId(TAB_ID)) .setColumnIndices(null) .setTableColumnProperties( new TableColumnProperties() .setWidthType("FIXED_WIDTH") .setWidth( new Dimension().setMagnitude(100d).setUnit("PT"))) .setFields("*"))); List<Integer> columnIndices = new ArrayList<>(); columnIndices.add(0); requests.add( new Request() .setUpdateTableColumnProperties( new UpdateTableColumnPropertiesRequest() .setTableStartLocation( new Location() .setIndex(2) .setTabId(TAB_ID)) .setColumnIndices(columnIndices) .setTableColumnProperties( new TableColumnProperties() .setWidthType("FIXED_WIDTH") .setWidth( new Dimension().setMagnitude(200d).setUnit("PT"))) .setFields("*"))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [{ 'updateTableColumnProperties': { 'tableStartLocation': {'index': 2, 'tabId': TAB_ID}, 'columnIndices': [], 'tableColumnProperties': { 'widthType': 'FIXED_WIDTH', 'width': { 'magnitude': 100, 'unit': 'PT' } }, 'fields': '*' }, 'updateTableColumnProperties': { 'tableStartLocation': {'index': 2, 'tabId': TAB_ID}, 'columnIndices': [0], 'tableColumnProperties': { 'widthType': 'FIXED_WIDTH', 'width': { 'magnitude': 200, 'unit': 'PT' } }, 'fields': '*' }, } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Satır stillerini değiştirme
UpdateTableRowsStyleRequest, bir tablodaki bir veya daha fazla satırın stilini değiştirmenize olanak tanır.
Tablonun başlangıç dizini ile birlikte bir TableRowStyle nesnesi sağlamanız gerekir. Yalnızca seçili satırları değiştirmek için isteğe satır numaralarının listesini ekleyin; tablodaki tüm satırları değiştirmek için boş bir liste sağlayın.
Aşağıdaki örnekte, bir tablonun 3. satırının minimum yüksekliği ayarlanmaktadır:
Java
List<Integer> rowIndices = new ArrayList<>(); rowIndices.add(3); List<Request> requests = new ArrayList<>(); requests.add( new Request() .setUpdateTableRowStyle( new UpdateTableRowStyleRequest() .setTableStartLocation( new Location() .setIndex(2) .setTabId(TAB_ID)) .setRowIndices(rowIndices) .setTableRowStyle( new TableRowStyle() .setMinRowHeight( new Dimension().setMagnitude(18d).setUnit("PT"))) .setFields("*"))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [{ 'updateTableRowStyle': { 'tableStartLocation': {'index': 2, 'tabId': TAB_ID}, 'rowIndices': [3], 'tableRowStyle': { 'minRowHeight': { 'magnitude': 18, 'unit': 'PT' } }, 'fields': '*' }, } ] result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()