public void Remove() { ChangeableDictionary <int> d = Setup(); d.RemoveAndShift(5); Assert.AreEqual(20, d[2]); Assert.AreEqual(40, d[4]); Assert.AreEqual(60, d[5]); Assert.AreEqual(70, d[6]); }
/// <summary> /// Deletes a worksheet from the collection /// </summary> /// <param name="Index">The position of the worksheet in the workbook</param> public void Delete(int Index) { /* * Hack to prefetch all the drawings, * so that all the images are referenced, * to prevent the deletion of the image file, * when referenced more than once */ foreach (var ws in _worksheets) { var drawings = ws.Drawings; } ExcelWorksheet worksheet = _worksheets[Index - _pck._worksheetAdd]; if (worksheet.Drawings.Count > 0) { worksheet.Drawings.ClearDrawings(); } //Remove all comments if (!(worksheet is ExcelChartsheet) && worksheet.Comments.Count > 0) { worksheet.Comments.Clear(); } while (worksheet.PivotTables.Count > 0) { worksheet.PivotTables.Delete(worksheet.PivotTables[0]); } //Delete any parts still with relations to the Worksheet. DeleteRelationsAndParts(worksheet.Part); //Delete the worksheet part and relation from the package _pck.Workbook.Part.DeleteRelationship(worksheet.RelationshipId); //Delete worksheet from the workbook XML XmlNode sheetsNode = _pck.Workbook.WorkbookXml.SelectSingleNode("//d:workbook/d:sheets", _namespaceManager); if (sheetsNode != null) { XmlNode sheetNode = sheetsNode.SelectSingleNode(string.Format("./d:sheet[@sheetId={0}]", worksheet.SheetId), _namespaceManager); if (sheetNode != null) { sheetsNode.RemoveChild(sheetNode); } } if (_pck.Workbook.VbaProject != null) { _pck.Workbook.VbaProject.Modules.Remove(worksheet.CodeModule); } _worksheets.RemoveAndShift(Index - _pck._worksheetAdd); ReindexWorksheetDictionary(); //If the active sheet is deleted, set the first tab as active. if (_pck.Workbook.View.ActiveTab >= _pck.Workbook.Worksheets.Count) { _pck.Workbook.View.ActiveTab = _pck.Workbook.View.ActiveTab - 1; } if (_pck.Workbook.View.ActiveTab == worksheet.SheetId) { _pck.Workbook.Worksheets[_pck._worksheetAdd].View.TabSelected = true; } }