| /// </code> | /// </code> | ||||
| /// </example> | /// </example> | ||||
| public void RemoveColumn(int index) | public void RemoveColumn(int index) | ||||
| { | |||||
| { | |||||
| if (index < 0 || index > ColumnCount - 1) | if (index < 0 || index > ColumnCount - 1) | ||||
| throw new IndexOutOfRangeException(); | throw new IndexOutOfRangeException(); | ||||
| foreach (Row r in Rows) | 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 | |||||
| XElement trPr = r.Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | |||||
| if (trPr != null) | |||||
| { | |||||
| XElement gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName)); | |||||
| if (gridAfter != null) | |||||
| { | |||||
| XAttribute val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (val != null) | |||||
| { | |||||
| gridAfterVal = int.Parse(val.Value); | |||||
| } | |||||
| } | |||||
| } | |||||
| // 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; | |||||
| XElement tcPr = rowCell.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 - 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; | _cachedColCount = -1; | ||||
| } | } | ||||
| // insert cell | // insert cell | ||||
| // checks if it is in bounds of index | // checks if it is in bounds of index | ||||
| // TODO: Check for gridspan of cells in row to check if merged cells | |||||
| if (r.Cells.Count < columnCount) | if (r.Cells.Count < columnCount) | ||||
| { | { | ||||
| if (index >= columnCount) | if (index >= columnCount) | ||||
| var positionIndex = 1; | var positionIndex = 1; | ||||
| var actualPosition = 1; | var actualPosition = 1; | ||||
| var gridAfterVal = 0; | var gridAfterVal = 0; | ||||
| // checks to see if here is a deleted cell | |||||
| // checks to see if there is a deleted cell | |||||
| XElement trPr = r.Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | XElement trPr = r.Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| if (trPr != null) | if (trPr != null) | ||||
| { | { | ||||
| // goes through iteration of cells to find the one the that contains the index number | // goes through iteration of cells to find the one the that contains the index number | ||||
| foreach (Cell rowCell in r.Cells) | foreach (Cell rowCell in r.Cells) | ||||
| { | { | ||||
| // checks if the cell has a gridspan | |||||
| var gridSpanVal = 0; | var gridSpanVal = 0; | ||||
| XElement tcPr = rowCell.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = rowCell.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr != null) | if (tcPr != null) | ||||
| gridSpanVal = value -1; | gridSpanVal = value -1; | ||||
| } | } | ||||
| } | } | ||||
| // Sees if the cell has gridSpan and if the index is within its lowest and highest cell value | |||||
| // checks to see if the index is within its lowest and highest cell value | |||||
| if ((index - gridAfterVal) >= actualPosition | if ((index - gridAfterVal) >= actualPosition | ||||
| && (index - gridAfterVal) <= (actualPosition + gridSpanVal)) | && (index - gridAfterVal) <= (actualPosition + gridSpanVal)) | ||||
| { | { | ||||
| var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | ||||
| if (val != null) | if (val != null) | ||||
| { | { | ||||
| val.Value = int.Parse(val.Value + 1).ToString(); | |||||
| val.Value = (int.Parse(val.Value) + 1).ToString(); | |||||
| } | } | ||||
| else | else | ||||
| { | { |
| <w:tr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | |||||
| <w:trPr> | |||||
| <w:gridAfter w:val="1" /> | |||||
| </w:trPr> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2310" w:type="dxa" /> | |||||
| </w:tcPr> | |||||
| <w:p> | |||||
| <w:pPr /> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| </w:tr> |
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |||||
| <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"> | |||||
| <w:body> | |||||
| <w:tbl> | |||||
| <w:tblPr> | |||||
| <w:tblStyle w:val="TableGrid"/> | |||||
| <w:tblW w:w="0" w:type="auto"/> | |||||
| <w:tblLook w:val="04A0"/> | |||||
| </w:tblPr> | |||||
| <w:tblGrid> | |||||
| <w:gridCol w:w="2319"/> | |||||
| <w:gridCol w:w="2319"/> | |||||
| <w:gridCol w:w="2315"/> | |||||
| <w:gridCol w:w="2289"/> | |||||
| </w:tblGrid> | |||||
| <w:tr w:rsidR="00D73752" w:rsidTr="00D73752"> | |||||
| <w:trPr> | |||||
| <w:gridAfter w:val="1"/> | |||||
| <w:wAfter w:w="2289" w:type="dxa"/> | |||||
| <w:trHeight w:val="1408"/> | |||||
| </w:trPr> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="6953" w:type="dxa"/> | |||||
| <w:gridSpan w:val="3"/> | |||||
| <w:vMerge w:val="restart"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"> | |||||
| <w:r> | |||||
| <w:t>a</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"> | |||||
| <w:r> | |||||
| <w:t>c</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"> | |||||
| <w:r> | |||||
| <w:t>g</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"> | |||||
| <w:r> | |||||
| <w:t>d</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"> | |||||
| <w:r> | |||||
| <w:t>d</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752" w:rsidP="00E942FF"> | |||||
| <w:r> | |||||
| <w:t>F</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| </w:tr> | |||||
| <w:tr w:rsidR="00D73752" w:rsidTr="00D73752"> | |||||
| <w:trPr> | |||||
| <w:trHeight w:val="273"/> | |||||
| </w:trPr> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="6953" w:type="dxa"/> | |||||
| <w:gridSpan w:val="3"/> | |||||
| <w:vMerge/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"/> | |||||
| </w:tc> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2289" w:type="dxa"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="00D73752" w:rsidRDefault="00D73752"/> | |||||
| </w:tc> | |||||
| </w:tr> | |||||
| <w:tr w:rsidR="0072257A" w:rsidTr="0072257A"> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2319" w:type="dxa"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="0072257A" w:rsidRDefault="0072257A"> | |||||
| <w:r> | |||||
| <w:t>g</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2319" w:type="dxa"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="0072257A" w:rsidRDefault="0072257A"> | |||||
| <w:r> | |||||
| <w:t>h</w:t> | |||||
| </w:r> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2315" w:type="dxa"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="0072257A" w:rsidRDefault="0072257A"> | |||||
| <w:proofErr w:type="spellStart"/> | |||||
| <w:r> | |||||
| <w:t>i</w:t> | |||||
| </w:r> | |||||
| <w:proofErr w:type="spellEnd"/> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2289" w:type="dxa"/> | |||||
| </w:tcPr> | |||||
| <w:p w:rsidR="0072257A" w:rsidRDefault="0072257A"/> | |||||
| </w:tc> | |||||
| </w:tr> | |||||
| </w:tbl> | |||||
| <w:p w:rsidR="00FB5292" w:rsidRDefault="00FB5292"/> | |||||
| <w:sectPr w:rsidR="00FB5292" w:rsidSect="00FB5292"> | |||||
| <w:pgSz w:w="11906" w:h="16838"/> | |||||
| <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/> | |||||
| <w:cols w:space="708"/> | |||||
| <w:docGrid w:linePitch="360"/> | |||||
| </w:sectPr> | |||||
| </w:body> | |||||
| </w:document> |
| <w:tr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | |||||
| <w:tc> | |||||
| <w:tcPr> | |||||
| <w:tcW w:w="2310" w:type="dxa" /> | |||||
| </w:tcPr> | |||||
| <w:p> | |||||
| <w:pPr /> | |||||
| </w:p> | |||||
| </w:tc> | |||||
| </w:tr> |
| } | } | ||||
| } | } | ||||
| [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_MergeRowMergedCellsWithColumnMergedCells() | |||||
| { | |||||
| 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.SaveAs(@"C:\\Meh\\ThisTest.docx"); | |||||
| } | |||||
| } | |||||
| [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] | [Test] | ||||
| public void Test_Table_InsertRow() | public void Test_Table_InsertRow() | ||||
| { | { | ||||
| t1.InsertRow(); | t1.InsertRow(); | ||||
| t1.Rows[2].MergeCells(1, 3); | t1.Rows[2].MergeCells(1, 3); | ||||
| t1.InsertRow(3); | t1.InsertRow(3); | ||||
| document.SaveAs(@"C:\\Meh\\ThisTest.docx"); | |||||
| document.Save(); | |||||
| } | } | ||||
| } | } | ||||
| Table t1 = document.InsertTable(t); | Table t1 = document.InsertTable(t); | ||||
| t1.DeleteAndShiftCellsLeft(0, 1); | t1.DeleteAndShiftCellsLeft(0, 1); | ||||
| // 4 columns | |||||
| t1.InsertColumn(); | t1.InsertColumn(); | ||||
| t1.DeleteAndShiftCellsLeft(0, 1); | t1.DeleteAndShiftCellsLeft(0, 1); | ||||
| // 5 columns | |||||
| t1.InsertColumn(); | t1.InsertColumn(); | ||||
| document.Save(); | |||||
| document.SaveAs(@"C:\\Meh\\ThisTest.docx"); | |||||
| t1.DeleteAndShiftCellsLeft(0, 1); | |||||
| document.Save(); | |||||
| } | } | ||||
| } | } | ||||
| t1.DeleteAndShiftCellsLeft(0, 1); | t1.DeleteAndShiftCellsLeft(0, 1); | ||||
| document.Save(); | document.Save(); | ||||
| document.SaveAs(@"C:\\Meh\\ThisTest.docx"); | |||||
| document.Save(); | |||||
| } | } | ||||
| } | } | ||||
| c.Paragraphs[0].InsertText("Hello"); | c.Paragraphs[0].InsertText("Hello"); | ||||
| } | } | ||||
| } | } | ||||
| t1.InsertColumn(6, false); | |||||
| Assert.AreEqual(t1.ColumnCount, 7); | |||||
| Assert.IsTrue(String.IsNullOrEmpty(t1.Rows[0].Cells[2].Paragraphs[0].Text)); | |||||
| //Assert.IsTrue(String.IsNullOrEmpty(t1.Rows[1].Cells[6].Paragraphs[0].Text)); | |||||
| t1.InsertColumn(6, false); | |||||
| t1.InsertColumn(); | t1.InsertColumn(); | ||||
| t1.InsertColumn(3, true); | t1.InsertColumn(3, true); | ||||
| t1.InsertColumn(6, true); | t1.InsertColumn(6, true); | ||||
| t1.InsertColumn(6, true); | t1.InsertColumn(6, true); | ||||
| t1.InsertColumn(5, true); | t1.InsertColumn(5, true); | ||||
| //Assert.AreEqual(t1.ColumnCount, 8); | |||||
| document.SaveAs(@"C:\\Meh\\ThisTest.docx"); | |||||
| document.Save(); | |||||
| } | } | ||||
| } | } | ||||
| document.Save(); | document.Save(); | ||||
| } | } | ||||
| using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx"))) | |||||
| { | |||||
| //Add A table | |||||
| Table t = document.AddTable(2, 1); | |||||
| t.Design = TableDesign.TableGrid; | |||||
| Table t1 = document.InsertTable(t); | |||||
| foreach (Row r in t1.Rows) | |||||
| { | |||||
| foreach (Cell c in r.Cells) | |||||
| { | |||||
| c.Paragraphs[0].InsertText("Hello"); | |||||
| } | |||||
| } | |||||
| } | |||||
| // Check table | // Check table | ||||
| using (DocX document = DocX.Load(Path.Combine(_directoryDocuments, "Tables2.docx"))) | using (DocX document = DocX.Load(Path.Combine(_directoryDocuments, "Tables2.docx"))) | ||||
| { | { |