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