使用 C++ 在演示文稿中管理图表工作簿

概述

本文介绍了在 Aspose.Slides 中如何使用图表工作簿。它展示了如何通过工作簿流读取和写入图表数据,使用工作簿单元格作为图表数据标签,访问工作表集合,以及为图表值指定数据源类型。

它还涵盖了将外部工作簿用作图表数据源的操作。示例演示了如何创建并分配外部工作簿,检索链接到图表的外部工作簿路径,以及在工作簿可用时编辑图表数据。

有关表示缺失数据的工作簿单元格,请参阅控制空单元格的显示,了解空单元格与零的区别,以及可用显示模式的折线图比较。

包含隐藏行和列中的数据

使用IChart::set_PlotVisibleCellsOnly来控制图表是否仅绘制隐藏工作表行列中的可见单元格。将其设为 true 只绘制可见单元格,设为 false 则包括可见和隐藏单元格。此设置仅控制图表绘制;它不会隐藏或取消隐藏工作表行列。

示例演示文稿在其第一张幻灯片的第一个形状中包含一个柱形图。嵌入的工作表 Sheet1 包含以下源范围 A1:C4。第 3 行和 C 列被隐藏,但它们的单元格仍然包含值。

工作表行 A: 月份 B: 零售 C: 批发(隐藏列)
2 一月 10 30
3(隐藏行) 二月 40 60
4 三月 20 50

通过IChartData::get_ChartDataWorkbook访问源单元格,并读取IChartDataCell::get_IsHidden以检查其隐藏状态。此属性为只读。在此文件中,B2 为可见,B3 属于隐藏行,C2 属于隐藏列;示例分别输出 False、True 和 True。

对于本示例,在更改绘图设置后刷新图表数据:使用ReadWorkbookStream保留嵌入工作簿,并使用WriteWorkbookStream重新加载。当包含所有单元格时,还需使用SetRange恢复完整范围,包括隐藏的二月份类别。仅更改标志不足以刷新此示例的缓存图表数据和类别标签。

#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <initializer_list>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"hidden-source-data.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
    Console::WriteLine(u"B2 hidden: {0}", workbook->GetCell(0, u"B2")->get_IsHidden());
    Console::WriteLine(u"B3 hidden: {0}", workbook->GetCell(0, u"B3")->get_IsHidden());
    Console::WriteLine(u"C2 hidden: {0}", workbook->GetCell(0, u"C2")->get_IsHidden());

    auto workbookStream = chart->get_ChartData()->ReadWorkbookStream();
    for (auto visibleOnly : {true, false})
    {
        chart->set_PlotVisibleCellsOnly(visibleOnly);

        // 从嵌入的工作簿刷新图表数据。
        workbookStream->set_Position(0);
        chart->get_ChartData()->WriteWorkbookStream(workbookStream);
        if (!visibleOnly)
        {
            // 恢复完整的源范围,包括隐藏的类别。
            chart->get_ChartData()->SetRange(u"Sheet1!$A$1:$C$4");
        }

        auto outputPath = visibleOnly ? u"hidden_cells_True.pptx" : u"hidden_cells_False.pptx";
        presentation->Save(outputPath, Export::SaveFormat::Pptx);
    }
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

该示例保存了两个版本的演示文稿:一个仅包含可见的零售值(10 和 20),另一个包含全部六个值。下图展示了两种绘图模式。第 3 行和 C 列在两个嵌入工作簿中均保持隐藏。

仅可见单元格 (true) 所有单元格 (false)
仅可见单元格:一月和三月的零售值 10 和 20。 所有单元格:一月、二月和三月的零售和批发值。

包含值的隐藏单元格不同于空单元格。IChart::get_DisplayBlanksAs控制缺失值的显示方式;它不包含或排除隐藏的源数据。请参阅控制空单元格的显示获取示例。

检索图表的数据范围

在更新现有演示文稿中的工作簿数据之前,检查源范围以确定每个图表使用的工作表单元格。 IChartData::GetRange 方法返回当前数据范围的工作表限定公式,例如 Sheet1!$A$1:$D$5。这里,Sheet1 是工作表名称,! 将其与单元格范围分隔,$A$1:$D$5 标识包含 A1 到 D5(含)的单元格。美元符号表示绝对行列引用。

该方法读取当前范围而不更改图表或其工作簿。如果图表未使用工作簿作为数据源,则会抛出 System::InvalidOperationException。更多信息请参阅ChartData API 参考。

此示例打开一个演示文稿并检查每张幻灯片上直接的形状以查找图表。它打印每个图表的名称和源范围。如果图表未使用工作簿,它会打印相应信息并继续检查下一个图表。

#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
#include <system/exceptions.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"presentation.pptx");

