Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove OpenXmlPackage.Close and references #1372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Removed obsolete validation logic from v1 of the SDK
- Removed obsoleted methods from 2.x
- Removed mutable properties on OpenXmlAttribute and marked as `readonly`
- Removed OpenXmlPackage.Close in favor of Dispose method (#1298)

### Fixed
- Fixed incorrect file extensions for vbaProject files (#1292)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,6 @@ public static class EMMA
/// </remarks>
public static readonly XName model_ref = emma + "model-ref";

/// <summary>
/// Represents the emma:no-input XML attribute.
/// </summary>
/// <remarks>
/// <para>As an XML attribute, it:</para>
/// <list type="bullet">
/// <item><description>is contained in the following XML elements: <see cref="interpretation" />.</description></item>
/// <item><description>corresponds to the following strongly-typed properties: Interpretation.NoInput.</description></item>
/// </list>
/// </remarks>
public static readonly XName no_input = emma + "no-input";

/// <summary>
/// Represents the emma:node XML element.
/// </summary>
Expand All @@ -432,6 +420,18 @@ public static class EMMA
/// </remarks>
public static readonly XName node = emma + "node";

/// <summary>
/// Represents the emma:no-input XML attribute.
/// </summary>
/// <remarks>
/// <para>As an XML attribute, it:</para>
/// <list type="bullet">
/// <item><description>is contained in the following XML elements: <see cref="interpretation" />.</description></item>
/// <item><description>corresponds to the following strongly-typed properties: Interpretation.NoInput.</description></item>
/// </list>
/// </remarks>
public static readonly XName no_input = emma + "no-input";

/// <summary>
/// Represents the emma:offset-to-start XML attributes.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22847,18 +22847,6 @@ public static class NoNamespace
/// </remarks>
public static readonly XName z = "z";

/// <summary>
/// Represents the z-order XML attribute.
/// </summary>
/// <remarks>
/// <para>As an XML attribute, it:</para>
/// <list type="bullet">
/// <item><description>is contained in the following XML elements: <see cref="X.anchor" />.</description></item>
/// <item><description>corresponds to the following strongly-typed properties: ObjectAnchor.ZOrder.</description></item>
/// </list>
/// </remarks>
public static readonly XName z_order = "z-order";

/// <summary>
/// Represents the zeroHeight XML attribute.
/// </summary>
Expand Down Expand Up @@ -22967,6 +22955,18 @@ public static class NoNamespace
/// </remarks>
public static readonly XName zoomToFit = "zoomToFit";

/// <summary>
/// Represents the z-order XML attribute.
/// </summary>
/// <remarks>
/// <para>As an XML attribute, it:</para>
/// <list type="bullet">
/// <item><description>is contained in the following XML elements: <see cref="X.anchor" />.</description></item>
/// <item><description>corresponds to the following strongly-typed properties: ObjectAnchor.ZOrder.</description></item>
/// </list>
/// </remarks>
public static readonly XName z_order = "z-order";

/// <summary>
/// Represents the zOrderOff XML attribute.
/// </summary>
Expand Down
109 changes: 54 additions & 55 deletions samples/PowerPointModernCommentSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,64 +38,63 @@ public static void Main(string[] args)
}

// Create a presentation
PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);

// create missing PowerPointAuthorsPart if it is null
if (presentationDocument?.PresentationPart?.authorsPart is null)
{
presentationDocument?.PresentationPart.AddNewPart<PowerPointAuthorsPart>();
}

// Add missing AuthorList if it is null
if (presentationDocument?.PresentationPart?.authorsPart?.AuthorList is null)
using (PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName))
{
presentationDocument.PresentationPart.authorsPart.AuthorList = new AuthorList();
}

// In production you will need to retrieve actual user information
string authorId = string.Concat("{", Guid.NewGuid(), "}");
presentationDocument.PresentationPart.authorsPart.AuthorList.AddChild(new Author()
{ Id = authorId, Name = "Jose Contoso", Initials = "JC", UserId = "jose@contosot.com::9ed1aa81-8d9b-4a15-b989-8cbbfd4c4b3e", ProviderId = "AD" });

// Get the Id of the slide to add the comment to
SlideId slideId = presentationDocument.PresentationPart.Presentation.SlideIdList.Elements<SlideId>().First();

Random ran = new Random();
UInt32Value cid = Convert.ToUInt32(ran.Next(100000000, 999999999));

// Add a comment to the slide
PowerPointCommentPart powerPointCommentPart = presentationDocument.PresentationPart.SlideParts.First().AddNewPart<PowerPointCommentPart>();

powerPointCommentPart.CommentList = new CommentList(
new Comment(
new SlideMonikerList(
new DocumentMoniker(),
new SlideMoniker()
{
CId = cid,
SldId = slideId.Id,
}),
new TextBodyType(
new BodyProperties(),
new ListStyle(),
new Paragraph(new Run(new Text("Hello World!")))))
// create missing PowerPointAuthorsPart if it is null
if (presentationDocument?.PresentationPart?.authorsPart is null)
{
Id = string.Concat("{", Guid.NewGuid(), "}"),
AuthorId = authorId,
Created = DateTime.Now,
});

