c# - 有没有办法在 Excel 中禁用或限制对功能区/菜单栏的访问?
问题描述
我想打开一个非模态 Win Form 并想限制对功能区/菜单栏的访问。基本上,我只想授予对 Excel 单元格和表格的访问权限。
我在调用表单之前尝试了以下代码,但它不起作用。
for (int i = 1; i < Globals.ThisAddIn.Application.CommandBars.Count; i++)
{
for (int c = 1; c < Globals.ThisAddIn.Application.CommandBars[i].Controls.Count; c++)
{
Globals.ThisAddIn.Application.CommandBars[i].Controls[c].Enabled = false;
}
}
解决方案
您可以使用以下 XML/C# 按名称显示/隐藏功能区。我使用了一个设置来保存可见性的值。
视频
XML
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<ribbon>
<tabs>
<tab idMso="TabHome" getVisible="GetVisible" />
<tab idMso="TabInsert" getVisible="GetVisible" />
<tab idMso="TabReview" getVisible="GetVisible" />
<tab idMso="TabData" getVisible="GetVisible" />
<tab idMso="TabView" getVisible="GetVisible" />
<tab idMso="TabFormulas" getVisible="GetVisible" />
<tab idMso="TabPageLayoutExcel" getVisible="GetVisible" />
<tab idMso="TabDeveloper" getVisible="GetVisible" />
<tab idMso="TabPrintPreview" getVisible="GetVisible" />
<tab idMso="TabAddIns" getVisible="GetVisible" />
<tab idMso="TabSetTableToolsExcel" getVisible="GetVisible" />
<tab
id="tabExample"
label="Example"
insertAfterMso="TabHome"
keytip="EX"
>
<group
id="grpRibbonVisibility"
label="Ribbon Visibility"
imageMso="WatchWindow"
>
<toggleButton
id="tglShowHideRibbons"
label="Show Hide Ribbons"
getPressed="GetPressed"
onAction="OnAction_Boolean"
imageMso="WatchWindow"
size="large"
screentip="Show or Hide Ribbons"
supertip="This will show or hide the system ribbons."
keytip="SHR"
/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
C#
/// <summary>
/// Assigns the visiblity to controls
/// </summary>
/// <param name="control">Represents the object passed into the callback procedure of a control in a ribbon or another user interface that can be customized by using Office Fluent ribbon extensibility. </param>
/// <returns>A method that returns true or false if the control is visible </returns>
public bool GetVisible(Office.IRibbonControl control)
{
try
{
switch (control.Id)
{
case "TabHome":
case "TabInsert":
case "TabReview":
case "TabData":
case "TabView":
case "TabFormulas":
case "TabPageLayoutExcel":
case "TabDeveloper":
case "TabPrintPreview":
case "TabAddIns":
case "TabSetTableToolsExcel":
return Properties.Settings.Default.IsRibbonVisible;
default:
return false;
}
}
catch (Exception ex)
{
//ErrorHandler.DisplayMessage(ex);
return false;
}
}
/// <summary>
/// Used for boolean controls like checkboxes and toggle buttons
/// </summary>
/// <param name="control"></param>
/// <param name="pressed"></param>
public void OnAction_Boolean(Office.IRibbonControl control, bool pressed)
{
try
{
switch (control.Id)
{
case "tglShowHideRibbons":
Properties.Settings.Default.IsRibbonVisible = pressed;
break;
}
ribbon.Invalidate();
}
catch (Exception)
{
//ErrorHandler.DisplayMessage(ex);
}
}
/// <summary>
/// To return the current value for boolean controls
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
public bool GetPressed(Office.IRibbonControl control)
{
try
{
switch (control.Id)
{
case "tglShowHideRibbons":
return Properties.Settings.Default.IsRibbonVisible;
default:
return true;
}
}
catch (Exception)
{
//ErrorHandler.DisplayMessage(ex);
return true;
}
}
/// <summary>
/// Used to update/reset the ribbon values
/// </summary>
public void InvalidateRibbon()
{
ribbon.Invalidate();
}
推荐阅读
- c - 启用分页导致三重故障(已解决)
- css - Ionic 5 应用程序的 IOS 中的图像高度被拉伸
- vue.js - 是否可以在 TinyMCE 中插入 css 选择器
- amazon-route53 - 使用 .net 显示带有 route53 的记录
- node.js - multererror 意外字段多个文件颤动和node.js
- python - 我想在 python 代码中使用 Dirac delta 作为时间函数来求解四个耦合微分方程。我没有做对
- google-apps-script - 如何通过 Google 表格中的二维数组通过 Apps 脚本插入 Big Query?
- java - Eclipse 项目不再导出可运行的 jar 文件
- php - Post ajax 在 IOS 和 MacOs 设备中不起作用
- flutter - 颤振:如何从颤振/提供者更改通知器中获取值