for (auto slide : IterateOver(presentation->get_Slides()))
{
    for (auto shape : IterateOver(slide->get_Shapes()))
    {
        auto chart = AsCast<IChart>(shape);
        if (chart != nullptr)
        {
            try
            {
                auto range = chart->get_ChartData()->GetRange();
                Console::WriteLine(u"{0}: {1}", chart->get_Name(), range);
            }
            catch (const InvalidOperationException&)
            {
                Console::WriteLine(u"{0}: The chart does not use a workbook as its data source.", chart->get_Name());
            }
        }
    }
}

从工作簿读取和写入图表数据

Aspose.Slides for C++ 提供了 ReadWorkbookStream 和 WriteWorkbookStream 方法,允许读取和写入包含使用 Aspose.Cells 编辑的图表数据的工作簿。注意,图表数据必须以相同方式组织或具有类似的结构。

此示例使用一个在第一张幻灯片的第一个形状中的图表的演示文稿。它将嵌入工作簿读取到流中,清除现有系列和类别,并将相同的工作簿写回。更改保留在内存中,示例未保存演示文稿。

#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"chart.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto chartData = chart->get_ChartData();
    auto workbookStream = chartData->ReadWorkbookStream();

    chartData->get_Series()->Clear();
    chartData->get_Categories()->Clear();

    workbookStream->set_Position(0);
    chartData->WriteWorkbookStream(workbookStream);
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

在修改工作簿后验证图表布局

当用已修改的工作簿替换嵌入工作簿时,图表仍保留原始的系列和类别集合。这种不匹配可能导致IChart::ValidateChartLayout 因索引超出范围错误而失败。写回更新的工作簿之前,请先清除现有的系列和类别。此示例使用第一张幻灯片的第一个形状中的图表。注释标记了工作簿编辑将发生的位置;可运行示例将原始工作簿写回并在内存中验证布局。

#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"chart.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto chartData = chart->get_ChartData();
    auto workbookStream = chartData->ReadWorkbookStream();

    // 在此修改工作簿流,例如,使用 Aspose.Cells.

    chartData->get_Series()->Clear();
    chartData->get_Categories()->Clear();

    workbookStream->set_Position(0);
    chartData->WriteWorkbookStream(workbookStream);
    chart->ValidateChartLayout();
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

清除集合可在写回工作簿之前移除陈旧的数据引用。为更新的工作簿在使用图表前重新构建所需的系列和类别映射。

将工作簿单元格设为图表数据标签

可以使用工作簿单元格中的文本作为图表数据标签。

此示例向现有演示文稿的第一张幻灯片添加一个带有默认数据的气泡图。它使用工作表 0 中的单元格 A10:A12 作为第一系列的前三个标签,启用来自单元格的标签,并保存更新后的演示文稿。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"chart2.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Bubble, 50, 50, 600, 400, true);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();

series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLabelValueFromCell(true);
auto firstLabelCell = workbook->GetCell(0, u"A10", ObjectExt::Box<String>(u"Label 0 cell value"));
auto secondLabelCell = workbook->GetCell(0, u"A11", ObjectExt::Box<String>(u"Label 1 cell value"));
auto thirdLabelCell = workbook->GetCell(0, u"A12", ObjectExt::Box<String>(u"Label 2 cell value"));
series->get_Labels()->idx_get(0)->set_ValueFromCell(firstLabelCell);
series->get_Labels()->idx_get(1)->set_ValueFromCell(secondLabelCell);
series->get_Labels()->idx_get(2)->set_ValueFromCell(thirdLabelCell);

presentation->Save(u"resultchart.pptx", Export::SaveFormat::Pptx);

管理工作表

IChartDataWorkbook::get_Worksheets 方法提供对图表工作簿中工作表的访问。此示例创建一个带有默认数据的饼图,并将每个工作表名称打印到控制台。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataWorksheet.h>
#include <DOM/Chart/IChartDataWorksheetCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 500);
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();

for (auto i = 0; i < workbook->get_Worksheets()->get_Count(); i++)
{
    Console::WriteLine(workbook->get_Worksheets()->idx_get(i)->get_Name());
}

指定数据源类型

此示例创建一个带有默认数据的 3D 柱形图,并使用不同的数据源设置两个系列名称。第一个名称使用字符串文字;第二个名称使用工作表 0 中的单元格 C1。DataSourceType 枚举选择每个名称的来源。示例保存了带有更新系列名称的演示文稿。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/DataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IStringChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Column3D, 50, 50, 600, 400, true);
auto literalName = chart->get_ChartData()->get_Series()->idx_get(0)->get_Name();

literalName->set_DataSourceType(DataSourceType::StringLiterals);
literalName->set_Data(ObjectExt::Box<String>(u"LiteralString"));

