टेबल के साथ काम करना

Google Docs API से, आपको टेबल के कॉन्टेंट में बदलाव करने की सुविधा मिलती है. आपके पास ये कार्रवाइयां शामिल हैं:

  • पंक्तियां, कॉलम या पूरी टेबल शामिल करें और मिटाएं.
  • टेबल की सेल में कॉन्टेंट डालें.
  • टेबल सेल का कॉन्टेंट पढ़ना.
  • कॉलम प्रॉपर्टी और पंक्तियों की स्टाइल में बदलाव करें.

Google Docs में मौजूद टेबल को दस्तावेज़ में स्ट्रक्चरल एलिमेंट के तौर पर दिखाया जाता है. हर टेबल में टेबल सेल की सूची होती है, जहां टेबल सेल की सूची होती है. सभी स्ट्रक्चरल एलिमेंट की तरह ही, टेबल में भी शुरुआती और आखिरी इंडेक्स होते हैं, जो दस्तावेज़ में टेबल की स्थिति दिखाते हैं. इंडेक्स करने के बारे में ज़्यादा जानकारी के लिए स्ट्रक्चर देखें. टेबल की प्रॉपर्टी में कॉलम की चौड़ाई और पैडिंग जैसे कई स्टाइल एलिमेंट शामिल होते हैं.

नीचे दिया गया 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())
                .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': ''
          }
      },
  }
  ]

  result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

इसी उदाहरण में, पहले शामिल की गई टेबल को मिटाने का तरीका बताया गया है:

Java

// Delete a table that was inserted at the start of the body.
// (The table is the second element in the body:
//  document.getBody().getContent().get(2).)

Document document = docsService.documents().get(DOCUMENT_ID).execute();
StructuralElement table = document.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()))));

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.
  # (The table is the second element in the body: ['body']['content'][2].)

  document = service.documents().get(documentId=DOCUMENT_ID).execute()
  table = document['body']['content'][2]

  requests = [{
      'deleteContentRange': {
        'range': {
          'segmentId': '',
          'startIndex': table['startIndex'],
          'endIndex':   table['endIndex']
        }
      },
  }
  ]

  result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()

टेबल को सामान्य कॉन्टेंट वाली सूची से मिटा दिया जाता है. शुरुआत और आखिर के इंडेक्स को तय करके, आपको इन इंडेक्स को कहीं से भी हासिल करना होता है. इस उदाहरण में, दस्तावेज़ के कॉन्टेंट से इन इंडेक्स को निकालने का एक तरीका बताया गया है.

पंक्तियां शामिल करना और मिटाना

अगर आपके दस्तावेज़ में पहले से ही कोई टेबल है, तो Google Docs एपीआई आपको टेबल की पंक्तियां डालने और मिटाने की सुविधा देता है. किसी टेबल सेल के ऊपर या नीचे पंक्तियां डालने के लिए, InsertTableRowRequest का इस्तेमाल करें और किसी सेल की जगह को दिखाने वाली पंक्ति हटाने के लिए DeleteTableRowRequest का इस्तेमाल करें.

नीचे दिए गए उदाहरण में, टेबल की पहली टेबल सेल में टेक्स्ट डाला गया है और टेबल की एक पंक्ति जोड़ी गई है.

Java

List<Request> requests = new ArrayList<>();
requests.add(new Request().setInsertText(new InsertTextRequest()
        .setText("Hello")
        .setLocation(new Location().setIndex(5))));
requests.add(new Request().setInsertTableRow(new InsertTableRowRequest()
        .setTableCellLocation(new TableCellLocation()
                .setTableStartLocation(new Location()
                        .setIndex(2))
                .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
          },
          'text': 'Hello'
      }
    },
    {
      'insertTableRow': {
          'tableCellLocation': {
              'tableStartLocation': {
                      'index': 2
              },
              '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))
                        .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
          },
          '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))
                .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))
                .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},
      'columnIndices': [],
      'tableColumnProperties': {
        'widthType': 'FIXED_WIDTH',
        'width': {
          'magnitude': 100,
          'unit': 'PT'
        }
      },
      'fields': '*'
    },
    'updateTableColumnProperties': {
      'tableStartLocation': {'index': 2},
      '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 ऑब्जेक्ट के साथ, टेबल का शुरुआती इंडेक्स देना होगा. सिर्फ़ चुनी गई पंक्तियों में बदलाव करने के लिए, अनुरोध में पंक्तियों की संख्या शामिल करें. साथ ही, टेबल की सभी पंक्तियों में बदलाव करने के लिए, एक खाली सूची दें.

यहां दिए गए उदाहरण में, टेबल की पंक्ति 3 की कम से कम ऊंचाई सेट की गई है:

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))
                .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},
          'rowIndices': [3],
          'tableRowStyle': {
              'minRowHeight': {
                'magnitude': 18,
                'unit': 'PT'
              }
          },
          'fields': '*'
      },
  }
  ]

  result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()