【问题标题】:Protect UserInterfaceOnly and EnableOutlining保护 UserInterfaceOnly 和 EnableOutlining
【发布时间】:2014-02-06 09:43:31
【问题描述】:

我正在为 Excel 创建一个插件。它正在保护工作簿中的工作表:

public static void ProtectSheetUi(_Worksheet sheet, string password)
{
    sheet.EnableOutlining = true;
    sheet.Protect(password, Contents: true, UserInterfaceOnly: true, AllowFormattingCells: true,
        AllowFormattingColumns: true, AllowFormattingRows: true);
}

但问题是,当我关闭并重新打开工作簿时,分组/取消分组不起作用。这是因为 UserInterfaceOnly 不是持久的。 在工作簿级别,我可以在打开工作表时再次取消保护和保护工作表。但是如何通过插件实现这一点呢?

【问题讨论】:

    标签: c# excel


    【解决方案1】:

    关闭工作簿时不会保存UserInterfaceOnly 设置。打开工作簿时,您必须重置它。执行此操作的最佳位置是在Workbook_Open 事件过程中。所以你需要在你的加载项中处理它。例如(从记忆中执行此操作,因此请忽略拼写错误/语法错误)

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.WorkbookOpen += 
        new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
    }
    
    void Application_WorkbookOpen(Excel.Workbook Wb)
    {
       //~~> Rest of your code
    }
    

    【讨论】:

    • 我需要重新保护工作表吗?这意味着我需要将密码存储在某个地方。我只能将 UserInterfaceOnly 属性设置为 true 吗?
    • AFAIK 和我可能是错的,但不幸的是,您必须取消保护并重新保护它,UserInterfaceOnly 才能生效。
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 2016-09-21
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多