auto cellName = chart->get_ChartData()->get_Series()->idx_get(1)->get_Name();
auto nameCell = chart->get_ChartData()->get_ChartDataWorkbook()->GetCell(0, u"C1", ObjectExt::Box<String>(u"NewCell"));
cellName->set_DataSourceType(DataSourceType::Worksheet);
cellName->set_Data(nameCell);

presentation->Save(u"pres.pptx", Export::SaveFormat::Pptx);

检测不受支持的嵌入工作簿格式

Aspose.Slides 不支持某些图表中可以嵌入的 Excel 二进制工作簿(.xlsb)格式。可以在IChartData 上使用 get_EmbeddedWorkbookType 方法结合 WorkbookType 枚举来检测不受支持的格式并跳过这些图表。此示例检查现有演示文稿第一张幻灯片上的形状,跳过非图表形状,并为每个带有嵌入 .xlsb 工作簿的图表打印诊断信息。

#include <DOM/Chart/ChartDataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/WorkbookType.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);

for (auto shape : IterateOver(slide->get_Shapes()))
{
    auto chart = AsCast<IChart>(shape);
    if (chart == nullptr)
    {
        continue;
    }

    auto chartData = chart->get_ChartData();
    auto isInternalWorkbook = chartData->get_DataSourceType() == ChartDataSourceType::InternalWorkbook;
    auto isBinaryMacro = chartData->get_EmbeddedWorkbookType() == WorkbookType::WorkbookBinaryMacro;

    if (isInternalWorkbook && isBinaryMacro)
    {
        Console::WriteLine(u"Skipping a chart with an unsupported .xlsb workbook.");
        continue;
    }

    // 在此读取或修改受支持的图表工作簿数据。
}

外部工作簿

Aspose.Slides 支持使用外部工作簿作为图表的数据源。

创建外部工作簿

使用ReadWorkbookStream和SetExternalWorkbook将嵌入的图表工作簿导出到文件,并将图表链接到该外部工作簿。

此示例创建一个带有默认数据的饼图并导出其工作簿。它在将外部工作簿分配为图表数据源之前关闭输出流,然后保存已链接的演示文稿。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/io/memory_stream.h>
#include <system/io/path.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600);
auto workbookPath = IO::Path::GetFullPath(u"externalWorkbook1.xlsx");
auto workbookStream = chart->get_ChartData()->ReadWorkbookStream();
auto fileStream = IO::File::Create(workbookPath);
workbookStream->CopyTo(fileStream);
fileStream->Close();

chart->get_ChartData()->SetExternalWorkbook(workbookPath);

presentation->Save(u"externalWorkbook.pptx", Export::SaveFormat::Pptx);

设置外部工作簿

使用SetExternalWorkbook 方法,可将外部工作簿分配给图表作为其数据源。该方法还可用于更新外部工作簿的路径(如果后者已移动)。

虽然无法编辑存储在远程位置或资源中的工作簿数据,但仍可将此类工作簿用作外部数据源。如果提供了外部工作簿的相对路径,则会自动转换为完整路径。

此示例使用一个外部工作簿,其工作表 Sheet1 在 B1 中包含系列名称,在 A2:A4 中包含类别名称,在 B2:B4 中包含数值。示例创建一个饼图,链接工作簿,并使用SetRange 将 A1:B4 映射为一个系列和三个类别。它保存了带有链接图表的演示文稿。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/path.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600, true);
auto chartData = chart->get_ChartData();
auto workbookPath = IO::Path::GetFullPath(u"externalWorkbook.xlsx");

chartData->SetExternalWorkbook(workbookPath);
chartData->SetRange(u"Sheet1!$A$1:$B$4");

presentation->Save(u"Presentation_with_externalWorkbook.pptx", Export::SaveFormat::Pptx);

SetExternalWorkbook的 updateChartData 参数控制是否加载工作簿。

  • 当 updateChartData 为 false 时,仅更新工作簿路径。图表数据不会从目标工作簿加载或更新,因此工作簿可以不可用。
  • 当 updateChartData 为 true 时,图表数据会从目标工作簿更新。

以下示例将占位符 URL 与 updateChartData 设置为 false 一起使用。它保留饼图的默认数据并在未加载不可用工作簿的情况下保存演示文稿。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600, true);

chart->get_ChartData()->SetExternalWorkbook(u"https://example.com/unavailable-workbook.xlsx", false);
presentation->Save(u"SetExternalWorkbookWithUpdateChartData.pptx", Export::SaveFormat::Pptx);

获取图表的外部数据源工作簿路径

要识别链接到图表的工作簿,请检查图表是否使用外部数据源并检索其工作簿路径。

