Better to have a Cell.SetText method

Oct 12, 2009 at 10:03 PM

I'm frequently needs to fill data into cells, and it's better to have a Cell.SetText method like this:

 

public void SetText(string text) {
  paragraphs.ForEach(paragraph => paragraph.Remove(false));
  paragraphs[0].Append(text);
}

So that we can write something like

for (int i = 0, j = table.Rows.Count; i < j; i++) {
  Row row = table.Rows[i];

  for (int k = 0, l = row.Cells.Count; k < l; k++) {
    row.Cells[k].SetText(getCellData(i, k));  // getCellData is a method which fetch data from data source
  }
}

Right now, I'm using an extension method to do this. I don't want to hack the source, and sync back after update :)~

 

Coordinator
Oct 12, 2009 at 10:49 PM
Hi Alsan,

You are correct. This would be a more natural and expected behavior for DocX to work with Tables.

I am glad that you written some extension methods for this functionality. I am very busy at the moment as I have just started the final year of my "Computer Science & Software Engineering" degree. Unfortunately this means that the free time I have to work on DocX is limited. I would be very grateful if you would post your extension methods here on the discussions section. I am sure that others would find them very useful. When I eventually find time to release a new version of DocX, I will include your functionality.

kind regards and happy coding,
Cathal

P.S. LINQ rocks.
Oct 12, 2009 at 10:58 PM

Ok, here is my version of the extension:

public static class DocXExtension {
  public static void SetText(this Cell cell, string text) {
    cell.Paragraphs.ForEach(paragraph => paragraph.Remove(false));
    cell.Paragraphs[0].Append(text);
  }
}

Hope this help someone.

Coordinator
Oct 12, 2009 at 11:12 PM
Thank you alsan.