SlidePart slidePart = presentationDocument.PresentationPart.SlideParts.First();
Slide slide = slidePart.Slide;
presentationDocument?.PresentationPart.AddNewPart<PowerPointAuthorsPart>();
}

slide.AppendChild(
new PresentationExtensionList(
new PresentationExtension(
new CommentRelationship()
{ Id = slidePart.GetIdOfPart(powerPointCommentPart) })
{ Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" }));

presentationDocument.Close();
// Add missing AuthorList if it is null
if (presentationDocument?.PresentationPart?.authorsPart?.AuthorList is null)
{
presentationDocument.PresentationPart.authorsPart.AuthorList = new AuthorList();
}

// In production you will need to retrieve actual user information
string authorId = string.Concat("{", Guid.NewGuid(), "}");
presentationDocument.PresentationPart.authorsPart.AuthorList.AddChild(new Author()
{ Id = authorId, Name = "Jose Contoso", Initials = "JC", UserId = "jose@contosot.com::9ed1aa81-8d9b-4a15-b989-8cbbfd4c4b3e", ProviderId = "AD" });

// Get the Id of the slide to add the comment to
SlideId slideId = presentationDocument.PresentationPart.Presentation.SlideIdList.Elements<SlideId>().First();

Random ran = new Random();
UInt32Value cid = Convert.ToUInt32(ran.Next(100000000, 999999999));

// Add a comment to the slide
PowerPointCommentPart powerPointCommentPart = presentationDocument.PresentationPart.SlideParts.First().AddNewPart<PowerPointCommentPart>();

powerPointCommentPart.CommentList = new CommentList(
new Comment(
new SlideMonikerList(
new DocumentMoniker(),
new SlideMoniker()
{
CId = cid,
SldId = slideId.Id,
}),
new TextBodyType(
new BodyProperties(),
new ListStyle(),
new Paragraph(new Run(new Text("Hello World!")))))
{
Id = string.Concat("{", Guid.NewGuid(), "}"),
AuthorId = authorId,
Created = DateTime.Now,
});

SlidePart slidePart = presentationDocument.PresentationPart.SlideParts.First();
Slide slide = slidePart.Slide;

slide.AppendChild(
new PresentationExtensionList(
new PresentationExtension(
new CommentRelationship()
{ Id = slidePart.GetIdOfPart(powerPointCommentPart) })
{ Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" }));
}
}
catch (Exception ex)
{
Expand Down
13 changes: 7 additions & 6 deletions samples/SunburstChartExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ private static void Main(string[] args)
public static void CreatePresentation(string filepath)
{
// Create a presentation at a specified file path. The presentation document type is pptx, by default.
var presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
var presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();
using (PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation))
{
var presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();

CreatePresentationParts(presentationPart);
CreatePresentationParts(presentationPart);

// Close the presentation handle
presentationDoc.Close();
// Close the presentation handle
}
}

private static void CreatePresentationParts(PresentationPart presentationPart)
Expand Down
90 changes: 44 additions & 46 deletions samples/ThreadedCommentExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,67 +51,65 @@ private static void Main(string[] args)
}

// WORKBOOK
SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName);

if (sd != null)
using (SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use using SpreadsheetDocument? sd = ....; instead without indenting the files

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

{
ExampleUtilities.InsertText((SpreadsheetDocument)sd, sheetName, @"Please comment on this cell.", column, row);
}
if (sd != null)
{
ExampleUtilities.InsertText((SpreadsheetDocument)sd, sheetName, @"Please comment on this cell.", column, row);
}

if (!(sd is null))
{
CreateMiscellaneousParts(sd, sheetName);
if (!(sd is null))
{
CreateMiscellaneousParts(sd, sheetName);

Worksheet? worksheet = ExampleUtilities.GetSheet(sd, sheetName)?.Worksheet;
Worksheet? worksheet = ExampleUtilities.GetSheet(sd, sheetName)?.Worksheet;

if (worksheet != null)
{
worksheet.AddChild(new LegacyDrawing() { Id = "rId1" });
if (worksheet != null)
{
worksheet.AddChild(new LegacyDrawing() { Id = "rId1" });

// NOTE: UserId, DisplayName, Id and ProviderId should be generated by querying the identity server used by this org and account.
string displayNameUser = "Jose Contoso";
string idUser = string.Concat("{", System.Guid.NewGuid().ToString().ToUpper(), "}");
string tcId = string.Concat("{", System.Guid.NewGuid().ToString().ToUpper(), "}");
// NOTE: UserId, DisplayName, Id and ProviderId should be generated by querying the identity server used by this org and account.
string displayNameUser = "Jose Contoso";
string idUser = string.Concat("{", System.Guid.NewGuid().ToString().ToUpper(), "}");
string tcId = string.Concat("{", System.Guid.NewGuid().ToString().ToUpper(), "}");

// For the format of these userId parameter, please refer to [MS-XLSX] 2.6.203 CT_Person
// This can be a SID from an Active Directory provider, an email address using "PeoplePicker", and O365 id or a simple name.
string userIdJose = "j.contoso@example.com";
string providerIdAzure = "PeoplePicker";
// For the format of these userId parameter, please refer to [MS-XLSX] 2.6.203 CT_Person
// This can be a SID from an Active Directory provider, an email address using "PeoplePicker", and O365 id or a simple name.
string userIdJose = "j.contoso@example.com";
string providerIdAzure = "PeoplePicker";

WorkbookPart workbookPart = sd.WorkbookPart ?? sd.AddWorkbookPart();
WorkbookPart workbookPart = sd.WorkbookPart ?? sd.AddWorkbookPart();

WorkbookPersonPart pp = workbookPart.AddNewPart<WorkbookPersonPart>();
WorkbookPersonPart pp = workbookPart.AddNewPart<WorkbookPersonPart>();

pp.PersonList = new PersonList(new Person() { DisplayName = displayNameUser, Id = idUser, UserId = userIdJose, ProviderId = providerIdAzure });
pp.PersonList = new PersonList(new Person() { DisplayName = displayNameUser, Id = idUser, UserId = userIdJose, ProviderId = providerIdAzure });

WorksheetPart wsp = ExampleUtilities.GetSheet(sd, sheetName);
WorksheetPart wsp = ExampleUtilities.GetSheet(sd, sheetName);

WorksheetCommentsPart wscp = wsp.AddNewPart<WorksheetCommentsPart>();
wscp.Comments = new Comments(
new Authors(
new Author("tc=" + tcId)), new CommentList(
new Comment(
new DocumentFormat.OpenXml.Spreadsheet.CommentText(
new DocumentFormat.OpenXml.Spreadsheet.Text("Comment: Ok, here's a comment!")))
WorksheetCommentsPart wscp = wsp.AddNewPart<WorksheetCommentsPart>();
wscp.Comments = new Comments(
new Authors(
new Author("tc=" + tcId)), new CommentList(
new Comment(
new DocumentFormat.OpenXml.Spreadsheet.CommentText(
new DocumentFormat.OpenXml.Spreadsheet.Text("Comment: Ok, here's a comment!")))

// Comment attributes
{ Reference = reference, AuthorId = 0, ShapeId = 0, Guid = tcId }));
// Comment attributes
{ Reference = reference, AuthorId = 0, ShapeId = 0, Guid = tcId }));

WorksheetThreadedCommentsPart wstcp = wsp.AddNewPart<WorksheetThreadedCommentsPart>();
wstcp.ThreadedComments = new ThreadedComments(
new ThreadedComment(
new ThreadedCommentText("Ok, here's a threaded comment!"))
WorksheetThreadedCommentsPart wstcp = wsp.AddNewPart<WorksheetThreadedCommentsPart>();
wstcp.ThreadedComments = new ThreadedComments(
new ThreadedComment(
new ThreadedCommentText("Ok, here's a threaded comment!"))

// ThreadedComment attributes
{ Ref = reference, PersonId = idUser, Id = tcId, DT = System.DateTime.Now });
// ThreadedComment attributes
{ Ref = reference, PersonId = idUser, Id = tcId, DT = System.DateTime.Now });
}
}
else
{
throw new Exception("SpreadsheetDocument is null");
}

// Close the document.
sd.Close();
}
else
{
throw new Exception("SpreadsheetDocument is null");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ public void DeletePartsRecursivelyOfType<T>()
DeletePartsRecursivelyOfTypeBase<T>();
}

/// <summary>
/// Saves and closes the OpenXml package and all underlying part streams.
/// </summary>
public void Close()
{
ThrowIfObjectDisposed();
Dispose();
}

#region methods to operate DataPart

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate()
var part = packageDocument.PresentationPart;
var root = part.Presentation;

packageDocument.SaveAs(Path.GetTempFileName()).Close();
packageDocument.SaveAs(Path.GetTempFileName()).Dispose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor these usages to be within a using block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, of course


// We are fine if we have not run into an exception.
Assert.True(true);
Expand All @@ -36,7 +36,7 @@ public void CanCreateSpreadsheetFromTemplate()
var part = packageDocument.WorkbookPart;
var root = part.Workbook;

packageDocument.SaveAs(Path.GetTempFileName()).Close();
packageDocument.SaveAs(Path.GetTempFileName()).Dispose();

// We are fine if we have not run into an exception.
Assert.True(true);
Expand All @@ -52,7 +52,7 @@ public void CanCreateWordprocessingDocumentFromTemplate()
var part = packageDocument.MainDocumentPart;
var root = part.Document;

packageDocument.SaveAs(Path.GetTempFileName()).Close();
packageDocument.SaveAs(Path.GetTempFileName()).Dispose();

// We are fine if we have not run into an exception.
Assert.True(true);
Expand Down
Loading