TableRow / TableCell

TableRow / TableCell — Aspose.Note FOSS for Python API Reference

คลาส: TableRow

Properties: aspose.note Properties: from aspose.note import TableRow Properties: CompositeNode

TableRow แสดงถึงแถวเดียวภายใน a Table. ลูกโดยตรงของมันคือ TableCell โหนด.


Properties

TableRow ไม่มีคุณสมบัติเพิ่มเติมนอกจากที่สืบทอดมาจาก CompositeNode และ Node.

สืบทอดจาก CompositeNode

Property / MethodProperties
FirstChildลูกโดยตรงแรก TableCell, หรือ None
LastChildลูกโดยตรงสุดท้าย TableCell,หรือ None
AppendChildLast(node)เพิ่มต่อท้าย TableCell
AppendChildFirst(node)เพิ่มที่ต้น TableCell
InsertChild(index, node)แทรก TableCell ที่ตำแหน่งเฉพาะ
RemoveChild(node)ลบ TableCell
GetChildNodes(Type)การค้นหาแบบลึกแรกแบบเรียกซ้ำ
for cell in rowวนซ้ำโดยตรง TableCell ลูก

สืบทอดจาก Node

PropertiesProperties
ParentNodeส่วนที่บรรจุ Table
Documentราก Document

คลาส: TableCell

Properties: aspose.note Properties: from aspose.note import TableCell Properties: CompositeNode

TableCell เป็นการแทนเซลล์เดียวภายใน TableRow. มันสามารถบรรจุ RichText, Image, และโหนดเนื้อหาอื่น ๆ เป็นลูก.


Properties

TableCell ไม่มีคุณสมบัติเพิ่มเติมนอกจากที่สืบทอดมาจาก CompositeNode และ Node.

สืบทอดจาก CompositeNode

คุณสมบัติ / วิธีการProperties
FirstChildลูกเนื้อหาโดยตรงแรก
LastChildลูกเนื้อหาโดยตรงสุดท้าย
AppendChildLast(node)เพิ่มโหนดเนื้อหาไปยังเซลล์นี้
AppendChildFirst(node)เพิ่มโหนดเนื้อหาที่จุดเริ่มต้น
InsertChild(index, node)แทรกในตำแหน่งที่กำหนด
RemoveChild(node)ลบโหนดเนื้อหา
GetChildNodes(Type)การค้นหาแบบลึก-แรกแบบเรียกซ้ำ
for child in cellวนซ้ำลูกโดยตรง

สืบทอดจาก Node

PropertiesProperties
ParentNodeส่วนที่บรรจุ TableRow
Documentราก Document

ตัวอย่างการใช้งาน

วนซ้ำทุกแถวและเซลล์ของตาราง

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print(f"Table column widths: {[col.Width for col in table.Columns]}")
    for row in table.GetChildNodes(TableRow):
        row_texts = []
        for cell in row.GetChildNodes(TableCell):
            texts = cell.GetChildNodes(RichText)
            cell_text = " ".join(rt.Text for rt in texts if rt.Text)
            row_texts.append(cell_text)
        print("  |", " | ".join(row_texts), "|")

ดึงข้อความจากทุกเซลล์เป็นรายการแบน

from aspose.note import Document, TableCell, RichText

doc = Document("MyNotes.one")
for cell in doc.GetChildNodes(TableCell):
    for rt in cell.GetChildNodes(RichText):
        if rt.Text.strip():
            print(rt.Text)

สร้างตารางโดยโปรแกรม

from aspose.note import (
    Document, Page, Outline, OutlineElement,
    Table, TableRow, TableCell, RichText, SaveFormat
)

doc = Document()
page = doc.AppendChildLast(Page())
outline = page.AppendChildLast(Outline())
elem = outline.AppendChildLast(OutlineElement())

from aspose.note import TableColumn
table = elem.AppendChildLast(Table(
    IsBordersVisible=True,
    Columns=[TableColumn(Width=100.0), TableColumn(Width=200.0)],
))

headers = ["Name", "Value"]
row = table.AppendChildLast(TableRow())
for header_text in headers:
    cell = row.AppendChildLast(TableCell())
    rt = cell.AppendChildLast(RichText())
    rt.Append(header_text)

data_row = table.AppendChildLast(TableRow())
for value in ["Item A", "42"]:
    cell = data_row.AppendChildLast(TableCell())
    rt = cell.AppendChildLast(RichText())
    rt.Append(value)

doc.Save("table-output.pdf", SaveFormat.Pdf)

นับจำนวนแถวและเซลล์

from aspose.note import Document, Table, TableRow, TableCell

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    rows = table.GetChildNodes(TableRow)
    cells = table.GetChildNodes(TableCell)
    print(f"Rows: {len(rows)}  Cells: {len(cells)}")

ความปลอดภัยจาก None

TableRow และ TableCell ไม่มีคุณสมบัติของตนที่เป็น nullable. รายการลูกที่ส่งกลับโดย GetChildNodes เป็นรายการเสมอ (อาจว่างเปล่า).


ดูเพิ่มเติม

  • Properties: พาเรนท์ของ TableRow
  • RichText: เนื้อหาหลักภายใน TableCell
  • Properties: เนื้อหาภาพภายใน TableCell
  • CompositeNode: วิธีการจัดการลูกที่สืบทอด
 ภาษาไทย