Table of Contents

Scenarios and embedded objects

XlsxDocumentSheet.Scenarios is a worksheet's Scenario Manager: named sets of values for its changing cells, listed, added, removed and applied. XlsxDocumentSheet.EmbeddedObjects is its OLE objects: files and documents embedded in the sheet, listed with their data, a file embedded as an icon, an object removed.

List and apply scenarios

using DocWright.Spreadsheet;

XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("scenarios.xlsx"));
XlsxDocumentSheet model = workbook.FindSheet("Model")!;
foreach (XlsxScenario scenario in model.Scenarios)
{
    Console.WriteLine($"{scenario.Name}: {string.Join(", ", scenario.ChangingCells)} = {string.Join("; ", scenario.Values)}");
}

model.Scenarios.Find("Best")!.Apply();
workbook.Calculate();
Console.WriteLine($"Best: B1 {model.GetCell("B1").DisplayText}, B3 {model.GetCell("B3").DisplayText}; shown: {model.Scenarios.Shown!.Name}");
model.Scenarios.Find("Kinds")!.Apply();
Console.WriteLine($"Kinds: C1 {model.GetCell("C1").DisplayText}, C2 {model.GetCell("C2").DisplayText}, C3 {model.GetCell("C3").DisplayText}");

Apply writes the values into the cells as Excel's Show writes them, each entered as Excel types an entry: 007 becomes 7, 12.5% becomes 0.125 shown 0.00%, 1e3 becomes 1000 shown 0.00E+00, TRUE a logical value. A formula in a changing cell is replaced; formulas that depend on the cells are recalculated by Calculate.

Add and remove a scenario

using DocWright.Spreadsheet;

model.Scenarios.Add("Cautious", "B1:B2", new[] { "4", "80" }, new XlsxScenarioOptions { Comment = "half the growth" });
model.Scenarios.Find("Worst")!.Remove();
using (FileStream output = File.Create("scenarios-edited.xlsx"))
{
    workbook.Save(output);
}

Excel's refusals are kept: a name already on the sheet (in any case), more than 32 changing cells, a name, comment or value longer than 255 characters, and a formula as a value. XlsxScenarioOptions sets the comment, the maker (User; nothing is written unless you give one), and whether the scenario is locked or hidden while the sheet is protected.

Read an embedded object

using DocWright.Ole;
using DocWright.Spreadsheet;

XlsxDocument objects = XlsxDocument.Open(File.OpenRead("embedded.xlsx"));
XlsxDocumentSheet sheet = objects.FindSheet("Objects")!;
foreach (XlsxEmbeddedObject embedded in sheet.EmbeddedObjects)
{
    using OlePayload payload = OlePayload.Open(embedded.OpenData());
    payload.TryOpenNativeContent(out Stream? content);
    using var reader = new StreamReader(content!);
    Console.WriteLine($"{embedded.Name} ({embedded.ProgId}) at {embedded.Anchor!.From}: {payload.Kind}, \"{reader.ReadLine()}\"");
}

OpenData gives the embedded bytes, read-only; nothing is opened or run. A file packaged by Excel's Insert, Object, Create from file is an OLE container: OlePayload from DocWright.Ole reads its streams, and TryOpenNativeContent gives the file. A linked object has no data in the workbook.

Embed a file

using DocWright.Spreadsheet;

XlsxEmbeddedObject added = sheet.EmbeddedObjects.Add(File.OpenRead("notes.txt"), "notes.txt", File.OpenRead("object-icon.png"), "F3");
Console.WriteLine($"added {added}");
sheet.EmbeddedObjects.Find("Notes")!.Remove();
using (FileStream output = File.Create("embedded-edited.xlsx"))
{
    objects.Save(output);
}
Best: B1, B2 = 6.25; 140
Worst: B1, B2 = 3; 60
Hidden one: B1, B2 = 5; 90
Label: B4 = shown text
Kinds: C1, C2, C3, C4, C5 = TRUE; 007; 12.5%; text; 1e3
Apart: D1, D3 = 1; 3
Formula cell: B3 = 1
Best: B1 6.25, B3 875; shown: Best
Kinds: C1 TRUE, C2 7, C3 12.50%
Notes (Packager Shell Object) at C3 +47625 +0: PackagedFile, "Embedded notes"
added Object 2 (Packager Shell Object)

The file is wrapped as Windows' Packager object and shown as the PNG or JPEG icon you give (DocWright draws none of its own), in the places Excel keeps one: the sheet's object list, the embedding, the drawing and the legacy drawing. Remove takes an object out of all of them, with its data and its picture when nothing else uses them.

Download the results: scenarios-edited.xlsx, embedded-edited.xlsx.