如何防止特定细胞的修饰?

发布于 2024-11-27 00:53:36 字数 54 浏览 0 评论 0原文

是否可以防止用户对某些特定单元格进行修改?我可以使用 VSTO 加载项插入 Excel 事件。

Is it possible to prevent user-made modifications of some specific cells? I can use VSTO add-in for plugging into Excel events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×眷恋的温暖 2024-12-04 00:53:36

看一下 工作表.更改事件。此事件采用 Range 对象。据我记得这个对象包含有关选择哪些单元格的信息。然后,您可以使用此单元格执行任何您想要的操作链接

Take a look at Worksheet.Change Event. This event takes a Range object. As far as I remember this objects contains information about what cell(s) is selected. Then you can do with this cell whatever you want link.

后来的我们 2024-12-04 00:53:36

我的方法略有不同。

我保护整个工作表,然后解锁特定的单元格或列或行,以便用户可以在这些工作表中填写详细信息。但你应该得到相同的结果。

代码已在 VS 2008 上进行了测试。

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            var activesheet = (Excel.Worksheet)this.Application.ActiveSheet;


            // keep particular cell unlocked 
            activesheet.get_Range("A1",missing).Locked = false;

            // keep entire row unlocked 

            activesheet.get_Range("B2", missing).EntireRow.Locked = false;

            // keep entire column unlocked 

            activesheet.get_Range("C1", missing).EntireColumn.Locked = false;

            // keep particular range (D4 - E8) unlocked 

            activesheet.get_Range("D4","E8").Locked = false;

            activesheet.Protect(missing, missing, missing, missing, missing, missing, missing,
                                    missing, missing, missing, missing, missing, missing, missing, missing, missing);


        }

当用户单击锁定的单元格时,Excel 会抛出警报消息,指出单元格已锁定。这对用户来说很烦人。您可以禁用displayalerts

只需在声明 var activesheet 之前在 ThisAddIn_Startup(object sender, System.EventArgs e) 中使用

this.Application.DisplayAlerts = false;

,并且不要忘记使用再次启用它,

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            this.Application.DisplayAlerts = true;
        }

这将解决烦人的问题警报消息。

My approach is slightly different.

I protect the complete sheet and then unlock particular cells or columns or rows, so user can fill details in those sheets. But you should get same results with this.

Code has been tested on VS 2008.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            var activesheet = (Excel.Worksheet)this.Application.ActiveSheet;


            // keep particular cell unlocked 
            activesheet.get_Range("A1",missing).Locked = false;

            // keep entire row unlocked 

            activesheet.get_Range("B2", missing).EntireRow.Locked = false;

            // keep entire column unlocked 

            activesheet.get_Range("C1", missing).EntireColumn.Locked = false;

            // keep particular range (D4 - E8) unlocked 

            activesheet.get_Range("D4","E8").Locked = false;

            activesheet.Protect(missing, missing, missing, missing, missing, missing, missing,
                                    missing, missing, missing, missing, missing, missing, missing, missing, missing);


        }

When user clicks on the locked cell, excel throws alert message saying cell is locked. Its annoying for user. You may disable displayalerts.

Just use

this.Application.DisplayAlerts = false;

in the ThisAddIn_Startup(object sender, System.EventArgs e) just before declaring var activesheet and do not forget the enable it in again using

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            this.Application.DisplayAlerts = true;
        }

This will take care of the annoying alert message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文