การทำงานกับตาราง

คุณสามารถใช้ Google เอกสาร API เพื่อแก้ไขสารบัญ การดำเนินการที่คุณทำได้มีดังนี้

  • แทรกและลบแถว คอลัมน์ หรือทั้งตาราง
  • แทรกเนื้อหาลงในเซลล์ของตาราง
  • อ่านเนื้อหาจากเซลล์ของตาราง
  • แก้ไขพร็อพเพอร์ตี้ของคอลัมน์และรูปแบบของแถว

ตารางใน Google เอกสาร จะแสดงเป็น 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())
                .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 เอกสาร API จะให้คุณแทรกและลบแถวของตาราง ใช้ 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 ช่วยให้คุณแก้ไขคุณสมบัติของคอลัมน์อย่างน้อย 1 คอลัมน์ในตารางได้

คุณต้องระบุดัชนีเริ่มต้นของตาราง พร้อมด้วยออบเจ็กต์ TableColumnProperties หากต้องการแก้ไขเฉพาะคอลัมน์ที่เลือก ให้ใส่รายการหมายเลขคอลัมน์ไว้ในคำขอ หากต้องการแก้ไขคอลัมน์ทั้งหมดในตาราง ให้ระบุรายการที่ว่างเปล่า

ตัวอย่างต่อไปนี้อัปเดตความกว้างของคอลัมน์ของตารางโดยตั้งค่าคอลัมน์ทั้งหมดให้กว้าง 100 pts จากนั้นความกว้างของคอลัมน์แรกเป็น 200 pt

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 ช่วยให้คุณแก้ไขรูปแบบของแถวอย่างน้อย 1 แถวในตารางได้

คุณต้องระบุดัชนีเริ่มต้นของตาราง พร้อมด้วยออบเจ็กต์ 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()