-
Notifications
You must be signed in to change notification settings - Fork 70
Fix nullable references #168
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
Merged
twsouthwick
merged 10 commits into
OfficeDev:main
from
mikeebowen:fix-nullable-references
Nov 6, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2b284b6
fix presentation samples
mikeebowen 5944149
cs files updated for nullable enable
tomjebo dc486c2
fix nullable issues in spreadsheet
mikeebowen 99d7f9e
Merge pull request #1 from tomjebo/fix-nullable-references
mikeebowen a4e2d58
Merge branch 'main' into fix-nullable-references
mikeebowen ec2b10f
make word examples executable
mikeebowen 06a0984
make recommended changes
mikeebowen cac8345
replace NullableRefernceExceptions with ArgumentNullExceptions
mikeebowen c9a56a3
use .First() to prevent nullable value
mikeebowen dd2e343
fix nullable issue
mikeebowen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
526 changes: 258 additions & 268 deletions
526
samples/presentation/create_by_providing_a_file_name/cs/Program.cs
Large diffs are not rendered by default.
Oops, something went wrong.
56 changes: 34 additions & 22 deletions
56
samples/presentation/retrieve_the_number_of_slides/cs/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,49 @@ | ||
| #nullable disable | ||
|
|
||
| using DocumentFormat.OpenXml.Packaging; | ||
| using System; | ||
| using System.IO.Enumeration; | ||
| using System.Linq; | ||
|
|
||
| static int RetrieveNumberOfSlides(string fileName, | ||
| bool includeHidden = true) | ||
| if (args is [ { } fileName, {} includeHidden]) | ||
| { | ||
| RetrieveNumberOfSlides(fileName, includeHidden); | ||
| } | ||
| else if (args is [{ } fileName2]) | ||
| { | ||
| RetrieveNumberOfSlides(fileName2); | ||
| } | ||
|
|
||
| static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true") | ||
| { | ||
| int slidesCount = 0; | ||
|
|
||
| using (PresentationDocument doc = | ||
| PresentationDocument.Open(fileName, false)) | ||
| using (PresentationDocument doc = PresentationDocument.Open(fileName, false)) | ||
| { | ||
| // Get the presentation part of the document. | ||
| PresentationPart presentationPart = doc.PresentationPart; | ||
| if (presentationPart != null) | ||
| if (doc is not null && doc.PresentationPart is not null) | ||
| { | ||
| if (includeHidden) | ||
| // Get the presentation part of the document. | ||
| PresentationPart presentationPart = doc.PresentationPart; | ||
| if (presentationPart is not null) | ||
| { | ||
| slidesCount = presentationPart.SlideParts.Count(); | ||
| } | ||
| else | ||
| { | ||
| // Each slide can include a Show property, which if hidden | ||
| // will contain the value "0". The Show property may not | ||
| // exist, and most likely will not, for non-hidden slides. | ||
| var slides = presentationPart.SlideParts.Where( | ||
| (s) => (s.Slide != null) && | ||
| ((s.Slide.Show == null) || (s.Slide.Show.HasValue && | ||
| s.Slide.Show.Value))); | ||
| slidesCount = slides.Count(); | ||
| if (includeHidden.ToLower() == "true") | ||
| { | ||
| slidesCount = presentationPart.SlideParts.Count(); | ||
| } | ||
| else | ||
| { | ||
| // Each slide can include a Show property, which if hidden | ||
| // will contain the value "0". The Show property may not | ||
| // exist, and most likely will not, for non-hidden slides. | ||
| var slides = presentationPart.SlideParts.Where( | ||
| (s) => (s.Slide is not null) && | ||
| ((s.Slide.Show is null) || (s.Slide.Show.HasValue && s.Slide.Show.Value))); | ||
|
|
||
| slidesCount = slides.Count(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Console.WriteLine($"Slide Count: {slidesCount}"); | ||
|
|
||
| return slidesCount; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
samples/spreadsheet/open_for_read_only_access/cs/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| #nullable disable | ||
| using DocumentFormat.OpenXml.Packaging; | ||
|
|
||
| static void OpenSpreadsheetDocumentReadonly(string filepath) | ||
| { | ||
| // Open a SpreadsheetDocument based on a filepath. | ||
| using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filepath, false)) | ||
| { | ||
| // Attempt to add a new WorksheetPart. | ||
| // The call to AddNewPart generates an exception because the file is read-only. | ||
| WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>(); | ||
| if (spreadsheetDocument.WorkbookPart is not null) | ||
| { | ||
| // Attempt to add a new WorksheetPart. | ||
| // The call to AddNewPart generates an exception because the file is read-only. | ||
| WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>(); | ||
|
|
||
| // The rest of the code will not be called. | ||
| // The rest of the code will not be called. | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,53 @@ | ||
| #nullable disable | ||
|
|
||
| using DocumentFormat.OpenXml.Packaging; | ||
| using DocumentFormat.OpenXml.Spreadsheet; | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| FileStream fileStream = new(args[0], FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); | ||
| OpenAndAddToSpreadsheetStream(fileStream); | ||
|
|
||
| static void OpenAndAddToSpreadsheetStream(Stream stream) | ||
| { | ||
| // Open a SpreadsheetDocument based on a stream. | ||
| SpreadsheetDocument spreadsheetDocument = | ||
| SpreadsheetDocument.Open(stream, true); | ||
| SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true); | ||
|
|
||
| // Add a new worksheet. | ||
| WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>(); | ||
| newWorksheetPart.Worksheet = new Worksheet(new SheetData()); | ||
| newWorksheetPart.Worksheet.Save(); | ||
| if (spreadsheetDocument is not null) | ||
| { | ||
| // Get or create the WorkbookPart | ||
| WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart ?? spreadsheetDocument.AddWorkbookPart(); | ||
|
|
||
| Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>(); | ||
| string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart); | ||
| // Add a new worksheet. | ||
| WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>(); | ||
| newWorksheetPart.Worksheet = new Worksheet(new SheetData()); | ||
| newWorksheetPart.Worksheet.Save(); | ||
|
|
||
| // Get a unique ID for the new worksheet. | ||
| uint sheetId = 1; | ||
| if (sheets.Elements<Sheet>().Count() > 0) | ||
| { | ||
| sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1; | ||
| } | ||
| Workbook workbook = workbookPart.Workbook ?? new Workbook(); | ||
|
|
||
| // Give the new worksheet a name. | ||
| string sheetName = "Sheet" + sheetId; | ||
| if (workbookPart.Workbook is null) | ||
| { | ||
| workbookPart.Workbook = workbook; | ||
| } | ||
|
|
||
| // Append the new worksheet and associate it with the workbook. | ||
| Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; | ||
| sheets.Append(sheet); | ||
| spreadsheetDocument.WorkbookPart.Workbook.Save(); | ||
| Sheets sheets = workbook.GetFirstChild<Sheets>() ?? workbook.AppendChild(new Sheets()); | ||
| string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart); | ||
|
|
||
| // Dispose the document handle. | ||
| spreadsheetDocument.Dispose(); | ||
| // Get a unique ID for the new worksheet. | ||
| uint sheetId = 1; | ||
|
|
||
| if (sheets.Elements<Sheet>().Count() > 0) | ||
| { | ||
| sheetId = (sheets.Elements<Sheet>().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements<Sheet>().Count() + 1; | ||
| } | ||
|
|
||
| // Give the new worksheet a name. | ||
| string sheetName = "Sheet" + sheetId; | ||
|
|
||
| // Append the new worksheet and associate it with the workbook. | ||
| Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; | ||
| sheets.Append(sheet); | ||
| workbookPart.Workbook.Save(); | ||
|
|
||
| // Dispose the document handle. | ||
| spreadsheetDocument.Dispose(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.