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 रेफ़रेंस देखें।.


संबंधित देखें

 हिन्दी