不廢話,直接代碼 private Stack<Action> actionStack = new Stack<Action>(); private void SetCellValues() { var worksheet = Globals.ThisAddIn.Application.ActiveS ...
不廢話,直接代碼
private Stack<Action> actionStack = new Stack<Action>();
private void SetCellValues()
{
var worksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
var range = worksheet.Range["A1"];
var previousValue = range.Value2;
// 保存當前狀態以便恢復
actionStack.Push(() =>
{
range.Value2 = previousValue;
});
}
private void UndoLastOperation()
{
if (actionStack.Count > 0)
{
var undoAction = actionStack.Pop();
undoAction();
}
}