﻿# Spreadsheet: Protection

Protect sheets to prevent editing of locked cells while allowing input in unlocked cells with XLSX round-trip support.

Keywords: spreadsheet, protection, locked, unlock, readonly, security, sheet

> API reference: [RadzenSpreadsheet API](https://blazor.radzen.com/docs/api/Radzen.Blazor.RadzenSpreadsheet.md)

## Examples

## Blazor Spreadsheet Protection

Protect sheets to prevent editing of locked cells while allowing input in unlocked cells. Supports cell-level lock control, sheet protection options, and XLSX round-trip.

```razor
<RadzenSpreadsheet Workbook=@workbook style="height: 600px" />

@code {
    Workbook workbook = new Workbook();

    protected override void OnInitialized()
    {
        BuildProtectedSheet();
        BuildUnlockedRangeSheet();
    }

    void BuildProtectedSheet()
    {
        var sheet = workbook.AddSheet("Protected Sheet", 20, 6);

        sheet.BeginUpdate();

        var titleFormat = new Format { Bold = true, FontSize = 14, BackgroundColor = "#1565c0", Color = "#ffffff", TextAlign = TextAlign.Center };
        var headerFormat = new Format { Bold = true, BackgroundColor = "#e3f2fd", Color = "#005da1" };
        var lockedFormat = new Format { BackgroundColor = "#f5f5f5", Color = "#212121" };
        var unlockedFormat = new Format { BackgroundColor = "#ffffff", Color = "#000000", Locked = false };
        var noteFormat = new Format { Color = "#757575", Italic = true };
        var currencyFormat = new Format { NumberFormat = "$#,##0.00", BackgroundColor = "#ffffff", Color = "#000000", Locked = false };
        var totalFormat = new Format { Bold = true, NumberFormat = "$#,##0.00", BackgroundColor = "#e8f5e9", Color = "#0f2e10" };

        sheet.Columns[0] = 140;
        sheet.Columns[1] = 120;
        sheet.Columns[2] = 120;
        sheet.Columns[3] = 120;
        sheet.Columns[4] = 120;
        sheet.Columns[5] = 180;

        // Title
        sheet.MergedCells.Add(RangeRef.Parse("A1:F1"));
        sheet.Cells["A1"].Value = "Quarterly Budget - Edit the Budget column";
        sheet.Cells["A1"].Format = titleFormat;
        sheet.Rows[0] = 32;

        // Headers (locked)
        string[] headers = { "Department", "Q1 Actual", "Q2 Actual", "Q3 Actual", "Budget", "Variance" };
        for (int i = 0; i < headers.Length; i++)
        {
            sheet.Cells[1, i].Value = headers[i];
            sheet.Cells[1, i].Format = headerFormat;
        }

        // Data
        string[] departments = { "Engineering", "Marketing", "Sales", "Operations", "HR", "Finance" };
        double[][] actuals = {
            new[] { 125000.0, 132000, 128000 },
            new[] { 45000.0, 52000, 48000 },
            new[] { 78000.0, 85000, 92000 },
            new[] { 35000.0, 33000, 36000 },
            new[] { 22000.0, 24000, 23000 },
            new[] { 18000.0, 19000, 18500 }
        };
        double[] budgets = { 130000, 50000, 90000, 35000, 25000, 20000 };

        for (int i = 0; i < departments.Length; i++)
        {
            int row = 2 + i;
            sheet.Cells[row, 0].Value = departments[i];
            sheet.Cells[row, 0].Format = lockedFormat;

            for (int q = 0; q < 3; q++)
            {
                sheet.Cells[row, q + 1].Value = actuals[i][q];
                sheet.Cells[row, q + 1].Format = new Format { NumberFormat = "$#,##0.00", BackgroundColor = "#f5f5f5", Color = "#212121" };
            }

            // Budget column - UNLOCKED (editable)
            sheet.Cells[row, 4].Value = budgets[i];
            sheet.Cells[row, 4].Format = currencyFormat;

            // Variance formula (locked)
            sheet.Cells[row, 5].Formula = $"=E{row + 1}-AVERAGE(B{row + 1}:D{row + 1})";
            sheet.Cells[row, 5].Format = new Format { NumberFormat = "$#,##0.00", BackgroundColor = "#f5f5f5", Color = "#212121" };
        }

        // Totals
        int totalRow = 2 + departments.Length;
        sheet.Cells[totalRow, 0].Value = "Total";
        sheet.Cells[totalRow, 0].Format = new Format { Bold = true, BackgroundColor = "#e8f5e9", Color = "#0f2e10" };
        for (int col = 1; col <= 5; col++)
        {
            var colLetter = (char)('A' + col);
            sheet.Cells[totalRow, col].Formula = $"=SUM({colLetter}3:{colLetter}{totalRow})";
            sheet.Cells[totalRow, col].Format = totalFormat;
        }

        // Note
        sheet.MergedCells.Add(new RangeRef(new CellRef(totalRow + 2, 0), new CellRef(totalRow + 2, 5)));
        sheet.Cells[totalRow + 2, 0].Value = "This sheet is protected. Only the Budget column (E) can be edited. Cell formatting is allowed for all cells.";
        sheet.Cells[totalRow + 2, 0].Format = noteFormat;

        // Enable protection - all cells are locked by default, only Budget column is unlocked
        sheet.Protection.IsProtected = true;
        sheet.Protection.AllowFormatCells = true;
        sheet.Protection.AllowSelectLockedCells = true;
        sheet.Protection.AllowSelectUnlockedCells = true;

        sheet.Rows.Frozen = 2;
        sheet.EndUpdate();
    }

    void BuildUnlockedRangeSheet()
    {
        var sheet = workbook.AddSheet("Form Template", 20, 4);

        sheet.BeginUpdate();

        var titleFormat = new Format { Bold = true, FontSize = 14, BackgroundColor = "#1565c0", Color = "#ffffff", TextAlign = TextAlign.Center };
        var labelFormat = new Format { Bold = true, BackgroundColor = "#f5f5f5", Color = "#212121", TextAlign = TextAlign.Right };
        var inputFormat = new Format { BackgroundColor = "#fff8e1", Color = "#ff6f00", Locked = false };
        var dateInputFormat = new Format { BackgroundColor = "#fff8e1", Color = "#ff6f00", Locked = false, NumberFormat = "yyyy-mm-dd" };
        var noteFormat = new Format { Color = "#757575", Italic = true };
        var sectionFormat = new Format { Bold = true, FontSize = 11, BackgroundColor = "#e3f2fd", Color = "#005da1" };

        sheet.Columns[0] = 140;
        sheet.Columns[1] = 200;
        sheet.Columns[2] = 140;
        sheet.Columns[3] = 200;

        // Title
        sheet.MergedCells.Add(RangeRef.Parse("A1:D1"));
        sheet.Cells["A1"].Value = "Employee Information Form";
        sheet.Cells["A1"].Format = titleFormat;
        sheet.Rows[0] = 32;

        // Personal section
        sheet.MergedCells.Add(RangeRef.Parse("A3:D3"));
        sheet.Cells["A3"].Value = "Personal Details";
        sheet.Cells["A3"].Format = sectionFormat;

        sheet.Cells["A4"].Value = "First Name:"; sheet.Cells["A4"].Format = labelFormat;
        sheet.Cells["B4"].Format = inputFormat;
        sheet.Cells["C4"].Value = "Last Name:"; sheet.Cells["C4"].Format = labelFormat;
        sheet.Cells["D4"].Format = inputFormat;

        sheet.Cells["A5"].Value = "Email:"; sheet.Cells["A5"].Format = labelFormat;
        sheet.Cells["B5"].Format = inputFormat;
        sheet.Cells["C5"].Value = "Phone:"; sheet.Cells["C5"].Format = labelFormat;
        sheet.Cells["D5"].Format = inputFormat;

        sheet.Cells["A6"].Value = "Start Date:"; sheet.Cells["A6"].Format = labelFormat;
        sheet.Cells["B6"].Format = dateInputFormat;
        sheet.Cells["C6"].Value = "Department:"; sheet.Cells["C6"].Format = labelFormat;
        sheet.Cells["D6"].Format = inputFormat;

        // Employment section
        sheet.MergedCells.Add(RangeRef.Parse("A8:D8"));
        sheet.Cells["A8"].Value = "Employment Details";
        sheet.Cells["A8"].Format = sectionFormat;

        sheet.Cells["A9"].Value = "Job Title:"; sheet.Cells["A9"].Format = labelFormat;
        sheet.Cells["B9"].Format = inputFormat;
        sheet.Cells["C9"].Value = "Manager:"; sheet.Cells["C9"].Format = labelFormat;
        sheet.Cells["D9"].Format = inputFormat;

        sheet.Cells["A10"].Value = "Office:"; sheet.Cells["A10"].Format = labelFormat;
        sheet.Cells["B10"].Format = inputFormat;
        sheet.Cells["C10"].Value = "Employee ID:"; sheet.Cells["C10"].Format = labelFormat;
        sheet.Cells["D10"].Format = inputFormat;

        // Notes section
        sheet.MergedCells.Add(RangeRef.Parse("A12:D12"));
        sheet.Cells["A12"].Value = "Notes";
        sheet.Cells["A12"].Format = sectionFormat;

        sheet.Cells["A13"].Value = "Comments:"; sheet.Cells["A13"].Format = labelFormat;
        sheet.MergedCells.Add(RangeRef.Parse("B13:D15"));
        sheet.Cells["B13"].Format = new Format { BackgroundColor = "#fff8e1", Color = "#ff6f00", Locked = false, WrapText = true };

        // Note
        sheet.MergedCells.Add(RangeRef.Parse("A17:D17"));
        sheet.Cells["A17"].Value = "Yellow cells are editable. Labels and structure are protected.";
        sheet.Cells["A17"].Format = noteFormat;

        // Protect the sheet
        sheet.Protection.IsProtected = true;
        sheet.Protection.AllowSelectLockedCells = true;
        sheet.Protection.AllowSelectUnlockedCells = true;

        sheet.EndUpdate();
    }
}
```
