public void MoveSheetsDown() { Workbook wb = Instance.Default.CreateWorkbook(6); WorkbookViewModel wvm = new WorkbookViewModel(wb); // Select the second-to-last sheet in the collection SheetViewModel svm = wvm.Sheets[wvm.Sheets.Count - 2]; string sheetName = svm.DisplayString; // With no sheets selected, the move-down command should // be disabled. wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; Assert.IsFalse(wvm.MoveSheetDown.CanExecute(null), "Move-down command is enabled, should be disabled with no sheets selected."); svm.IsSelected = true; Assert.IsTrue(wvm.MoveSheetDown.CanExecute(null), "Move-down command is disabled, should be enabled with one sheet selected."); wvm.MoveSheetDown.Execute(null); // The selected sheet should now be the last sheet, // which cannot be moved 'down' as it is at the 'bottom' // already; so the command should be disabled again. Assert.IsFalse(wvm.MoveSheetDown.CanExecute(null), "Move-down command is enabled, should be disabled if the very last sheet is selected."); // Check the the move was performed on the workbook too. Assert.AreEqual(sheetName, wb.Sheets[wb.Sheets.Count].Name, "Moving the sheet down was not performed on the actual workbook"); }
public void MoveFirstSheetDown() { Workbook wb = Instance.Default.CreateWorkbook(3); WorkbookViewModel wvm = new WorkbookViewModel(wb); // Select the first sheet in the collection SheetViewModel svm = wvm.Sheets[0]; string sheetName = svm.DisplayString; // With no sheets selected, the move-down command should // be disabled. wb.Sheets[wb.Sheets.Count].Activate(); wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; Assert.IsFalse(wvm.MoveSheetDown.CanExecute(null), "Move-down command is enabled, should be disabled with no sheets selected."); wb.Sheets[1].Activate(); Assert.IsTrue(wvm.MoveSheetDown.CanExecute(null), "Move-down command is disabled, should be enabled with one sheet selected."); wvm.MoveSheetDown.Execute(null); // Caveat, when accessing the worksheets collection, the index is 1-based! Assert.AreEqual(sheetName, wb.Sheets[2].Name, "The first sheet was not moved down"); }
public void MoveSheetsUp() { Workbook wb = Instance.Default.CreateWorkbook(3); WorkbookViewModel wvm = new WorkbookViewModel(wb); // Select the second sheet in the collection (index #1) SheetViewModel svm = wvm.Sheets[1]; string sheetName = svm.DisplayString; // With no sheets selected, the move-up command should // be disabled. wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; Assert.IsFalse(wvm.MoveSheetUp.CanExecute(null), "Move command is enabled, should be disabled with no sheets selected."); svm.IsSelected = true; Assert.IsTrue(wvm.MoveSheetUp.CanExecute(null), "Move command is disabled, should be enabled with one sheet selected."); wvm.MoveSheetUp.Execute(null); // The selected sheet should now be the first sheet, // which cannot be moved 'up' as it is at the 'top' // already; so the command should be disabled again. Assert.IsFalse(wvm.MoveSheetUp.CanExecute(null), "Move command is enabled, should be disabled if the very first sheet is selected."); // Check the the move was performed on the workbook too. Assert.AreEqual(sheetName, wb.Sheets[1].Name, "Moving the sheet was not performed on the actual workbook"); }
private void AttachToCurrentWindow() { // If the current window does not yet have our task pane, add it to it IntPtr currentHandle = Bovender.Win32Window.MainWindowHandleProvider(); if (!Panes.ContainsKey(currentHandle)) { Logger.Info("Attaching new WorksheetManager panel to window 0x{0:X08}", currentHandle); WorkbookViewModel viewModel; if (Instance.Default.IsSingleDocumentInterface) { // Create a new workbook view model only if this is an SDI application viewModel = new WorkbookViewModel(Instance.Default.ActiveWorkbook); } else { viewModel = _viewModel; } SheetManagerTaskPane tp = new SheetManagerTaskPane(viewModel, Width, Visible); tp.VisibilityChanged += (object sender, SheetManagerEventArgs args) => { OnVisibilityChanged(args.TaskPane); }; Panes.Add(currentHandle, tp); } else { Logger.Info("Window 0x{0:X08} already has a WorksheetManager panel", currentHandle); } }
private TaskPaneManager() { _width = UserSettings.UserSettings.Default.TaskPaneWidth; _viewModel = new WorkbookViewModel(Instance.Default.ActiveWorkbook); AttachToCurrentWindow(); Excel.ViewModels.Instance.Default.Application.WindowActivate += Application_WindowActivate; }
public void SelectSheet() { Workbook wb = Instance.Default.CreateWorkbook(8); WorkbookViewModel wvm = new WorkbookViewModel(wb); wvm.Sheets[2].IsSelected = true; Assert.AreEqual(wvm.Sheets[2].DisplayString, wb.ActiveSheet.Name); wvm.Sheets[4].IsSelected = true; Assert.AreEqual(wvm.Sheets[4].DisplayString, wb.ActiveSheet.Name); }
public void DeleteSheets() { Workbook wb = Instance.Default.CreateWorkbook(8); int oldCount = wb.Sheets.Count; WorkbookViewModel wvm = new WorkbookViewModel(wb); wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; Assert.IsFalse(wvm.DeleteSheets.CanExecute(null), "Delete sheets command should be disabled with no sheets selected."); wvm.Sheets[2].IsSelected = true; // wvm.Sheets[4].IsSelected = true; string sheetName3 = wvm.Sheets[2].DisplayString; string sheetName5 = wvm.Sheets[4].DisplayString; int numSelected = wvm.NumSelectedSheets; Assert.IsTrue(wvm.DeleteSheets.CanExecute(null), "Delete sheets command should be enabled with some sheets selected."); bool messageSent = false; bool confirmDelete = false; wvm.ConfirmDeleteMessage.Sent += (sender, message) => { messageSent = true; message.Content.Confirmed = confirmDelete; message.Respond(); }; wvm.DeleteSheets.Execute(null); Assert.IsTrue(messageSent, "No ViewModelMessage was sent."); Assert.AreEqual(oldCount, wvm.Sheets.Count, "Number of sheets was changed even though deletion was not confirmed."); confirmDelete = true; wvm.DeleteSheets.Execute(null); Assert.AreEqual(oldCount - numSelected, wvm.Sheets.Count, "After deleting sheets, the workbook view model has unexpected number of sheet view models."); Assert.AreEqual(oldCount - numSelected, wb.Sheets.Count, "After deleting sheets, the workbook has unexpected number of sheets."); object obj; Assert.Throws(typeof(System.Runtime.InteropServices.COMException), () => { obj = wb.Sheets[sheetName3]; }, String.Format("Sheet {0} (sheetName3) should have been deleted but is still there.", sheetName3) ); // Assert.Throws(typeof(System.Runtime.InteropServices.COMException), () => // { // obj = wb.Sheets[sheetName5]; // }, // String.Format("Sheet {0} (sheetName5) should have been deleted but is still there.", sheetName5) // ); }
public SheetManagerTaskPane(WorkbookViewModel workbookViewModel, int initialWidth, bool visible) { if (workbookViewModel == null) { throw new ArgumentNullException("workbookViewModel", "WorkbokViewModel must not be null"); } ViewModel = workbookViewModel; _width = initialWidth; _visible = visible; CreateCustomTaskPane(); }
static void Properties() { Xl.Workbook wb = Instance.Default.ActiveWorkbook; if (wb != null) { Logger.Info("Properties"); Excel.ViewModels.WorkbookViewModel vm = new WorkbookViewModel(wb); Bovender.ComHelpers.ReleaseComObject(wb); vm.InjectInto <Excel.Views.PropertiesView>().ShowDialogInForm(); } else { Logger.Info("Properties: There is no active workbook"); } }
public void WorkbookViewModelProperties() { Workbook wb = Instance.Default.CreateWorkbook(); WorkbookViewModel wvm = new WorkbookViewModel(wb); Assert.AreEqual(wvm.DisplayString, wb.Name, "WorkbookViewModel does not give workbook name as display string"); // When accessing sheets in a collection, keep in mind that // the Sheets collection of a Workbook instance is 1-based. Assert.AreEqual(wvm.Sheets[0].DisplayString, wb.Sheets[1].Name, "SheetViewModel in WorkbookViewModel has incorrect sheet name"); Assert.AreEqual(wvm.Sheets.Count, wb.Sheets.Count, "ViewModel and workbook report different sheet counts."); }
public void MoveSheetsToTop() { Workbook wb = Instance.Default.CreateWorkbook(8); WorkbookViewModel wvm = new WorkbookViewModel(wb); wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; // Without sheets selected, the Move-to-top command should be disabled Assert.IsFalse(wvm.MoveSheetsToTop.CanExecute(null), "The Move-to-top command should be disabled without selected sheets."); // // Select the fourth and sixth sheets and remember their names // SheetViewModel svm4 = wvm.Sheets[3]; // svm4.IsSelected = true; // string sheetName4 = svm4.DisplayString; SheetViewModel svm6 = wvm.Sheets[5]; svm6.IsSelected = true; string sheetName6 = svm6.DisplayString; // With sheets selected, the Move-to-top command should be disabled Assert.IsTrue(wvm.MoveSheetsToTop.CanExecute(null), "The Move-to-top command should be enabled with selected sheets."); wvm.MoveSheetsToTop.Execute(null); // Since a selected sheet was moved to the top, the command should // now be disabled again. Assert.IsFalse(wvm.MoveSheetsToTop.CanExecute(null), "The Move-to-top command should be disabled if the first sheet is selected."); // Verify that the display strings of the view models correspond to // the names of the worksheets in the workbook, to make sure that // the worksheets have indeed been rearranged as well. // Assert.AreEqual(sheetName4, wb.Sheets[1].Name, // "Moving the sheets to top was not performed on the actual workbook"); Assert.AreEqual(sheetName6, wb.Sheets[1].Name, "Moving the sheets to top was not performed for all sheets on the actual workbook"); }
public void RenameSheet() { Workbook wb = Instance.Default.CreateWorkbook(); WorkbookViewModel wvm = new WorkbookViewModel(wb); string oldName = wvm.Sheets[0].DisplayString; string newName = "valid new name"; bool messageSent = false; wvm.Sheets.First(sheet => sheet.IsSelected).IsSelected = false; Assert.False(wvm.RenameSheet.CanExecute(null), "Rename sheet command should be disabled with no sheet selected."); wvm.Sheets[0].IsSelected = true; Assert.True(wvm.RenameSheet.CanExecute(null), "Rename sheet command should be enabled with one sheet selected."); wvm.RenameSheetMessage.Sent += (sender, args) => { messageSent = true; args.Content.Confirmed = true; args.Content.Value = newName; args.Respond(); }; wvm.RenameSheet.Execute(null); Assert.True(messageSent, "Rename sheet message was not sent."); Assert.AreEqual(newName, wvm.Sheets[0].DisplayString, String.Format( "Worksheet name is '{0}', should have been renamed to '{1}'.", wvm.Sheets[0].DisplayString, newName ) ); Assert.Throws <InvalidSheetNameException>(() => { newName = "invalid\\sheet\\name"; wvm.RenameSheet.Execute(null); Assert.Fail("Assigning an invalid name should cause an exception."); }); }