Propriedade WorksheetBase.Protection
Obtém um Microsoft.Office.Interop.Excel.Protection objeto que representa as opções de proteção da planilha.
Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel.v4.0.Utilities (em Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)
Sintaxe
'Declaração
Public ReadOnly Property Protection As Protection
Get
public Protection Protection { get; }
Valor de propriedade
Tipo: Microsoft.Office.Interop.Excel.Protection
A Microsoft.Office.Interop.Excel.Protection objeto que representa as opções de proteção da planilha.
Exemplos
O exemplo de código a seguir obtém o valor da Protection propriedade para determinar se a formatação de célula é permitida atualmente. Se a formatação de célula não é permitido, em seguida, a Protect método é chamado com o AllowFormattingCells parâmetro definido como true para que as células podem ser formatadas.
Este exemplo é para uma personalização em nível de documento.
Private Sub ProtectCellFormatting()
' Protect cell contents, but do not change any
' other protection type. This enables the Protection
' property to have effect.
Me.Protect(DrawingObjects:=Me.ProtectDrawingObjects, _
Contents:=True, Scenarios:=Me.ProtectScenarios, _
UserInterfaceOnly:=Me.ProtectionMode, _
AllowFormattingCells:=Me.Protection.AllowFormattingCells, _
AllowFormattingColumns:=Me.Protection.AllowFormattingColumns, _
AllowFormattingRows:=Me.Protection.AllowFormattingRows, _
AllowInsertingColumns:=Me.Protection.AllowInsertingColumns, _
AllowInsertingRows:=Me.Protection.AllowInsertingRows, _
AllowInsertingHyperlinks:=Me.Protection.AllowInsertingHyperlinks, _
AllowDeletingColumns:=Me.Protection.AllowDeletingColumns, _
AllowDeletingRows:=Me.Protection.AllowDeletingRows, _
AllowSorting:=Me.Protection.AllowSorting, _
AllowFiltering:=Me.Protection.AllowFiltering, _
AllowUsingPivotTables:=Me.Protection.AllowUsingPivotTables)
If Not Me.Protection.AllowFormattingCells Then
If DialogResult.Yes = MessageBox.Show("Formatting cells is " & _
"not allowed. Allow formatting cells?", "Example", _
MessageBoxButtons.YesNo) Then
' Allow cell formatting, but do not change any other protection type.
Me.Protect(DrawingObjects:=Me.ProtectDrawingObjects, _
Contents:=Me.ProtectContents, Scenarios:=Me.ProtectScenarios, _
UserInterfaceOnly:=Me.ProtectionMode, _
AllowFormattingCells:=True, _
AllowFormattingColumns:=Me.Protection.AllowFormattingColumns, _
AllowFormattingRows:=Me.Protection.AllowFormattingRows, _
AllowInsertingColumns:=Me.Protection.AllowInsertingColumns, _
AllowInsertingRows:=Me.Protection.AllowInsertingRows, _
AllowInsertingHyperlinks:=Me.Protection.AllowInsertingHyperlinks, _
AllowDeletingColumns:=Me.Protection.AllowDeletingColumns, _
AllowDeletingRows:=Me.Protection.AllowDeletingRows, _
AllowSorting:=Me.Protection.AllowSorting, _
AllowFiltering:=Me.Protection.AllowFiltering, _
AllowUsingPivotTables:=Me.Protection.AllowUsingPivotTables)
End If
End If
End Sub
private void ProtectCellFormatting()
{
// Protect cell contents, but do not change any
// other protection type. This enables the Protection
// property to have effect.
this.Protect(missing, this.ProtectDrawingObjects,
true, this.ProtectScenarios, this.ProtectionMode,
this.Protection.AllowFormattingCells,
this.Protection.AllowFormattingColumns,
this.Protection.AllowFormattingRows,
this.Protection.AllowInsertingColumns,
this.Protection.AllowInsertingRows,
this.Protection.AllowInsertingHyperlinks,
this.Protection.AllowDeletingColumns,
this.Protection.AllowDeletingRows,
this.Protection.AllowSorting,
this.Protection.AllowFiltering,
this.Protection.AllowUsingPivotTables);
if (!this.Protection.AllowFormattingCells)
{
if (DialogResult.Yes == MessageBox.Show("Formatting cells is " +
"not allowed. Allow formatting cells?", "Example",
MessageBoxButtons.YesNo))
{
// Allow cell formatting, but do not change any other protection type.
this.Protect(missing, this.ProtectDrawingObjects,
this.ProtectContents, this.ProtectScenarios,
this.ProtectionMode, true,
this.Protection.AllowFormattingColumns,
this.Protection.AllowFormattingRows,
this.Protection.AllowInsertingColumns,
this.Protection.AllowInsertingRows,
this.Protection.AllowInsertingHyperlinks,
this.Protection.AllowDeletingColumns,
this.Protection.AllowDeletingRows,
this.Protection.AllowSorting,
this.Protection.AllowFiltering,
this.Protection.AllowUsingPivotTables);
}
}
}
Segurança do .NET Framework
- Confiança total para o chamador imediato. O membro não pode ser usado por código parcialmente confiável. Para obter mais informações, consulte Usando bibliotecas de código parcialmente confiáveis.