瀏覽代碼

Merge pull request #54 from Michalazza/Working-on-Table

Fixed insertColumns/removeColumns method along with remove. It is now able to work with merged cells.

Added a method to delete cells

Added gridSpan property to cells

Added gridAfter property to rows
master
PrzemyslawKlys 9 年之前
父節點
當前提交
615f12ca3f
共有 5 個檔案被更改,包括 551 行新增185 行删除
  1. 387
    181
      DocX/Table.cs
  2. 二進制
      DocX/bin/Debug/DocX.dll
  3. 149
    1
      UnitTests/DocXUnitTests.cs
  4. 7
    3
      UnitTests/UnitTests.csproj
  5. 8
    0
      UnitTests/packages.config

+ 387
- 181
DocX/Table.cs 查看文件

@@ -56,7 +56,7 @@ namespace Novacode
* null will be returned if no such element exists.
*/
XElement start_tcPr = null;
if(columnIndex > Rows[startRow].Cells.Count)
if (columnIndex > Rows[startRow].Cells.Count)
start_tcPr = Rows[startRow].Cells[Rows[startRow].Cells.Count - 1].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
else
start_tcPr = Rows[startRow].Cells[columnIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
@@ -206,7 +206,7 @@ namespace Novacode
if (r.Cells.Count > c)
r.Cells[c].Width = widths[c];
}
}
}
@@ -269,7 +269,7 @@ namespace Novacode
tblCellMar.AddFirst(new XElement(XName.Get(type.ToString(), DocX.w.NamespaceName)));
tblMargin = tblCellMar.Element(XName.Get(type.ToString(), DocX.w.NamespaceName));
}
tblMargin.RemoveAttributes();
// set the value for the cell margin
tblMargin.Add(new XAttribute(XName.Get("w", DocX.w.NamespaceName), margin));
@@ -284,7 +284,7 @@ namespace Novacode
public Double GetColumnWidth(Int32 index)
{
List<Double> widths = ColumnWidths;
if (widths == null || index > widths.Count -1) return Double.NaN;
if (widths == null || index > widths.Count - 1) return Double.NaN;
return widths[index];
}
@@ -492,14 +492,14 @@ namespace Novacode
}
}
/// <summary>
/// String containing the Table Caption value (the table's Alternate Text Title)
/// </summary>
private string _tableCaption;
/// <summary>
/// Gets or Sets the value of the Table Caption (Alternate Text Title) of this table.
/// </summary>
public string TableCaption
/// <summary>
/// String containing the Table Caption value (the table's Alternate Text Title)
/// </summary>
private string _tableCaption;
/// <summary>
/// Gets or Sets the value of the Table Caption (Alternate Text Title) of this table.
/// </summary>
public string TableCaption
{
set
{
@@ -514,7 +514,7 @@ namespace Novacode
tblCaption = new XElement(XName.Get("tblCaption", DocX.w.NamespaceName),
new XAttribute(XName.Get("val", DocX.w.NamespaceName), value));
tblPr.Add(tblCaption);
tblPr.Add(tblCaption);
}
}
@@ -685,7 +685,7 @@ namespace Novacode
tableAttributeValue = columnAttributeValue = "dxa";
XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
XElement tblLayout = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName));
XElement tblLayout = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName));
if (tblLayout == null)
{
@@ -709,7 +709,9 @@ namespace Novacode
break;
} else {
}
else
{
var qry = from d in Xml.Descendants()
let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
where (d.Name.LocalName == "tblLayout") && type != null
@@ -1305,7 +1307,7 @@ namespace Novacode
/// </example>
public void InsertColumn()
{
InsertColumn(ColumnCount);
InsertColumn(ColumnCount, true);
}
/// <summary>
@@ -1416,7 +1418,41 @@ namespace Novacode
throw new IndexOutOfRangeException();
foreach (Row r in Rows)
r.Cells[index].Xml.Remove();
if (r.Cells.Count < ColumnCount)
{
var positionIndex = 0;
var actualPosition = 0;
var gridAfterVal = 0;
// checks to see if there is a deleted cell
gridAfterVal = r.gridAfter;
// goes through iteration of cells to find the one the that contains the index number
foreach (Cell rowCell in r.Cells)
{
// checks if the cell has a gridspan
var gridSpanVal = 0;
if (rowCell.GridSpan != 0)
{
gridSpanVal = rowCell.GridSpan - 1;
}
// checks to see if the index is within its lowest and highest cell value
if ((index - gridAfterVal) >= actualPosition
&& (index - gridAfterVal) <= (actualPosition + gridSpanVal))
{
r.Cells[positionIndex].Xml.Remove();
break;
}
positionIndex += 1;
actualPosition += gridSpanVal + 1;
}
}
else
{
r.Cells[index].Xml.Remove();
}
_cachedColCount = -1;
}
@@ -1508,6 +1544,7 @@ namespace Novacode
/// Insert a column into a table.
/// </summary>
/// <param name="index">The index to insert the column at.</param>
/// <param name="direction">The side in which you wish to place the colum(True right, false left)</param>
/// <example>
/// Insert a column to the left of a table.
/// <code>
@@ -1518,7 +1555,7 @@ namespace Novacode
/// Table table = document.Tables[0];
///
/// // Insert a new column to this left of this table.
/// table.InsertColumn(0);
/// table.InsertColumn(0, false);
///
/// // Set the new columns text to "Row no."
/// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
@@ -1543,23 +1580,149 @@ namespace Novacode
/// }// Release this document from memory.
/// </code>
/// </example>
public void InsertColumn(int index)
public void InsertColumn(int index, bool direction)
{
var columnCount = ColumnCount;
if (RowCount > 0)
{
_cachedColCount = -1;
foreach (Row r in Rows)
if (index > 0 && index <= columnCount)
{
// create cell
XElement cell = HelperFunctions.CreateTableCell();
_cachedColCount = -1;
foreach (Row r in Rows)
{
// create cell
XElement cell = HelperFunctions.CreateTableCell();
// insert cell
if (r.Cells.Count == index)
r.Cells[index - 1].Xml.AddAfterSelf(cell);
// insert cell
// checks if it is in bounds of index
if (r.Cells.Count < columnCount)
{
if (index >= columnCount)
{
AddCellToRow(r, cell, r.Cells.Count, direction);
}
else
{
bool directionTest = true;
var positionIndex = 1;
var actualPosition = 1;
var gridAfterVal = 0;
// checks to see if there is a deleted cell
gridAfterVal = r.gridAfter;
// goes through iteration of cells to find the one the that contains the index number
foreach (Cell rowCell in r.Cells)
{
// checks if the cell has a gridspan
var gridSpanVal = 0;
if (rowCell.GridSpan != 0)
{
gridSpanVal = rowCell.GridSpan - 1;
}
// checks to see if the index is within its lowest and highest cell value
if ((index - gridAfterVal) >= actualPosition
&& (index - gridAfterVal) <= (actualPosition + gridSpanVal))
{
if (index == (actualPosition + gridSpanVal) && direction == true)
{
directionTest = true;
}
else
{
directionTest = false;
}
AddCellToRow(r, cell, positionIndex, directionTest);
break;
}
positionIndex += 1;
actualPosition += gridSpanVal + 1;
}
}
}
else if (r.Cells.Count == index)
{
AddCellToRow(r, cell, index, direction);
}
else
AddCellToRow(r, cell, index, direction);
}
}
else
{
throw new IndexOutOfRangeException("Out of index bounds, column count is " + columnCount + " you input " + index);
}
}
}
/// <summary>
/// Adds a cell to the right or left of a cell
/// </summary>
/// <param name="row">is the row you are adding</param>
/// <param name="cell">is the cell you are adding</param>
/// <param name="index">the cell index position you are refferencing from</param>
/// <param name="direction">which side of the cell you wish to add cell</param>
private void AddCellToRow(Row row, XElement cell, int index, bool direction)
{
index -= 1;
if (direction)
{
row.Cells[index].Xml.AddAfterSelf(cell);
}
else
{
row.Cells[index].Xml.AddBeforeSelf(cell);
}
}
/// <summary>
/// Deletes a cell in a row
/// </summary>
/// <param name="rowIndex">index of the row you want to remove the cell</param>
/// <param name="celIndex">index of the cell you want to remove</param>
public void DeleteAndShiftCellsLeft(int rowIndex, int celIndex)
{
XAttribute gridAfterVal = new XAttribute(XName.Get("val", DocX.w.NamespaceName), 0);
var trPr = Rows[rowIndex].Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null)
{
var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName));
if (gridAfter != null)
{
var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
{
val.Value = (int.Parse(val.Value) + 1).ToString();
}
else
r.Cells[index].Xml.AddBeforeSelf(cell);
{
val.Value = "1";
}
}
else
{
var gridAfterElement = new XElement("gridAfter");
var gridAfterValAttribute = new XAttribute("val", 1);
gridAfter.SetAttributeValue("val", 1);
}
}
else
{
XElement trPrXElement = new XElement(XName.Get("trPr", DocX.w.NamespaceName));
XElement gridAfterElement = new XElement(XName.Get("gridAfter", DocX.w.NamespaceName));
XAttribute gridAfterValAttribute = new XAttribute(XName.Get("val", DocX.w.NamespaceName), 1);
gridAfterElement.Add(gridAfterValAttribute);
trPrXElement.Add(gridAfterElement);
Rows[rowIndex].Xml.AddFirst(trPrXElement);
}
var columnCount = this.ColumnCount;
if (celIndex <= this.ColumnCount && this.Rows[rowIndex].ColumnCount <= this.ColumnCount)
{
Rows[rowIndex].Cells[celIndex].Xml.Remove();
}
}
/// <summary>
@@ -2028,79 +2191,79 @@ namespace Novacode
/// <param name="borderType">The table border to set</param>
/// <param name="border">Border object to set the table border</param>
public void SetBorder(TableBorderType borderType, Border border)
{
/*
{
/*
* Get the tblPr (table properties) element for this Table,
* null will be return if no such element exists.
*/
XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
if (tblPr == null)
{
Xml.SetElementValue(XName.Get("tblPr", DocX.w.NamespaceName), string.Empty);
tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
}
/*
XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
if (tblPr == null)
{
Xml.SetElementValue(XName.Get("tblPr", DocX.w.NamespaceName), string.Empty);
tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
}
/*
* Get the tblBorders (table borders) element for this Table,
* null will be return if no such element exists.
*/
XElement tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
if (tblBorders == null)
{
tblPr.SetElementValue(XName.Get("tblBorders", DocX.w.NamespaceName), string.Empty);
tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
}
/*
XElement tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
if (tblBorders == null)
{
tblPr.SetElementValue(XName.Get("tblBorders", DocX.w.NamespaceName), string.Empty);
tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
}
/*
* Get the 'borderType' (table border) element for this Table,
* null will be return if no such element exists.
*/
string tbordertype;
tbordertype = borderType.ToString();
// only lower the first char of string (because of insideH and insideV)
tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
XElement tblBorderType = tblBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
if (tblBorderType == null)
{
tblBorders.SetElementValue(XName.Get(tbordertype, DocX.w.NamespaceName), string.Empty);
tblBorderType = tblBorders.Element(XName.Get(tbordertype, DocX.w.NamespaceName));
}
// get string value of border style
string borderstyle = border.Tcbs.ToString().Substring(5);
borderstyle = borderstyle.Substring(0, 1).ToLower() + borderstyle.Substring(1);
// The val attribute is used for the border style
tblBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);
if (border.Tcbs != BorderStyle.Tcbs_nil)
{
int size;
switch (border.Size)
{
case BorderSize.one: size = 2; break;
case BorderSize.two: size = 4; break;
case BorderSize.three: size = 6; break;
case BorderSize.four: size = 8; break;
case BorderSize.five: size = 12; break;
case BorderSize.six: size = 18; break;
case BorderSize.seven: size = 24; break;
case BorderSize.eight: size = 36; break;
case BorderSize.nine: size = 48; break;
default: size = 2; break;
}
// The sz attribute is used for the border size
tblBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());
// The space attribute is used for the cell spacing (probably '0')
tblBorderType.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), (border.Space).ToString());
// The color attribute is used for the border color
string tbordertype;
tbordertype = borderType.ToString();
// only lower the first char of string (because of insideH and insideV)
tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
XElement tblBorderType = tblBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
if (tblBorderType == null)
{
tblBorders.SetElementValue(XName.Get(tbordertype, DocX.w.NamespaceName), string.Empty);
tblBorderType = tblBorders.Element(XName.Get(tbordertype, DocX.w.NamespaceName));
}
// get string value of border style
string borderstyle = border.Tcbs.ToString().Substring(5);
borderstyle = borderstyle.Substring(0, 1).ToLower() + borderstyle.Substring(1);
// The val attribute is used for the border style
tblBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);
if (border.Tcbs != BorderStyle.Tcbs_nil)
{
int size;
switch (border.Size)
{
case BorderSize.one: size = 2; break;
case BorderSize.two: size = 4; break;
case BorderSize.three: size = 6; break;
case BorderSize.four: size = 8; break;
case BorderSize.five: size = 12; break;
case BorderSize.six: size = 18; break;
case BorderSize.seven: size = 24; break;
case BorderSize.eight: size = 36; break;
case BorderSize.nine: size = 48; break;
default: size = 2; break;
}
// The sz attribute is used for the border size
tblBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());
// The space attribute is used for the cell spacing (probably '0')
tblBorderType.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), (border.Space).ToString());
// The color attribute is used for the border color
tblBorderType.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), border.Color.ToHex());
}
}
}
}
/// <summary>
/// Get a table border
@@ -2242,7 +2405,7 @@ namespace Novacode
}
return b;
}
}
/// <summary>
@@ -2259,22 +2422,14 @@ namespace Novacode
{
int gridSpanSum = 0;
gridSpanSum += gridAfter;
// Foreach each Cell between startIndex and endIndex inclusive.
foreach (Cell c in Cells)
{
XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr != null)
if (c.GridSpan != 0)
{
XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
int value = 0;
if (val != null)
if (int.TryParse(val.Value, out value))
gridSpanSum += value - 1;
}
gridSpanSum += c.GridSpan - 1;
}
}
@@ -2283,6 +2438,31 @@ namespace Novacode
}
}
/// <summary>
/// Returns the GridAfter of a row ie. The amount of cells that are deleted
/// </summary>
public int gridAfter
{
get
{
var gridAfterValue = 0;
var trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null)
{
var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName));
if (gridAfter != null)
{
var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
{
gridAfterValue += int.Parse(val.Value);
}
}
}
return gridAfterValue;
}
}
/// <summary>
/// A list of Cells in this Row.
/// </summary>
@@ -2458,41 +2638,41 @@ namespace Novacode
/// Set to true to make this row the table header row that will be repeated on each page
/// </summary>
public bool TableHeader
{
get
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null)
{
return false;
}
else
{
return true;
}
}
set
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
{
Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
}
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null && value)
{
trPr.SetElementValue(XName.Get("tblHeader", DocX.w.NamespaceName), string.Empty);
}
if (tblHeader != null && !value)
{
tblHeader.Remove();
}
}
}
{
get
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null)
{
return false;
}
else
{
return true;
}
}
set
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
{
Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
}
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null && value)
{
trPr.SetElementValue(XName.Get("tblHeader", DocX.w.NamespaceName), string.Empty);
}
if (tblHeader != null && !value)
{
tblHeader.Remove();
}
}
}
/// <summary>
/// Allow row to break across pages.
/// The default value is true: Word will break the contents of the row across pages.
@@ -2500,48 +2680,48 @@ namespace Novacode
/// </summary>
public bool BreakAcrossPages
{
get
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
return true;
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit == null)
return true;
return false;
}
set
{
if (value == false)
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
{
Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
}
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit == null)
trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty);
}
if (value == true)
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null)
{
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit != null)
trCantSplit.Remove();
}
}
}
get
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
return true;
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit == null)
return true;
return false;
}
set
{
if (value == false)
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
{
Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
}
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit == null)
trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty);
}
if (value == true)
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null)
{
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit != null)
trCantSplit.Remove();
}
}
}
}
/// <summary>
@@ -2622,6 +2802,7 @@ namespace Novacode
public class Cell : Container
{
internal Row row;
internal Cell(Row row, DocX document, XElement xml)
: base(document, xml)
{
@@ -2641,6 +2822,31 @@ namespace Novacode
return paragraphs;
}
}
/// <summary>
/// Returns the GridSpan of a specific Cell ie. How many cells are merged
/// </summary>
public int GridSpan
{
get
{
var gridSpanVal = 0;
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr != null)
{
XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
int value = 0;
if (val != null)
if (int.TryParse(val.Value, out value))
gridSpanVal = value;
}
}
return gridSpanVal;
}
}
/// <summary>
/// Gets or Sets this Cells vertical alignment.
@@ -3819,5 +4025,5 @@ namespace Novacode
public bool NoHorizontalBanding { get; set; }
public bool NoVerticalBanding { get; set; }
}
}

