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

Added the ability to clear the default empty paragraph in TableCell. #182

Merged
merged 5 commits into from
Feb 2, 2024
Merged
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
59 changes: 59 additions & 0 deletions OfficeIMO.Tests/Word.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,5 +859,64 @@ public void Test_CreatingWordDocumentWithTablesWithReplace() {

}

[Fact]
public void Test_CreatingWordDocumentWithTablesClearParagraphs() {
string filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithTables.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
Assert.True(document.Paragraphs.Count == 0, "Number of paragraphs during creation is wrong. Current: " + document.Paragraphs.Count);
Assert.True(document.Tables.Count == 0, "Tables count matches");
Assert.True(document.Lists.Count == 0, "List count matches");

var paragraph = document.AddParagraph("Basic paragraph - Page 4");
paragraph.ParagraphAlignment = JustificationValues.Center;

WordTable wordTable = document.AddTable(3, 4);
wordTable.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1";
wordTable.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2";
wordTable.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3";

wordTable.Rows[0].Cells[1].Paragraphs[0].Text = "Test Row 0 Cell 1";

Assert.True(document.Tables.Count == 1);
Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 1);
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 1);

// add 2 more texts to the same cell as new paragraphs
wordTable.Rows[0].Cells[0].Paragraphs[0].AddParagraph("New");
wordTable.Rows[0].Cells[0].Paragraphs[1].AddParagraph("New more");

Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 3);

Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].Text == "Test 1");
Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[1].Text == "New");
Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[2].Text == "New more");

// replace existing paragraphs with single one
wordTable.Rows[0].Cells[0].AddParagraph("New paragraph, delete rest", true);

Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs.Count == 1);
Assert.True(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].Text == "New paragraph, delete rest");

// lets try to add new paragraph to the same cell, using WordParagraph
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 1);
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[0].Text == "Test Row 0 Cell 1");

WordParagraph paragraph1 = new WordParagraph {
Text = "Paragraph added separately as WordParagraph",
Bold = true,
Italic = true
};

wordTable.Rows[0].Cells[1].AddParagraph(paragraph1);

Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs.Count == 2);
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[0].Text == "Test Row 0 Cell 1");
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Text == "Paragraph added separately as WordParagraph");
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Bold == true);
Assert.True(document.Tables[0].Rows[0].Cells[1].Paragraphs[1].Italic == true);

document.Save(false);
}
}
}
}
30 changes: 23 additions & 7 deletions OfficeIMO.Word/WordTableCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,24 @@ public string ShadingFillColorHex {
/// <summary>
/// Add paragraph to the table cell
/// </summary>
/// <param name="paragraph"></param>
/// <returns></returns>
public WordParagraph AddParagraph(WordParagraph paragraph = null) {
/// <param name="paragraph">The paragraph to add to this cell, if
/// this is not passed then a new empty paragraph with settings from
/// the previous paragraph will be added.</param>
/// <param name="removeExistingParagraphs">If value is not passed or false then add
/// the given paragraph into the cell. If set to true then clear
/// every existing paragraph before adding the new paragraph.
/// </param>
/// <returns>A reference to the added paragraph.</returns>
public WordParagraph AddParagraph(WordParagraph paragraph = null, bool removeExistingParagraphs = false) {
// Considering between implementing a reset that clears all paragraphs or
// a deletePrevious that will replace the last paragraph.
// NOTE: Raise this during PR.
if (removeExistingParagraphs) {
var paragraphs = _tableCell.ChildElements.OfType<Paragraph>().ToList();
foreach (var wordParagraph in paragraphs) {
wordParagraph.Remove();
}
}
if (paragraph == null) {
paragraph = new WordParagraph(this._document);
}
Expand All @@ -113,9 +128,10 @@ public WordParagraph AddParagraph(WordParagraph paragraph = null) {
/// Add paragraph to the table cell with text
/// </summary>
/// <param name="text"></param>
/// <param name="removeExistingParagraphs"></param>
/// <returns></returns>
public WordParagraph AddParagraph(string text) {
return AddParagraph().SetText(text);
public WordParagraph AddParagraph(string text, bool removeExistingParagraphs = false) {
return AddParagraph(paragraph: null, removeExistingParagraphs).SetText(text);
}

/// <summary>
Expand Down Expand Up @@ -339,7 +355,7 @@ public void MergeHorizontally(int cellsCount, bool copyParagraphs = false) {
}

/// <summary>
/// Splits (unmerge) cells that were merged
/// Splits (unmerge) cells that were merged
/// </summary>
/// <param name="cellsCount"></param>
public void SplitHorizontally(int cellsCount) {
Expand All @@ -359,7 +375,7 @@ public void SplitHorizontally(int cellsCount) {
}

/// <summary>
/// Merges two or more cells together vertically
/// Merges two or more cells together vertically
/// </summary>
/// <param name="cellsCount"></param>
/// <param name="copyParagraphs"></param>
Expand Down