在簡報中使用 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) |
|---|---|
![]() |
![]() |
包含數值的隱藏儲存格不同於空白儲存格。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時,圖表資料會從目標工作簿更新。
以下範例以 false 設定 updateChartData,指派佔位 URL。它保留圓餅圖的預設資料,並在未載入不可用工作簿的情況下儲存簡報。
#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 進行設定,並在開啟簡報前以 true 呼叫ISpreadsheetOptions::set_RecoverWorkbookFromChartCache。
以下 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。僅在使用快取的圖表資料作為可接受的備援時才啟用復原,因為快取可能不包含簡報最後更新後對外部工作簿所做的變更。
常見問題
我可以判斷特定圖表是連結至外部還是內嵌工作簿嗎?
可以。圖表具備資料來源類型與外部工作簿路徑;若來源為外部工作簿,您即可讀取完整路徑,以確認使用的是外部檔案。
是否支援外部工作簿的相對路徑?它們如何被儲存?
是的。若您指定相對路徑,系統會自動將其轉換為絕對路徑。簡報會在 PPTX 檔案中儲存絕對路徑,若移動工作簿可能需要更新連結。
我能使用位於網路資源/共享中的工作簿嗎?
可以,此類工作簿可作為外部資料來源使用。但 Aspose.Slides 並不支援直接編輯遠端工作簿——它們只能作為來源使用。
Aspose.Slides 在儲存簡報時會覆寫外部 XLSX 嗎?
簡報會儲存外部檔案的連結。編輯以儲存格為基礎的圖表資料亦可能更新連結的本機 XLSX 檔。若必須保留原始工作簿不變,請使用其副本。
如果外部檔案受密碼保護,我該怎麼辦?
Aspose.Slides 在連結時不接受密碼。常見的做法是事先移除保護或建立已解密的副本(例如使用Aspose.Cells),並連結至該副本。
多個圖表可以參考同一個外部工作簿嗎?
可以。每個圖表都會儲存自己的連結。若它們皆指向同一檔案,更新該檔案後,下一次載入資料時,每個圖表皆會反映出變更。