此示例检查带有链接外部工作簿的演示文稿第一张幻灯片的第一个形状。如果它是链接到外部工作簿的图表,示例将get_ExternalWorkbookPath 打印到控制台。随后保存演示文稿的副本。

#include <DOM/Chart/ChartDataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"externalWorkbook.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto chartData = chart->get_ChartData();
    if (chartData->get_DataSourceType() == ChartDataSourceType::ExternalWorkbook)
    {
        Console::WriteLine(chartData->get_ExternalWorkbookPath());
    }
    else
    {
        Console::WriteLine(u"The chart does not use an external workbook.");
    }
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

presentation->Save(u"Result.pptx", Export::SaveFormat::Pptx);

编辑图表数据

可以像编辑内部工作簿的内容一样编辑外部工作簿中的数据。当外部工作簿无法加载时,将抛出异常。

此示例使用第一张幻灯片的第一个形状且链接到可访问外部工作簿的图表。它将第一系列第一个数据点的单元格支持值设为 100 并保存更新后的演示文稿。编辑单元格值可能会更新链接的外部 XLSX 文件,因此若需保留原始工作簿,请使用副本。

#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto presentation = MakeObject<Presentation>(u"presentation.pptx");
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto series = chart->get_ChartData()->get_Series();
    if (series->get_Count() > 0 && series->idx_get(0)->get_DataPoints()->get_Count() > 0)
    {
        auto valueCell = series->idx_get(0)->get_DataPoints()->idx_get(0)->get_Value()->get_AsCell();
        if (valueCell != nullptr)
        {
            valueCell->set_Value(ObjectExt::Box<int32_t>(100));
            presentation->Save(u"presentation_out.pptx", Export::SaveFormat::Pptx);
        }
        else
        {
            Console::WriteLine(u"The first data point is not linked to a workbook cell.");
        }
    }
    else
    {
        Console::WriteLine(u"The chart has no data points to edit.");
    }
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

从图表缓存中恢复工作簿

如果图表使用的外部工作簿缺失或不可用,Aspose.Slides 可从演示文稿中缓存的数据重建图表工作簿。创建LoadOptions,使用set_SpreadsheetOptions进行配置,并在打开演示文稿前调用ISpreadsheetOptions::set_RecoverWorkbookFromChartCache 并设为 true。

以下 C++ 示例恢复了第一张幻灯片上第一个形状的图表的工作簿数据,该图表引用了不可用的外部工作簿。它通过IChart::get_ChartData 和IChartData::get_ChartDataWorkbook 访问恢复的数据:

#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <DOM/SpreadsheetOptions.h>
#include <system/console.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;

auto spreadsheetOptions = MakeObject<SpreadsheetOptions>();
spreadsheetOptions->set_RecoverWorkbookFromChartCache(true);

auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_SpreadsheetOptions(spreadsheetOptions);

auto presentation = MakeObject<Presentation>(u"presentation.pptx", loadOptions);
auto slide = presentation->get_Slide(0);

auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
    auto recoveredWorkbook = chart->get_ChartData()->get_ChartDataWorkbook();

    // 在此读取或修改恢复的工作簿数据。
}
else
{
    Console::WriteLine(u"The first shape is not a chart.");
}

如果外部工作簿不可用且未启用恢复,Aspose.Slides 将抛出 System::InvalidOperationException。仅在使用缓存的图表数据是可接受的回退方案时才启用恢复,因为缓存可能不包含对外部工作簿在上次更新演示文稿后所做的更改。

FAQ

我如何判断特定图表是链接到外部工作簿还是嵌入工作簿?

可以。图表具有data source type 和外部工作簿路径;如果来源是外部工作簿,您可以读取完整路径以确认使用了外部文件。

是否支持外部工作簿的相对路径,且它们是如何存储的?

支持。如果指定相对路径,系统会自动将其转换为绝对路径。演示文稿在 PPTX 文件中存储绝对路径,因此移动工作簿可能需要更新链接。

我可以使用位于网络资源/共享上的工作簿吗?

可以,这些工作簿可以用作外部数据源。不过,Aspose.Slides 不支持直接编辑远程工作簿——它们只能作为源使用。

Aspose.Slides 在保存演示文稿时会覆盖外部 XLSX 吗?

演示文稿存储了对外部文件的链接。编辑单元格支持的图表数据也可能更新链接的本地 XLSX 文件。如需保持原始工作簿不变,请使用工作簿的副本。

如果外部文件受密码保护该怎么办?

Aspose.Slides 在链接时不接受密码。常见做法是预先移除保护或准备一个已解密的副本(例如使用Aspose.Cells),然后链接到该副本。

多个图表可以引用同一个外部工作簿吗?

可以。每个图表存储各自的链接。如果它们都指向同一文件,更新该文件将在下次加载数据时反映到每个图表中。