TemplNET.TemplDoc.CellClear C# (CSharp) Méthode

CellClear() public static méthode

Clear text and images from all paragraphs in cell. Optional: Delete all paragraph objects. Keep in mind, cells with zero paragraphs are considered malformed by Word! Also keep in mind, you will lose formatting info (e.g. font). Special cases of content other than text or images may not be removed; If this becomes a problem, we can develop it.
public static CellClear ( Cell cell, bool deleteAllParagraphs = false ) : void
cell Novacode.Cell
deleteAllParagraphs bool
Résultat void
        public static void CellClear(Cell cell, bool deleteAllParagraphs = false)
        {
            // Remove all but first paragraph
            cell.Paragraphs.Skip(deleteAllParagraphs ? 0 : 1).ToList().ForEach(p => cell.RemoveParagraph(p));
            if (!deleteAllParagraphs)
            {
                var p = cell.Paragraphs.Last();
                if (p.Text.Length > 0)
                {
                    p.RemoveText(0, p.Text.Length);
                }
            }
            cell.Pictures.ForEach(pic => pic.Remove());
        }

Usage Example

Exemple #1
0
        public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m)
        {
            var width = m.Table.Rows.First().Cells.Count;
            var keys  = TemplModelEntry.Get(model, m.Body).ToStringKeys();
            var nrows = (int)Math.Ceiling(keys.Count() / (float)width);

            m.Validate();
            m.RemovePlaceholder();
            for (int n = 0; n < width; n++)
            {
                if (n != m.CellIndex)
                {
                    TemplDoc.CellCopyContents(m.Cell, m.Row.Cells[n]);
                }
            }
            Row row = m.Row;

            for (int keyIdx = 0; keyIdx < nrows * width; keyIdx++)
            {
                if (keyIdx % width == 0)
                {
                    row = m.Table.InsertRow(m.Row, m.RowIndex + keyIdx / width + 1);
                }
                Cell cell = row.Cells[keyIdx % width];
                if (keyIdx < keys.Count())
                {
                    new TemplCollectionModule().BuildFromScope(doc, model, cell.Paragraphs, $"{m.Body}[{keys[keyIdx]}]");
                }
                else
                {
                    TemplDoc.CellClear(cell);
                }
            }
            m.Row.Remove();
            m.Removed = true;
            return(m);
        }