نحوه کار با جدول‌ها در C++

نحوه کار با جدول‌ها در C++

Aspose.Slides FOSS for C++ supports creating tables on slides with configurable column widths and row heights. This guide shows how to add a table, populate it with data, and apply basic text formatting to cells.

راهنمای گام به گام

مرحله ۱: ساخت و لینک کردن کتابخانه

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

مرحله ۲: ایجاد یا باز کردن یک ارائه

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    // ... add table ...
    prs.save("table.pptx", asf::SaveFormat::PPTX);
    return 0;
}

مرحله ۳: تعریف عرض ستون‌ها و ارتفاع ردیف‌ها

جدول‌ها نیاز به عرض ستون‌ها و ارتفاع ردیف‌های صریح بر حسب پوینت دارند (۱ پوینت = ۱/۷۲ اینچ). یک اسلاید استاندارد عرض ۷۲۰ پوینت و ارتفاع ۵۴۰ پوینت دارد.

std::vector<double> col_widths = {200.0, 150.0, 150.0};  // 3 columns
std::vector<double> row_heights = {45.0, 40.0, 40.0};    // 3 rows

مرحله ۴: افزودن جدول

slide.shapes().add_table(x, y, col_widths, row_heights) جدول را در موقعیت ایجاد می‌کند (x, y):

#include <Aspose/Slides/Foss/presentation.h>
#include <vector>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    std::vector<double> col_widths = {200.0, 150.0, 150.0};
    std::vector<double> row_heights = {45.0, 40.0, 40.0};
    auto& table = slide.shapes().add_table(50, 100, col_widths, row_heights);

    prs.save("table.pptx", asf::SaveFormat::PPTX);
    return 0;
}

مرحله ۵: تنظیم متن سلول

دسترسی به سلول‌ها از طریق table.rows()[row_index][col_index] و متن را از طریق اختصاص دهید .text_frame()->set_text():

#include <Aspose/Slides/Foss/presentation.h>
#include <vector>
#include <string>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    std::vector<double> col_widths = {200.0, 150.0, 150.0};
    std::vector<double> row_heights = {45.0, 40.0, 40.0};
    auto& table = slide.shapes().add_table(50, 100, col_widths, row_heights);

    // Header row (row 0)
    std::vector<std::string> headers = {"Product", "Units Sold", "Revenue"};
    for (size_t col = 0; col < headers.size(); ++col) {
        table.rows()[0][col].text_frame()->set_text(headers[col]);
    }

    // Data rows
    std::vector<std::vector<std::string>> data = {
        {"Widget A", "1,200", "$24,000"},
        {"Widget B", "850",   "$17,000"},
    };
    for (size_t r = 0; r < data.size(); ++r) {
        for (size_t c = 0; c < data[r].size(); ++c) {
            table.rows()[r + 1][c].text_frame()->set_text(data[r][c]);
        }
    }

    prs.save("sales-table.pptx", asf::SaveFormat::PPTX);
    return 0;
}

مرحله ۶: قالب‌بندی متن سلول سرصفحه

قالب‌بندی بولد را بر روی سلول‌های سرصفحه با استفاده از اعمال کنید PortionFormat:

for (size_t col = 0; col < headers.size(); ++col) {
    auto& cell = table.rows()[0][col];
    auto& portions = cell.text_frame()->paragraphs()[0].portions();
    if (portions.size() > 0) {
        auto& fmt = portions[0].portion_format();
        fmt.set_font_bold(asf::NullableBool::TRUE);
        fmt.fill_format().set_fill_type(asf::FillType::SOLID);
        fmt.fill_format().solid_fill_color().set_color(
            asf::Color::from_argb(255, 255, 255, 255));
    }
}

مثال کامل کارا

#include <Aspose/Slides/Foss/presentation.h>
#include <vector>
#include <string>
#include <iostream>

int main() {
    namespace asf = Aspose::Slides::Foss;

    std::vector<std::vector<std::string>> data_rows = {
        {"North", "$1.2M", "+8%"},
        {"South", "$0.9M", "+4%"},
        {"East",  "$1.5M", "+12%"},
        {"West",  "$0.7M", "+2%"},
    };
    std::vector<std::string> headers = {"Region", "Revenue", "Growth"};

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    std::vector<double> col_widths = {180.0, 140.0, 120.0};
    std::vector<double> row_heights(1 + data_rows.size(), 38.0);
    row_heights[0] = 45.0;

    auto& table = slide.shapes().add_table(60, 80, col_widths, row_heights);

    // Header row
    for (size_t col = 0; col < headers.size(); ++col) {
        auto& cell = table.rows()[0][col];
        cell.text_frame()->set_text(headers[col]);
        auto& portions = cell.text_frame()->paragraphs()[0].portions();
        if (portions.size() > 0) {
            portions[0].portion_format().set_font_bold(asf::NullableBool::TRUE);
        }
    }

    // Data rows
    for (size_t r = 0; r < data_rows.size(); ++r) {
        for (size_t c = 0; c < data_rows[r].size(); ++c) {
            table.rows()[r + 1][c].text_frame()->set_text(data_rows[r][c]);
        }
    }

    prs.save("regional-revenue.pptx", asf::SaveFormat::PPTX);
    std::cout << "Saved regional-revenue.pptx\n";
    return 0;
}

مشکلات رایج و راه‌حل‌ها

std::out_of_range هنگام دسترسی table.rows()[row][col]

اندیس‌های ردیف و ستون از صفر شروع می‌شوند. اگر شما تعریف کرده‌اید row_heights با ۳ عنصر، اندیس‌های ردیف معتبر ۰، ۱، ۲ هستند.

متن سلول در فایل ذخیره‌شده نمایش داده نمی‌شود

همیشه از طریق اختصاص دهید .text_frame()->set_text(), نه از طریق .set_text() مستقیماً روی شی سلول.

موقعیت جدول خارج از اسلاید است

بررسی کنید که x + sum(col_widths) <= 720 و y + sum(row_heights) <= 540 برای یک اسلاید استاندارد.


سوالات متداول

آیا می‌توانم سلول‌های جدول را ادغام کنم؟?

ادغام سلول‌ها در این نسخه پشتیبانی نمی‌شود.

آیا می‌توانم رنگ پس‌زمینه‌ای برای کل جدول اعمال کنم؟?

قالب‌گذاری پر کردن را برای هر سلول به‌صورت جداگانه اعمال کنید:

for (size_t r = 0; r < table.rows().size(); ++r) {
    for (size_t c = 0; c < table.rows()[r].size(); ++c) {
        auto& cell = table.rows()[r][c];
        cell.fill_format().set_fill_type(asf::FillType::SOLID);
        cell.fill_format().solid_fill_color().set_color(
            asf::Color::from_argb(255, 240, 248, 255));
    }
}

آیا می‌توانم سبک‌های حاشیه سلول را تنظیم کنم؟?

ویژگی‌های حاشیه سلول از طریق متدهای حاشیه قالب سلول قابل دسترسی هستند. برای فهرست کامل ویژگی‌های قالب حاشیه به مرجع API مراجعه کنید.


همچنین ببینید

 فارسی