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ştirin.
Google Dokümanlar'daki tablolar, dokümanda bir StructuralElement türü olarak gösterilir. Her tablo, tablo satırlarının listesini içerir. Her satırda ise tablo hücrelerinin listesi bulunur. Tüm yapısal öğelerde olduğu gibi, tabloda da başlangıç ve bitiş dizinleri bulunur. Bu dizinler, tablonun dokümandaki konumunu gösterir. Dizin oluşturma hakkında daha fazla bilgi için yapıyı inceleyin. Tablo özellikleri, sütun genişlikleri ve dolgu gibi birçok stil öğesini içerir.
Aşağıdaki JSON parçası, ayrıntıların çoğu kaldırılmış basit bir 2x2 tabloyu gösterir:
"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:
- Tablonun satır ve sütun boyutları.
- Yeni tablonun ekleneceği konum: Bu, bir segmentin içindeki bir dizin veya bir segmentin sonu olabilir. İkisinden biri, belirtilen sekmenin kimliğini içermelidir.
Tabloları silmek için açık bir yöntem yoktur. Bir belgeyi tablodan silmek için tabloyu diğer içerikler gibi değerlendirin: DeleteContentRangeRequest'i kullanarak tablonun tamamını kapsayan bir aralık belirtin.
Aşağıdaki örnekte, boş bir belgenin sonuna 3x3'lük bir tablo eklenmektedir:
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()
Tabloyu normal içerik olarak (başlangıç ve bitiş dizinlerini belirterek) sildiğiniz için bu dizinleri bir yerden almanız gerekir. Örnekte, bu dizinlerin doküman içeriğinden nasıl çıkarılacağı 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. Belirtilen 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 ise DeleteTableRowRequest'i kullanın.
Aşağıdaki örnekte, bir tablonun ilk tablo hücresine metin ekleniyor ve tablo satırı ekleniyor.
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:
- Yeni bir sütunun eklenmesini istediğiniz hücre.
- Yeni sütunun hangi tarafa (sol veya sağ) ekleneceği.
Aşağıdaki örnekte, daha önce gösterilen 2x2 tablosuna nasıl sütun ekleyebileceğ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ütun içindeki hücre konumunu, sütun ekleme işleminde gösterildiği gibi belirtirsiniz.
Tablo hücrelerindeki içeriği okuma
Tablo hücresi, yapısal öğelerin 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çeriklerini 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 dizine InsertTextRequest kullanın. Tablo dizinleri, güncellenen metni hesaba katacak şekilde ayarlanır. Aynı durum, DeleteContentRangeRequest ile hücre metnini silmek için de geçerlidir.
Sütun özelliklerini değiştirme
UpdateTableColumnPropertiesRequest, bir tablodaki sütunlardan bir veya daha fazlasının özelliklerini değiştirmenize olanak tanır.
Tablonun başlangıç dizinini ve bir TableColumnProperties nesnesini 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, bir tablonun sütun genişlikleri güncellenerek tüm sütunlar 100 pt genişliğe, ilk sütun ise 200 pt genişliğe ayarlanıyor:
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ıç dizinini bir TableRowStyle nesnesiyle birlikte 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, 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()