Skip to content

Commit

Permalink
Merge pull request #171 from startewho/public-FindAndReplace
Browse files Browse the repository at this point in the history
pullic FindAndReplace
  • Loading branch information
PrzemyslawKlys authored Nov 26, 2023
2 parents 2cf68ef + 33a63db commit 6c8abe1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
48 changes: 47 additions & 1 deletion OfficeIMO.Tests/Word.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,53 @@ public void Test_SetTableStyleId() {
document.Save(false);

}

}


[Fact]
public void Test_CreatingWordDocumentWithTablesWithReplace() {
string filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithTablesReplace.docx");
using (WordDocument document = WordDocument.Create(filePath)) {

document.AddParagraph("Table1 - Test 1");
document.AddParagraph("Test1");


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";

document.Save(false);
}

using (WordDocument document = WordDocument.Load(filePath)) {

Assert.True(document.Tables.Count == 1);

var wordTable = document.Tables[0];

Assert.True(wordTable.Rows[0].Cells[0].Paragraphs[0].Text == "Test 1");
Assert.True(wordTable.Rows[1].Cells[0].Paragraphs[0].Text == "Test 2");
Assert.True(wordTable.Rows[2].Cells[0].Paragraphs[0].Text == "Test 3");

WordDocument.FindAndReplace(wordTable.Rows[0].Cells[0].Paragraphs, "Test 1", "Test 11");
WordDocument.FindAndReplace(wordTable.Rows[1].Cells[0].Paragraphs, "Test 2", "Test 21");

// lets make sure find and replace works only on table
Assert.True(document.Paragraphs[0].Text == "Table1 - Test 1");
Assert.True(document.Paragraphs[1].Text == "Test1");

// lets check data for table
Assert.True(wordTable.Rows[0].Cells[0].Paragraphs[0].Text == "Test 11");
Assert.True(wordTable.Rows[1].Cells[0].Paragraphs[0].Text == "Test 21");
Assert.True(wordTable.Rows[2].Cells[0].Paragraphs[0].Text == "Test 3");

document.Save();
}

}

}
}
23 changes: 22 additions & 1 deletion OfficeIMO.Word/WordDocument.PublicMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,35 @@ public List<WordParagraph> Find(string text, StringComparison stringComparison =
return list;
}

/// <summary>
/// FindAdnReplace from the whole doc
/// </summary>
/// <param name="textToFind"></param>
/// <param name="textToReplace"></param>
/// <param name="stringComparison"></param>
/// <returns></returns>
public int FindAndReplace(string textToFind, string textToReplace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
int countFind = 0;
FindAndReplaceInternal(textToFind, textToReplace, ref countFind, true, stringComparison);
return countFind;
}

/// <summary>
/// FindAdnReplace from the range parparagraphs
/// </summary>
/// <param name="paragraphs"></param>
/// <param name="textToFind"></param>
/// <param name="textToReplace"></param>
/// <param name="stringComparison"></param>
/// <returns></returns>
public static int FindAndReplace(List<WordParagraph> paragraphs, string textToFind, string textToReplace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
int countFind = 0;
FindAndReplaceNested(paragraphs, textToFind, textToReplace, ref countFind, true, stringComparison);
return countFind;
}


private List<WordParagraph> FindAndReplaceNested(List<WordParagraph> paragraphs, string textToFind, string textToReplace, ref int count, bool replace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
private static List<WordParagraph> FindAndReplaceNested(List<WordParagraph> paragraphs, string textToFind, string textToReplace, ref int count, bool replace, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) {
List<WordParagraph> foundParagraphs = ReplaceText(paragraphs, textToFind, textToReplace, ref count, replace, stringComparison);
return foundParagraphs;
}
Expand Down

0 comments on commit 6c8abe1

Please sign in to comment.