Table — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss แสดงตารางโครงสร้างของ Excel (ListObjects) ผ่าน Table คลาสและ TableCollection สามารถเข้าถึงได้เป็น ws.list_objects. ตารางให้ช่วงที่ตั้งชื่อ, ตัวกรองอัตโนมัติ, แถวหัวตาราง, และการจัดรูปแบบแถบแถวที่ฝังอยู่ในรูปแบบ XLSX.
Properties: aspose.cells_foss
from aspose.cells_foss import TableStyleInfo, TableColumnTableStyleInfo
อธิบายสไตล์ภาพที่ใช้กับตาราง (การทำแถบ, การเน้นคอลัมน์แรก/สุดท้าย).
Properties
TableStyleInfo()Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | ชื่อสไตล์ของตาราง Excel (เช่น,., "TableStyleMedium9"). |
show_first_column | bool | True เพื่อใช้การจัดรูปแบบพิเศษกับคอลัมน์แรก. |
show_last_column | bool | True เพื่อใช้การจัดรูปแบบพิเศษกับคอลัมน์สุดท้าย. |
show_row_stripes | bool | True เพื่อใช้การจัดแถบแถวสลับกัน. |
show_column_stripes | bool | True เพื่อใช้การจัดแถบคอลัมน์สลับกัน. |
TableColumn
อธิบายคอลัมน์เดียวภายในตาราง.
Properties
TableColumn(col_id: int, name: str)| Properties | Properties | Properties |
|---|---|---|
col_id | int | 1-based column identifier within the table (not the worksheet column index). |
name | str | ข้อความหัวคอลัมน์. ต้องเป็นเอกลักษณ์ภายในตาราง. |
Properties
| Properties | Properties | Properties |
|---|---|---|
id | int | ตัวระบุคอลัมน์ (อ่านอย่างเดียว). |
name | str | ข้อความหัวคอลัมน์. |
totals_row_function | str | ฟังก์ชันรวมที่แสดงในแถวผลรวม: 'none', 'sum', 'average', 'count', 'countNums', 'max', 'min', 'stdDev', 'var', 'custom'. |
totals_row_label | str | ข้อความป้ายกำกับที่แสดงในเซลล์ผลรวมเมื่อ totals_row_function เป็น 'none'. |
totals_row_formula | str | สูตรกำหนดเองที่ใช้เมื่อ totals_row_function คือ 'custom'. |
Properties
Table อ็อบเจ็กต์ไม่ได้ถูกสร้างโดยตรง. ใช้ ws.list_objects.add(...) หรือ ws.list_objects.add_with_range(...).
Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | ชื่อเทเบิลภายใน (ต้องเป็นเอกลักษณ์ในเวิร์กบุ๊ก, ไม่มีช่องว่าง). |
display_name | str | ชื่อที่แสดงในกล่องชื่อของ Excel. |
ref | str | ช่วงเซลล์ที่ตารางครอบคลุม (เช่น,., "A1:D10"). |
has_headers | bool | True หากแถวแรกของช่วงมีหัวคอลัมน์. |
show_totals_row | bool | True เพื่อแสดงแถวผลรวมการรวมที่ด้านล่างของตาราง. |
show_auto_filter | bool | True เพื่อแสดงลูกศรดรอปดาวน์ของการกรองอัตโนมัติในแถวหัวตาราง. |
table_style_info | TableStyleInfo | การตั้งค่าสไตล์ภาพ (การจัดแถบ, ไฮไลท์คอลัมน์). |
columns | list[TableColumn] | รายการลำดับของ TableColumn อ็อบเจกต์ (หนึ่งต่อคอลัมน์ของตาราง). |
TableCollection
TableCollection เข้าถึงได้เป็น ws.list_objects. มันจัดการตารางที่มีโครงสร้างทั้งหมดบนแผ่นงาน.
Properties
add
ws.list_objects.add(
start_row: int,
start_col: int,
end_row: int,
end_col: int,
has_headers: bool = True,
name: str | None = None
) -> Tableสร้างตารางบนช่วงเซลล์ที่กำหนด ทั้งหมดของดัชนีคือ 0-based. หาก name คือ None, ชื่อจะถูกสร้างโดยอัตโนมัติ (เช่น,., "Table1").
add_with_range
ws.list_objects.add_with_range(
cell_range: str,
name: str | None = None,
has_headers: bool = True
) -> Tableสร้างตารางจากสตริงช่วงแบบ A1 (เช่น,., "A1:D10"). อ่านง่ายกว่า add() สำหรับช่วงที่กำหนดแบบคงที่.
คุณสมบัติและการวนซ้ำ
| Properties | Properties |
|---|---|
count | จำนวนตารางบนแผ่นงานนี้ (อ่านอย่างเดียว). |
__len__() | Properties count. |
__iter__() | วนซ้ำผ่านทั้งหมด Table อ็อบเจ็กต์. |
__getitem__(index) | คืนค่า Table โดยเริ่มจากศูนย์ index. |
Properties
ตัวอย่างต่อไปนี้เขียนชุดข้อมูลสินค้าคงคลังและแปลงเป็นตาราง Excel ที่มีสไตล์พร้อมแถวผลรวม.
from aspose.cells_foss import Workbook
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Inventory"
# Write header and data rows
headers = ["SKU", "Product", "Qty", "Unit Price"]
rows = [
["A001", "Widget Alpha", 150, 12.99],
["A002", "Widget Beta", 80, 24.50],
["A003", "Gadget Gamma", 45, 49.99],
["A004", "Gadget Delta", 210, 8.75],
]
for col, h in enumerate(headers):
ws.cells[0][col].put_value(h)
for row_i, row in enumerate(rows, start=1):
for col_i, val in enumerate(row):
ws.cells[row_i][col_i].put_value(val)
# Create a table over A1:D5
table = ws.list_objects.add_with_range("A1:D5", name="InventoryTable")
# Configure style
table.table_style_info.name = "TableStyleMedium9"
table.table_style_info.show_row_stripes = True
table.table_style_info.show_first_column = False
table.table_style_info.show_last_column = False
# Add a totals row with sum on Qty and Unit Price columns
table.show_totals_row = True
table.columns[2].totals_row_function = "sum" # Qty
table.columns[3].totals_row_function = "sum" # Unit Price
table.columns[0].totals_row_label = "Total"
wb.save("inventory_table.xlsx")