Skip to content

Commit

Permalink
Merge pull request #182 from tmheath/master
Browse files Browse the repository at this point in the history
Added the ability to clear the default empty paragraph in TableCell.
  • Loading branch information
PrzemyslawKlys committed Feb 2, 2024
2 parents 2f39457 + 7738d7a commit 8c50e97
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
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 partial class Word {

}

[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 class WordTableCell {
/// <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 class WordTableCell {
/// 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 class WordTableCell {
}

/// <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 class WordTableCell {
}

/// <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

0 comments on commit 8c50e97

Please sign in to comment.