二進制
DocX/bin/Debug/DocX.dll 查看文件


+ 149
- 1
UnitTests/DocXUnitTests.cs 查看文件

@@ -135,6 +135,7 @@ namespace UnitTests
}
[Test]
public void TestPatternFuncReplacement()
{
@@ -1399,6 +1400,153 @@ namespace UnitTests
}
}
[Test]
public void Test_Table_RemoveColumnWithMergedCells()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(2, 3);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.Rows[0].MergeCells(1, 2);
t1.RemoveColumn();
document.Save();
}
}
[Test]
public void Test_Table_MergedRowCellsMergedWithColumnMergedCells()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(3, 3);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.Rows[0].MergeCells(0, 1);
t1.Rows[1].MergeCells(0, 1);
t1.MergeCellsInColumn(0, 0, 1);
document.Save();
}
}
[Test]
public void Test_Table_RemoveRowWithMergedCells()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(3, 4);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.Rows[0].MergeCells(1, 2);
t1.RemoveRow();
t1.MergeCellsInColumn(0, 0, 1);
t1.InsertRow();
t1.RemoveRow(1);
document.Save();
}
}
[Test]
public void Test_Table_InsertRow()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(2, 3);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.MergeCellsInColumn(1, 0, 1);
t1.InsertRow();
t1.Rows[2].MergeCells(1, 3);
t1.InsertRow(3);
document.Save();
}
}
[Test]
public void Test_Table_AddColumnWithCellDeleted()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(2, 3);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.DeleteAndShiftCellsLeft(0, 1);
// 4 columns
t1.InsertColumn();
t1.DeleteAndShiftCellsLeft(0, 1);
// 5 columns
t1.InsertColumn();
t1.DeleteAndShiftCellsLeft(0, 1);
document.Save();
}
}
[Test]
public void Test_Table_DeleteCellInRow()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(2, 2);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.DeleteAndShiftCellsLeft(0, 1);
document.Save();
document.Save();
}
}
[Test]
public void Test_Table_InsertColumnWithMergedCells()
{
using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
{
//Add A table
Table t = document.AddTable(2, 2);
t.Design = TableDesign.TableGrid;
Table t1 = document.InsertTable(t);
t1.InsertColumn(2, true);
t1.InsertColumn(2, true);
t1.InsertColumn(2, true);
t1.InsertColumn(2, true);
t1.Rows[0].MergeCells(1, 4);
Assert.AreEqual(t1.Rows[1].Cells.Count, 6);
Assert.AreEqual(t1.ColumnCount, 6);
foreach (Row r in t1.Rows)
{
foreach (Cell c in r.Cells)
{
c.Paragraphs[0].InsertText("Hello");
}
}
t1.InsertColumn(6, false);
t1.InsertColumn();
t1.InsertColumn(3, true);
t1.InsertColumn(6, true);
t1.InsertColumn(6, true);
t1.InsertColumn(5, true);
document.Save();
}
}
[Test]
public void Test_Table_InsertRowAndColumn()
{
@@ -1418,7 +1566,7 @@ namespace UnitTests
Table t1 = document.InsertTable(t);
// ... and add a column and a row
t1.InsertRow(1);
t1.InsertColumn(1);
t1.InsertColumn(1, true);
// Save the document.
document.Save();

+ 7
- 3
UnitTests/UnitTests.csproj 查看文件

@@ -18,6 +18,8 @@
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -48,8 +50,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\DocX\bin\Debug\DocX.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net40\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\nunit.framework.2.63.0\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
@@ -139,7 +141,9 @@
<None Include="documents\VariousTextFormatting.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="documents\green.jpg">

+ 8
- 0
UnitTests/packages.config 查看文件

@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.4.1" targetFramework="net40" />
<package id="NUnit.Console" version="3.4.1" targetFramework="net40" />
<package id="NUnit.ConsoleRunner" version="3.4.1" targetFramework="net40" />
<package id="NUnit.Extension.NUnitProjectLoader" version="3.4.1" targetFramework="net40" />
<package id="NUnit.Extension.NUnitV2Driver" version="3.4.1" targetFramework="net40" />
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.4.1" targetFramework="net40" />
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.1" targetFramework="net40" />
<package id="NUnit.Extension.VSProjectLoader" version="3.4.1" targetFramework="net40" />
<package id="nunit.framework" version="2.63.0" targetFramework="net40" />
</packages>

Loading…
取消
儲存