How do I Protect an entire column BASED on the selected excel drop-down value using php in a excel file?

132 Views Asked by At

The downloadable excel file will contain a dropdown menu. Selecting the dropdown menu will cause certain cells/columns to be protected. The excel file will have the embed code to configure the functionality of the dropdown which is to protect certain cells/columns.

The code below inserts a dropdown in cell "A2" that includes the options "1,2,3" in an excel file.

$validation = $sheet->getCell("A2")->getDataValidation();
$validation->setType(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
$validation->setFormula1('"1,2,3"');
$validation->setAllowBlank(false);
$validation->setShowDropDown(true);
$validation->setShowInputMessage(true);

I want it so if option '1' is selected, the entire column I chose is protected.

    Sub test()
If Me.[A2] = "Yes" Then
    Me.Unprotect
    Me.Range("B2:B14").Locked = False
    Me.Protect
ElseIf Me.[B1] = "No" Then
    Me.Unprotect
    Me.Range("B2:B14").Locked = True
    Me.Protect
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
test
End If
End Sub

The macro code inserted above for reference is what I want in PHP to lock a specific column based on a "yes" or a "no" selection in the excel dropdown. Is it possible to embed the macrocode above using PHP?

0

There are 0 best solutions below