在运行使用Excel的过程时,我通常会在过程开始时关闭某些应用程序设置,然后在过程结束时再次打开它们.
关闭和打开应用程序设置的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XLTimeTracker
{
class API
{
public static void TurnAppSettingsOff()
{
AddinModule.CurrentInstance.ExcelApp.EnableEvents = false;
AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = false;
}
public static void TurnAppSettingsOn()
{
if (AddinModule.CurrentInstance.ExcelApp == null) return;
AddinModule.CurrentInstance.ExcelApp.EnableEvents = true;
AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用以下方式调用此过程:
API.TurnAppSettingsOff();
// my code
API.TurnAppSettingsOn();
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是说我只想打开我跑前的应用程序设置API.TurnAppSettingsOff().什么是编码的好方法?
一些想法:
Boolean screenUpdating = AddinModule.CurrentInstance.ExcelApp.ScreenUpdating;
我希望最终结果是正确的,即使关闭和打开appsettings的函数也调用另一个关闭和打开应用程序设置的功能.
我不知道这是否是最好设置使用一个命令的所有设置,例如API.TurnAppSettingsOff(),或者这将是明智的用户API.TurnScreenUpdatingOff()和API.TurnEventsOff().
Ste*_*zek -1
您可以使用该类Stack<T>来实现后进先出 (LIFO) 行为。
struct ExcelEventSettings
{
public bool EnableEvents;
public bool ScreenUpdating;
}
class Example
{
private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>();
// you can call this function as often as you called SaveAppSettings
public void RestoreAppSettings()
{
if (settingStack.Count == 0)
throw new Exception("There is no previous state!");
ExcelEventSettings prevState = settingStack.Pop();
setCurrentEnableEvents(prevState.EnableEvents);
setCurrentScreenUpdating(prevState.ScreenUpdating);
}
public void SetAppSettings(bool enableEvents, bool screenUpdating)
{
ExcelEventSettings currentState;
currentState.EnableEvents = getCurrentEnableEvents();
currentState.ScreenUpdating = getCurrentScreenUpdating();
settingStack.Push(currentState);
setCurrentScreenUpdating(enableEvents);
setCurrentEnableEvents(screenUpdating);
}
private bool getCurrentEnableEvents()
{
// Here you would call your Excel function
}
private bool getCurrentScreenUpdating()
{
// Here you would call your Excel function
}
private void setCurrentEnableEvents(bool value)
{
// Here you would call your Excel function
}
private void setCurrentScreenUpdating(bool value)
{
// Here you would call your Excel function
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1659 次 |
| 最近记录: |