איך לעבוד עם טבלאות ב‑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.

מדריך שלב אחר שלב

שלב 1: בנייה וקישור הספרייה

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 .

שלב 2: צור או פתח מצגת

#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;
}

שלב 3: הגדר רוחבי עמודות וגבהי שורות

טבלאות דורשות רוחבי עמודות וגבהי שורות מפורשים בנקודות (1 נקודה = 1/72 אינץ’). שקופית סטנדרטית היא ברוחב של 720 נקודות ובגובה של 540 נקודות.

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

שלב 4: הוסף את הטבלה

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;
}

שלב 5: הגדר טקסט בתא

גש לתאים דרך 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;
}

שלב 6: עצב את טקסט התא בכותרת

החל עיצוב מודגש על תאי כותרת באמצעות 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 עם 3 אלמנטים, אינדקסי שורה תקינים הם 0, 1, 2.

טקסט התא אינו מופיע בקובץ השמור

תמיד הקצה דרך .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 לקבלת הרשימה המלאה של תכונות פורמט הגבול.


ראה גם

 עברית