مدیریت جدولهای ارائه در PHP
مقدمه
جداول در پاورپوینت اطلاعات را به سطرها و ستونها سازماندهی میکنند تا خواندن و مقایسه مقادیر آسانتر شود.
Aspose.Slides کلاسهای جدول ، سلول و انواع دیگر را فراهم میکند تا بتوانید جداول را در ارائهها ایجاد، بهروزرسانی و مدیریت کنید.
ایجاد جدول از ابتدا
با تعیین موقعیت، عرض ستونها و ارتفاع سطرها یک جدول ایجاد کنید. پس از افزودن آن به اسلاید، میتوانید حاشیههای سلول را قالببندی کنید، سلولها را ادغام کنید و متن وارد کنید.
- یک نمونه از کلاس Presentation ایجاد کنید.
- یک مرجع به اسلاید را بر اساس شاخص آن دریافت کنید.
- یک آرایه از عرض ستونها را بر حسب نقطه تعریف کنید.
- یک آرایه از ارتفاع سطرها را بر حسب نقطه تعریف کنید.
- یک شیء Table را از طریق متد addTable به اسلاید اضافه کنید.
- در هر Cell پیمایش کنید تا قالببندی حاشیههای بالا، پایین، راست و چپ را اعمال کنید.
- دو سلول اول ردیف اول جدول را ادغام کنید.
- از طریق متد getTextFrame به سلول ادغامشده دسترسی پیدا کنید.
- متن را در سلول ادغامشده تنظیم کنید.
- ارائه تغییر یافته را ذخیره کنید.
مثال زیر جدولی با سه ستون و پنج ردیف در موقعیت (100, 50) نقطه ایجاد میکند. حاشیههای قرمز با عرض 5 نقطه اعمال میشود، دو سلول اول ردیف اول ادغام میشود و نتیجه به عنوان table.pptx ذخیره میشود.
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$red = java("java.awt.Color")->RED;
$slide = $presentation->getSlides()->get_Item(0);
$columnWidths = [ 50, 50, 50 ];
$rowHeights = [ 50, 30, 30, 30, 30 ];
$table = $slide->getShapes()->addTable(100, 50, $columnWidths, $rowHeights);
for ($rowIndex = 0; $rowIndex < java_values($table->getRows()->size()); $rowIndex++) {
$row = $table->getRows()->get_Item($rowIndex);
for ($columnIndex = 0; $columnIndex < java_values($row->size()); $columnIndex++) {
$cell = $row->get_Item($columnIndex);
$cellFormat = $cell->getCellFormat();
$cellFormat->getBorderTop()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderTop()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderTop()->setWidth(5);
$cellFormat->getBorderBottom()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderBottom()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderBottom()->setWidth(5);
$cellFormat->getBorderLeft()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderLeft()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderLeft()->setWidth(5);
$cellFormat->getBorderRight()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderRight()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderRight()->setWidth(5);
}
}
$table->mergeCells($table->get_Item(0, 0), $table->get_Item(1, 0), false);
$table->get_Item(0, 0)->getTextFrame()->setText("Merged Cells");
$presentation->save("table.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
شمارهگذاری در جدول استاندارد
در یک جدول استاندارد، شاخصهای سلول از صفر شروع میشوند و به ترتیب (ستون، ردیف) هستند. اولین سلول به صورت (0, 0) اندیسگذاری میشود.
به عنوان مثال، سلولهای یک جدول با 4 ستون و 4 ردیف به این شکل شمارهگذاری میشوند:
| (0, 0) | (1, 0) | (2, 0) | (3, 0) |
|---|---|---|---|
| (0, 1) | (1, 1) | (2, 1) | (3, 1) |
| (0, 2) | (1, 2) | (2, 2) | (3, 2) |
| (0, 3) | (1, 3) | (2, 3) | (3, 3) |
این مثال جدول 4 × 4 نشان داده شده در بالا را ایجاد میکند، با عرض ستونها و ارتفاع سطرها برابر 70 نقطه و حاشیههای سلولی قرمز با عرض 5 نقطه. مختصاتها شاخصهای سلول را نشان میدهند؛ این مثال سلولها را خالی میگذارد و جدول را به عنوان StandardTables_out.pptx ذخیره میکند.
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$red = java("java.awt.Color")->RED;
$slide = $presentation->getSlides()->get_Item(0);
$columnWidths = [ 70, 70, 70, 70 ];
$rowHeights = [ 70, 70, 70, 70 ];
$table = $slide->getShapes()->addTable(100, 50, $columnWidths, $rowHeights);
for ($rowIndex = 0; $rowIndex < java_values($table->getRows()->size()); $rowIndex++) {
$row = $table->getRows()->get_Item($rowIndex);
for ($columnIndex = 0; $columnIndex < java_values($row->size()); $columnIndex++) {
$cell = $row->get_Item($columnIndex);
$cellFormat = $cell->getCellFormat();
$cellFormat->getBorderTop()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderTop()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderTop()->setWidth(5);
$cellFormat->getBorderBottom()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderBottom()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderBottom()->setWidth(5);
$cellFormat->getBorderLeft()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderLeft()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderLeft()->setWidth(5);
$cellFormat->getBorderRight()->getFillFormat()->setFillType(FillType::Solid);
$cellFormat->getBorderRight()->getFillFormat()->getSolidFillColor()->setColor($red);
$cellFormat->getBorderRight()->setWidth(5);
}
}
$presentation->save("StandardTables_out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
دسترسی به جدول موجود
جداول در مجموعهٔ شکلهای یک اسلاید ذخیره میشوند. با پیمایش شکلها جدول را پیدا کنید، سپس از کلاس Table برای خواندن یا بهروزرسانی سلولهای آن استفاده کنید.
- ارائه را با استفاده از کلاس Presentation بارگذاری کنید.
- مرجع اسلاید حاوی جدول را بر اساس شاخص آن دریافت کنید.
- در اشیاء Shape پیمایش کنید و زمانی که جدول یافت شد توقف کنید. اگر اسلاید شامل چند جدول باشد، از getAlternativeText برای شناسایی جدول مورد نیاز استفاده کنید.
- متن سلول هدف را بهروزرسانی کنید.
- ارائه تغییر یافته را ذخیره کنید.
مثال زیر فایل UpdateExistingTable.pptx را باز میکند و اولین جدول در اولین اسلاید را پیدا میکند. سلول در ستون 0، ردیف 1 را به مقدار New تنظیم میکند و نتیجه را به عنوان table1_out.pptx ذخیره میکند. ورودی باید حداقل یک اسلاید داشته باشد و اولین جدول در آن اسلاید باید حداقل یک ستون و دو ردیف داشته باشد.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("UpdateExistingTable.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$table = null;
$tableClass = new JavaClass("com.aspose.slides.Table");
$shapeCount = java_values($slide->getShapes()->size());
for ($shapeIndex = 0; $shapeIndex < $shapeCount; $shapeIndex++) {
$shape = $slide->getShapes()->get_Item($shapeIndex);
if (java_instanceof($shape, $tableClass)) {
$table = $shape;
break;
}
}
if ($table !== null) {
$table->get_Item(0, 1)->getTextFrame()->setText("New");
$presentation->save("table1_out.pptx", SaveFormat::Pptx);
}
} finally {
$presentation->dispose();
}
برای تغییر اندازهٔ یک سطر در جدول موجود و درک دلیل اینکه چرا ارتفاع واقعی میتواند بیش از حداقل درخواستشده باشد، به کنترل ارتفاع سطر مراجعه کنید.
یافتن سلولی که TextFrame صاحب آن است
هنگامی که کد عمومی پردازش متن یک TextFrame را از یک جدول دریافت میکند، از متد TextFrame::getParentCell برای بازیابی Cell صاحب آن استفاده کنید. برای یک TextFrame سلول‑جدول، TextFrame::getParentCell صاحب را برمیگرداند و TextFrame::getParentShape مقدار null برمیگرداند، حتی اگر جدول خود یک شکل باشد.
مختصات سلولها از طریق متدهای فقط‑خواندنی Cell::getFirstColumnIndex و Cell::getFirstRowIndex در دسترس هستند. همچنین TextFrame::getParentCell ناوبری فقط‑خواندنی را فراهم میکند: صاحب را برمیگرداند اما مالکیت را تغییر نمیدهد. همواره قبل از استفاده، سلول بازگردانده شده را با java_is_null بررسی کنید.
برای یک مثال کامل که مالکین سلول‑جدول و شکل را شناسایی میکند، از جمله اشکالی که با گرههای SmartArt مرتبط هستند، به جستجو و جایگزینی متن مراجعه کنید.
تراز کردن متن در جدول
میتوانید تثبیت عمودی و جهت متن سلولهای فردی جدول را کنترل کنید. مثال در این بخش متن را در داخل اولین سلول بهصورت مرکزی قرار میدهد و به اندازهٔ 270 درجه چرخش میدهد.
- یک نمونه از کلاس Presentation ایجاد کنید.
- مرجع اسلاید را بر اساس شاخص آن دریافت کنید.
- یک شیء Table را به اسلاید اضافه کنید.
- یک شیء TextFrame را از جدول دسترسی پیدا کنید.
- اولین Paragraph را دسترسی یافته و متن و رنگ آن را تنظیم کنید.
- تثبیت عمودی سلول و جهت متن را با استفاده از setTextAnchorType و setTextVerticalType تنظیم کنید.
- ارائه تغییر یافته را ذخیره کنید.
این مثال جدول 4 × 4 ای با عرض ستونهای 120 نقطه و ارتفاع سطرهای 100 نقطه ایجاد میکند. متن سلول (0, 0) را قالببندی میکند، مقادیر را به بقیه سلولهای ردیف اول اضافه میکند و نتیجه را به عنوان Vertical_Align_Text_out.pptx ذخیره میکند.
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextAnchorType;
use aspose\slides\TextVerticalType;
$presentation = new Presentation();
try {
$black = java("java.awt.Color")->BLACK;
$slide = $presentation->getSlides()->get_Item(0);
$columnWidths = [ 120, 120, 120, 120 ];
$rowHeights = [ 100, 100, 100, 100 ];
$table = $slide->getShapes()->addTable(100, 50, $columnWidths, $rowHeights);
$table->get_Item(1, 0)->getTextFrame()->setText("10");
$table->get_Item(2, 0)->getTextFrame()->setText("20");
$table->get_Item(3, 0)->getTextFrame()->setText("30");
$textFrame = $table->get_Item(0, 0)->getTextFrame();
$paragraph = $textFrame->getParagraphs()->get_Item(0);
$portion = $paragraph->getPortions()->get_Item(0);
$portion->setText("Text here");
$portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($black);
$cell = $table->get_Item(0, 0);
$cell->setTextAnchorType(TextAnchorType::Center);
$cell->setTextVerticalType(TextVerticalType::Vertical270);
$presentation->save("Vertical_Align_Text_out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
تنظیم قالببندی متن در سطح جدول
از setTextFormat برای اعمال قالببندی متن به تمام سلولهای یک جدول استفاده کنید. بارگذاریهای آن بخش، پاراگراف و قالببندی چارچوب متن را میپذیرند، بهطوری که میتوانید این ویژگیها را بدون پیمایش سلولهای فردی تنظیم کنید.
- ارائه را با استفاده از کلاس Presentation بارگذاری کنید.
- مرجع اسلاید را بر اساس شاخص آن بگیرید.
- یک شیء Table را از اسلاید دسترسی پیدا کنید.
- اندازهٔ قلم را با استفاده از setFontHeight برای متن تنظیم کنید.
- ترازبندی پاراگراف و حاشیهٔ راست را با استفاده از setAlignment و setMarginRight تنظیم کنید.
- جهت متن را با استفاده از setTextVerticalType تنظیم کنید.
- ارائه تغییر یافته را ذخیره کنید.
مثال زیر فایل table.pptx را باز میکند که باید حداقل یک اسلاید داشته باشد که اولین شکل آن یک جدول باشد. اندازهٔ قلم را به 25 نقطه تنظیم میکند، پاراگرافها را راستچین با حاشیهٔ راست 20 نقطه میکند و متن را عمودی میسازد. ارائه قالببندیشده به عنوان result.pptx ذخیره میشود.
use aspose\slides\ParagraphFormat;
use aspose\slides\PortionFormat;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextAlignment;
use aspose\slides\TextFrameFormat;
use aspose\slides\TextVerticalType;
$presentation = new Presentation("table.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$table = $slide->getShapes()->get_Item(0);
$portionFormat = new PortionFormat();
$portionFormat->setFontHeight(25);
$table->setTextFormat($portionFormat);
$paragraphFormat = new ParagraphFormat();
$paragraphFormat->setAlignment(TextAlignment::Right);
$paragraphFormat->setMarginRight(20);
$table->setTextFormat($paragraphFormat);
$textFrameFormat = new TextFrameFormat();
$textFrameFormat->setTextVerticalType(TextVerticalType::Vertical);
$table->setTextFormat($textFrameFormat);
$presentation->save("result.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
دریافت ویژگیهای سبک جدول
از getStylePreset برای خواندن سبک پیشتنظیمشدهٔ جدول و از setStylePreset برای اختصاص آن استفاده کنید. این مثال TableStylePreset::DarkStyle1 را بر یک جدول اعمال میکند، مقدار پیشتنظیم را چاپ میکند و همان پیشتنظیم را به جدول دوم اختصاص میدهد. هر دو جدول در table-style.pptx ذخیره میشوند.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TableStylePreset;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$columnWidths = [ 100, 150 ];
$rowHeights = [ 5, 5, 5 ];
$table = $slide->getShapes()->addTable(10, 10, $columnWidths, $rowHeights);
$table->setStylePreset(TableStylePreset::DarkStyle1);
$stylePreset = java_values($table->getStylePreset());
echo "Table style preset: " . $stylePreset . PHP_EOL;
$anotherTable = $slide->getShapes()->addTable(10, 100, $columnWidths, $rowHeights);
$anotherTable->setStylePreset($stylePreset);
$presentation->save("table-style.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
قفل کردن نسبت ابعاد جدول
نسبت ابعاد جدول، نسبت عرض آن به ارتفاع است. از setAspectRatioLocked برای قفل کردن این نسبت برای جدول استفاده کنید.
مثال زیر فایل pres.pptx را باز میکند که باید حداقل یک اسلاید داشته باشد که اولین شکل آن یک جدول باشد. حالت قفل فعلی را چاپ میکند، قفل نسبت ابعاد را فعال میکند، حالت بهروزشده (true) را چاپ میکند و نتیجه را به عنوان pres-out.pptx ذخیره میکند.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("pres.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$table = $slide->getShapes()->get_Item(0);
echo "Lock aspect ratio set: " . (java_values($table->getGraphicalObjectLock()->getAspectRatioLocked()) ? "true" : "false") . PHP_EOL;
$table->getGraphicalObjectLock()->setAspectRatioLocked(true);
echo "Lock aspect ratio set: " . (java_values($table->getGraphicalObjectLock()->getAspectRatioLocked()) ? "true" : "false") . PHP_EOL;
$presentation->save("pres-out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
FAQ
آیا میتوانم جهت خواندن راست‑به‑چپ (RTL) را برای یک جدول کامل و متن داخل سلولهای آن فعال کنم؟
بله. جدول یک متد setRightToLeft را ارائه میدهد و پاراگرافها متد ParagraphFormat::setRightToLeft دارند. استفاده از هر دو اطمینان میدهد که ترتیب RTL صحیح بوده و رندر داخل سلولها به درستی انجام شود.
چگونه میتوانم از جابجا یا تغییر اندازهٔ جدول توسط کاربران در فایل نهایی جلوگیری کنم؟
از shape locks برای غیرفعالسازی جابجایی، تغییر اندازه، انتخاب و غیره استفاده کنید. این قفلها بر جداول نیز اعمال میشوند.
آیا درج تصویر بهعنوان پسزمینه داخل یک سلول پشتیبانی میشود؟
بله. میتوانید برای یک سلول یک picture fill تنظیم کنید؛ تصویر بهموجب حالت انتخابی (کشیدن یا کاشی) کل ناحیهٔ سلول را پوشش میدهد.