VSTO 撤回代码修改的值

不废话,直接代码

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();
    }
}
posted @ 2024-05-24 13:50  E024  阅读(51)  评论(0编辑  收藏  举报