简体   繁体   中英

How to assign read only authority to cells in excel through PHP?

I am using Github library ( https://github.com/PHPOffice/PHPExcel/ ) for excel file reading and writing through PHP. Now, the problem is that I want Cells with drop down shall be protected to limited any other input that means when you double-click on the drop down it should not be editable or formattable.

I don't think you really want read-only, because you want to enable users to edit the contents of the cell, but you want to make sure they only choose one of the items from the dropdown box. What you then need is input validation.

You can enable input validation like this:

$objValidation = $objPHPExcel->getActiveSheet()->getCell('B5')->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION );
$objValidation->setAllowBlank(false);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setShowDropDown(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Value is not in list.');
$objValidation->setPromptTitle('Pick from list');
$objValidation->setPrompt('Please pick a value from the drop-down list.');
$objValidation->setFormula1('"Item A,Item B,Item C"');
$objPHPExcel->getActiveSheet()->getCell('B5')->setDataValidation($objValidation);

Source: https://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519927

Please note that input validation is only a gimmick to help the user to enter the correct data. You can not, on a later moment, assume the data in the excel spreadsheet is consistent with the validation. So on a re-upload of the excel file you will need to check for data validity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM