จัดการเซลล์ตารางในงานนำเสนอด้วย JavaScript
ภาพรวม
Aspose.Slides ช่วยให้คุณเข้าถึงและแก้ไขเซลล์ตารางในงานนำเสนอ PowerPoint บทความนี้อธิบายวิธีระบุเซลล์ตารางที่ผสาน, ลบขอบเซลล์, ทำงานกับการกำหนดหมายเลขเซลล์หลังจากการผสานหรือแยกเซลล์, เปลี่ยนสีพื้นหลังของเซลล์, และเพิ่มรูปภาพภายในเซลล์ตาราง ตัวอย่างจะแสดงวิธีสร้างหรือเปิดงานนำเสนอ, ดึงตารางจากสไลด์, ปรับรูปแบบเซลล์ผ่านคุณสมบัติของเซลล์, และบันทึกงานนำเสนอที่แก้ไขเป็นไฟล์ PPTX
Aspose.Slides ใช้ดัชนีเริ่มจากศูนย์ในการเข้าถึงเซลล์ตารางตามลำดับ (column, row).
ระบุเซลล์ตารางที่ผสาน
ตัวอย่างนี้เปิดงานนำเสนอที่มีอยู่และเข้าถึงรูปร่างแรกบนสไลด์แรกเป็นตาราง มันสมมุติว่ามีสไลด์และรูปร่างอยู่และรูปร่างนั้นเป็นตาราง จากนั้นวนผ่านทุกแถวและคอลัมน์และใช้ isMergedCell เพื่อระบุเซลล์ในพื้นที่ที่ผสาน สำหรับแต่ละผลการจับคู่จะพิมพ์พิกัดเซลล์ในลำดับ row;column, getRowSpan, getColSpan, และพิกัดเริ่มต้นของพื้นที่, getFirstRowIndex และ getFirstColumnIndex.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("presentation_with_table.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const table = slide.getShapes().get_Item(0);
const rowCount = table.getRows().size();
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
const columnCount = table.getColumns().size();
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
const cell = table.get_Item(columnIndex, rowIndex);
if (cell.isMergedCell()) {
console.log("Cell %d;%d belongs to a merged region with RowSpan=%d and ColSpan=%d starting at %d;%d.", rowIndex, columnIndex, cell.getRowSpan(), cell.getColSpan(), cell.getFirstRowIndex(), cell.getFirstColumnIndex());
}
}
}
} finally {
presentation.dispose();
}
ลบขอบเซลล์ตาราง
Create a Presentation and add a table to its first slide with addTable. Column widths, row heights, and the table position are specified in points. The example sets all four cell borders to FillType.NoFill, making them invisible.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [50, 50, 50, 50]);
const rowHeights = java.newArray("double", [50, 30, 30, 30, 30]);
const table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);
for (let rowIndex = 0; rowIndex < table.getRows().size(); rowIndex++) {
const row = table.getRows().get_Item(rowIndex);
for (let columnIndex = 0; columnIndex < row.size(); columnIndex++) {
const cell = row.get_Item(columnIndex);
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
}
}
presentation.save("table.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
เพิ่มขอบเซลล์แนวทแยง
Use getBorderDiagonalDown to draw a line from the top-left corner to the bottom-right, and getBorderDiagonalUp to draw a line from the bottom-left corner to the top-right. A diagonal can mark a corner header; both diagonals can form an X to mark a cell as not applicable.
Each diagonal is a LineFormat. In its getFillFormat, use setFillType to select FillType.Solid and choose a color through getSolidFillColor. Set the line’s width in points with setWidth.
The example creates three cells in one row: a red downward diagonal, a blue upward diagonal, and both diagonals. It leaves the top, bottom, left, and right cell borders unchanged.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [210, 210, 210]);
const rowHeights = java.newArray("double", [160]);
const table = slide.getShapes().addTable(45, 130, columnWidths, rowHeights);
const downwardBorder = table.get_Item(0, 0).getCellFormat().getBorderDiagonalDown();
downwardBorder.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
downwardBorder.getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
downwardBorder.setWidth(3);
const upwardBorder = table.get_Item(1, 0).getCellFormat().getBorderDiagonalUp();
upwardBorder.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
upwardBorder.getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
upwardBorder.setWidth(5);
const crossDownwardBorder = table.get_Item(2, 0).getCellFormat().getBorderDiagonalDown();
crossDownwardBorder.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
crossDownwardBorder.getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
crossDownwardBorder.setWidth(3);
const crossUpwardBorder = table.get_Item(2, 0).getCellFormat().getBorderDiagonalUp();
crossUpwardBorder.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
crossUpwardBorder.getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
crossUpwardBorder.setWidth(5);
presentation.save("diagonal_cell_borders.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
A diagonal is a formatting property, not a cell split. Each cell retains a single text frame, accessed through getTextFrame; the regions on either side of a diagonal cannot be edited independently.
The saved table, rendered in Microsoft PowerPoint:

จากซ้ายไปขวา: เส้นทแยงลง (สีแดง, 3 pt), เส้นทแยงขึ้น (สีน้ำเงิน, 5 pt), และเส้นทแยงทั้งสอง.
ผสานเซลล์ตาราง
Use mergeCells to combine a rectangular range of table cells into one cell. Specify the cells at the top-left and bottom-right corners of the range. The final argument controls whether the merge may include cells outside the specified range; false keeps the merge within that range.
The example creates a 4-by-4 table with 70-point columns and rows, then merges the four central cells from (1, 1) through (2, 2). The resulting cell spans two columns and two rows, while the table’s underlying grid retains four columns and four rows. To access the merged cell’s content or formatting, use its top-left position: table.get_Item(1, 1) in this example. The other positions in the merged range remain part of the table grid, so the indices of cells outside the range do not change.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [70, 70, 70, 70]);
const rowHeights = java.newArray("double", [70, 70, 70, 70]);
const table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);
table.mergeCells(table.get_Item(1, 1), table.get_Item(2, 2), false);
presentation.save("merged_cells.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
แยกเซลล์ตาราง
Merging cells in the previous example preserves the table’s grid. Splitting a cell can introduce a new grid column and change the column indices of cells to its right. Aspose.Slides follows PowerPoint’s table grid model.
This example creates a 4-by-4 table with 70-point columns and rows and calls splitByWidth on cell (1, 1). Half of the cell’s 70-point width is passed to create two equal-width cells.
After this split, the two halves are accessed as table.get_Item(1, 1) and table.get_Item(2, 1). The table grid now has five columns: cells originally in columns 2 and 3 move to columns 3 and 4, respectively. Row indices remain unchanged. Use these updated column indices when accessing cells after the split.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [70, 70, 70, 70]);
const rowHeights = java.newArray("double", [70, 70, 70, 70]);
const table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);
table.get_Item(1, 1).splitByWidth(table.get_Item(1, 1).getWidth() / 2);
presentation.save("split_cells.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
การแยกความกว้างส่งผลต่อเซลล์ (1, 1), โดยใช้ดัชนี (คอลัมน์, แถว) เริ่มจากศูนย์. แถวอื่น ๆ คงเซลล์ที่ครอบคลุมตำแหน่งคอลัมน์ใหม่ทั้งสอง.
แยกเซลล์ที่ผสานตามช่วงแถวหรือคอลัมน์
To prepare merged template cells for data population, use splitByRowSpan to split along an existing row boundary, or splitByColSpan to split along a column boundary.
The index argument counts rows in the upper part or columns in the left part of the split; it is relative to the merged region:
- การแยกแถว:
0 < index <getRowSpan. - การแยกคอลัมน์:
0 < index <getColSpan.
The example expects a presentation to have a table as the first shape on the first slide, with (1, 2) and (1, 3) merged vertically. Starting from the lower position, it uses getFirstColumnIndex and getFirstRowIndex to locate the origin and checks both spans. splitByRowSpan(1) then separates rows 2 and 3 for product names. For a horizontal two-column merge, use splitByColSpan(1) instead.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("table_template.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const table = slide.getShapes().get_Item(0);
const selectedCell = table.get_Item(1, 3);
const firstColumnIndex = selectedCell.getFirstColumnIndex();
const firstRowIndex = selectedCell.getFirstRowIndex();
const mergedCell = table.get_Item(firstColumnIndex, firstRowIndex);
if (mergedCell.isMergedCell() && mergedCell.getRowSpan() == 2 && mergedCell.getColSpan() == 1) {
mergedCell.splitByRowSpan(1);
// ดึงเซลล์ที่ได้จากตารางหลังจากการแยก.
const upperCell = table.get_Item(firstColumnIndex, firstRowIndex);
const lowerCell = table.get_Item(firstColumnIndex, firstRowIndex + 1);
console.log("Upper cell merged: " + upperCell.isMergedCell());
console.log("Lower cell merged: " + lowerCell.isMergedCell());
upperCell.getTextFrame().setText("Product A");
lowerCell.getTextFrame().setText("Product B");
presentation.save("split_template.pptx", aspose.slides.SaveFormat.Pptx);
} else {
console.log("Select a merged region spanning exactly two rows and one column.");
}
} finally {
presentation.dispose();
}
The table grid and surrounding cell indices stay unchanged. Retrieve the resulting cells by their coordinates; here, both have spans of 1 and isMergedCell prints false. Larger regions can remain partly merged after one split.
The original text and its formatting remain in the upper (or left) cell; the new cell is empty but inherits cell formatting such as fill, borders, and margins. Populate the cells after splitting and set any required text formatting explicitly.
The saved presentation contains separate “Product A” and “Product B” cells with the template’s cell formatting retained. See the Cell API Reference for details.
แยกเซลล์ที่ความสูงเฉพาะ
splitByHeight takes the height of the upper part in points. splitByRowSpan takes the number of existing grid rows in the upper part. To center a 40-point cell in the right column, subtract 40 from the cell’s actual height and divide the remaining space equally above and below it.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [160, 160]);
const rowHeights = java.newArray("double", [140]);
const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
table.get_Item(0, 0).getTextFrame().setText("Left");
const height = table.get_Item(1, 0).getHeight();
const margin = (height - 40) / 2;
table.get_Item(1, 0).splitByHeight(margin);
table.get_Item(1, 1).splitByHeight(40);
table.get_Item(1, 1).getTextFrame().setText("Middle");
presentation.save("split_by_height.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
The first split adds a grid row and leaves the lower 90 points in cell (1, 1). The second split divides that cell into a 40-point middle cell and a 50-point bottom cell. The right column now has three cells with heights of 50, 40, and 50 points from top to bottom. The left cell spans all three rows and retains its original height of 140 points.

เปลี่ยนสีพื้นหลังของเซลล์ตาราง
This example creates a table with 150-point columns and 50-point rows. It uses setFillType to select a solid fill and sets the color returned by getSolidFillColor to red for cell (2, 3), in the third column and fourth row.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [150, 150, 150, 150]);
const rowHeights = java.newArray("double", [50, 50, 50, 50, 50]);
const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
const cell = table.get_Item(2, 3);
cell.getCellFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
cell.getCellFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
presentation.save("cell_background_color.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
เพิ่มรูปภาพภายในเซลล์ตาราง
Place the input image in the working directory before running this example. It loads the image with Images.fromFile and adds it to the presentation’s image collection with addImage. It then assigns the image to the picture fill of cell (0, 0), the first cell in the table.
PictureFillMode.Stretch stretches the image to fill the cell, which may change its aspect ratio. Column widths and row heights are in points. The loaded image is disposed in a finally block after it is added to the presentation.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [150, 150, 150, 150]);
const rowHeights = java.newArray("double", [100, 100, 100, 100, 90]);
const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
let ppImage;
const image = aspose.slides.Images.fromFile("aspose_logo.jpg");
try {
ppImage = presentation.getImages().addImage(image);
} finally {
image.dispose();
}
table.get_Item(0, 0).getCellFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Picture));
table.get_Item(0, 0).getCellFormat().getFillFormat().getPictureFillFormat().setPictureFillMode(aspose.slides.PictureFillMode.Stretch);
table.get_Item(0, 0).getCellFormat().getFillFormat().getPictureFillFormat().getPicture().setImage(ppImage);
presentation.save("table_cell_with_image.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
คำถามที่พบบ่อย
ฉันสามารถตั้งความหนาและสไตล์ของเส้นต่าง ๆ สำหรับแต่ละด้านของเซลล์เดียวได้หรือไม่?
ใช่. ขอบ top/bottom/left/right มีคุณสมบัติแยกกัน, ดังนั้นความหนาและสไตล์ของแต่ละด้านจึงสามารถแตกต่างกันได้.
อะไรจะเกิดขึ้นกับรูปภาพหากฉันเปลี่ยนขนาดคอลัมน์/แถวหลังจากตั้งภาพเป็นพื้นหลังของเซลล์?
พฤติกรรมขึ้นอยู่กับ fill mode (stretch/tile). หากใช้การยืด, รูปภาพจะปรับตามเซลล์ใหม่; หากใช้การเรียงต่อ, แผ่นภาพจะถูกคำนวนใหม่.
ฉันสามารถกำหนด hyperlink ให้กับเนื้อหาทั้งหมดของเซลล์ได้หรือไม่?
Hyperlinks ถูกตั้งที่ระดับข้อความ (portion) ภายในกรอบข้อความของเซลล์หรือที่ระดับของตาราง/รูปร่างทั้งหมด. ในทางปฏิบัติคุณจะกำหนดลิงก์ให้กับ portion หรือกับข้อความทั้งหมดในเซลล์.
ฉันสามารถตั้งฟอนต์ที่แตกต่างกันภายในเซลล์เดียวได้หรือไม่?
ใช่. กรอบข้อความของเซลล์สนับสนุน portions (runs) ที่มีการจัดรูปแบบอิสระ—ฟอนต์, สไตล์, ขนาด, และสี.