Google Docs API की मदद से, टेबल के कॉन्टेंट में बदलाव किया जा सकता है. ये ऑपरेशन किए जा सकते हैं:
- पंक्तियां, कॉलम या पूरी टेबल डालें और मिटाएं.
- टेबल की सेल में कॉन्टेंट डालें.
- टेबल सेल में मौजूद कॉन्टेंट को पढ़ता है.
- कॉलम की प्रॉपर्टी और पंक्तियों की स्टाइल में बदलाव करें.
Google Docs में टेबल को दस्तावेज़ में एक तरह के StructuralElement के तौर पर दिखाया जाता है. हर टेबल में टेबल की पंक्तियों की सूची होती है. साथ ही, हर पंक्ति में टेबल की सेल की सूची होती है. स्ट्रक्चरल एलिमेंट की तरह ही, टेबल में शुरू और आखिर के इंडेक्स होते हैं. इनसे दस्तावेज़ में टेबल की पोज़िशन का पता चलता है. इंडेक्स करने के बारे में ज़्यादा जानकारी के लिए, स्ट्रक्चर देखें. टेबल प्रॉपर्टी में कई स्टाइल एलिमेंट शामिल होते हैं, जैसे कि कॉलम की चौड़ाई और पैडिंग.
यहां दिया गया JSON फ़्रैगमेंट, 2x2 वाली एक साधारण टेबल दिखाता है. इसमें ज़्यादातर जानकारी हटा दी गई है:
"table": {
"columns": 2,
"rows": 2,
"tableRows": [
{ "tableCells": [
{
"content": [ { "paragraph": { ... }, } ],
},
{
"content": [ { "paragraph": { ... }, } ],
}
],
},
{
"tableCells": [
{
"content": [ { "paragraph": { ... }, } ],
},
{
"content": [ { "paragraph": { ... }, } ],
}
],
}
]
}
टेबल डालना और मिटाना
किसी दस्तावेज़ में नई टेबल जोड़ने के लिए, InsertTableRequest का इस्तेमाल करें. टेबल डालते समय, आपको यह जानकारी देनी होगी:
- पंक्तियों और कॉलम में टेबल डाइमेंशन.
- नई टेबल डालने की जगह: यह किसी सेगमेंट में इंडेक्स हो सकता है या किसी सेगमेंट का आखिरी हिस्सा हो सकता है. इनमें से किसी एक में, बताए गए टैब का आईडी शामिल होना चाहिए.
टेबल मिटाने का कोई खास तरीका नहीं है. किसी दस्तावेज़ से टेबल मिटाने के लिए, उससे वैसे ही व्यवहार करें जैसे किसी दूसरे कॉन्टेंट से किया जाता है: DeleteContentRangeRequest का इस्तेमाल करके, पूरी टेबल को कवर करने वाली रेंज तय करें.
इस उदाहरण में, खाली दस्तावेज़ के आखिर में 3x3 टेबल डाली गई है:
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()
इस उदाहरण में, पहले से डाली गई टेबल को मिटाने का तरीका बताया गया है:
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()
टेबल को सामान्य कॉन्टेंट के तौर पर मिटाया जाता है. इसके लिए, आपको टेबल के शुरू और आखिर के इंडेक्स की जानकारी देनी होती है. उदाहरण में, दस्तावेज़ के कॉन्टेंट से इन इंडेक्स को निकालने का एक तरीका दिखाया गया है.
लाइनें डालना और मिटाना
अगर आपके दस्तावेज़ में पहले से ही कोई टेबल मौजूद है, तो Google Docs API की मदद से टेबल की पंक्तियां डाली और मिटाई जा सकती हैं. किसी टेबल सेल के ऊपर या नीचे पंक्तियां जोड़ने के लिए, InsertTableRowRequest का इस्तेमाल करें. साथ ही, किसी सेल की जगह पर मौजूद पंक्ति को हटाने के लिए, DeleteTableRowRequest का इस्तेमाल करें.
नीचे दिए गए उदाहरण में, टेबल की पहली सेल में टेक्स्ट डाला गया है और टेबल की एक पंक्ति जोड़ी गई है.
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()
कॉलम जोड़ना और मिटाना
किसी मौजूदा टेबल में कॉलम जोड़ने के लिए, InsertTableColumnRequest का इस्तेमाल करें. आपको यह जानकारी देनी होगी:
- वह सेल जिसके बगल में आपको नया कॉलम जोड़ना है.
- नया कॉलम किस तरफ़ (बाईं या दाईं) जोड़ना है.
इस उदाहरण में, पहले दिखाई गई 2x2 टेबल में कॉलम जोड़ने का तरीका बताया गया है:
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()
किसी कॉलम को मिटाने के लिए, DeleteTableColumnRequest का इस्तेमाल करें. कॉलम डालने के लिए, टारगेट कॉलम में सेल की जगह की जानकारी उसी तरह दी जाती है जिस तरह पहले दिखाई गई थी.
टेबल सेल से कॉन्टेंट पढ़ना
टेबल सेल में स्ट्रक्चरल एलिमेंट की सूची होती है. इनमें से हर स्ट्रक्चरल एलिमेंट, टेक्स्ट वाला पैराग्राफ़ या किसी दूसरी तरह का स्ट्रक्चर हो सकता है. यहां तक कि, यह कोई दूसरी टेबल भी हो सकती है. टेबल का कॉन्टेंट पढ़ने के लिए, टेक्स्ट एक्सट्रैक्ट करें में दिखाए गए तरीके से, हर एलिमेंट की बार-बार जांच की जा सकती है.
टेबल की सेल में कॉन्टेंट डालना
टेबल सेल में लिखने के लिए, उस सेल के इंडेक्स में InsertTextRequest का इस्तेमाल करें जिसे अपडेट करना है. अपडेट किए गए टेक्स्ट के हिसाब से, टेबल के इंडेक्स में बदलाव किए जाते हैं. यही बात, DeleteContentRangeRequest का इस्तेमाल करके सेल का टेक्स्ट मिटाने पर भी लागू होती है.
कॉलम की प्रॉपर्टी में बदलाव करना
UpdateTableColumnPropertiesRequest की मदद से, टेबल में मौजूद एक या उससे ज़्यादा कॉलम की प्रॉपर्टी में बदलाव किया जा सकता है.
आपको टेबल का शुरुआती इंडेक्स और TableColumnProperties ऑब्जेक्ट देना होगा. सिर्फ़ चुने गए कॉलम में बदलाव करने के लिए, अनुरोध में कॉलम नंबर की सूची शामिल करें. टेबल के सभी कॉलम में बदलाव करने के लिए, खाली सूची दें.
नीचे दिए गए उदाहरण में, टेबल के कॉलम की चौड़ाई अपडेट की गई है. इसमें सभी कॉलम की चौड़ाई 100 पॉइंट पर सेट की गई है. इसके बाद, पहले कॉलम की चौड़ाई 200 पॉइंट पर सेट की गई है:
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()
पंक्ति की स्टाइल में बदलाव करना
UpdateTableRowsStyleRequest की मदद से, टेबल में मौजूद एक या उससे ज़्यादा पंक्तियों के स्टाइल में बदलाव किया जा सकता है.
आपको टेबल का शुरुआती इंडेक्स और TableRowStyle ऑब्जेक्ट देना होगा. सिर्फ़ चुनी गई पंक्तियों में बदलाव करने के लिए, अनुरोध में पंक्ति संख्याओं की सूची शामिल करें. टेबल की सभी पंक्तियों में बदलाव करने के लिए, खाली सूची दें.
इस उदाहरण में, टेबल की तीसरी लाइन की कम से कम ऊंचाई सेट की गई है:
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()