简体   繁体   English

如何通过PHP向Excel中的单元格分配只读权限?

[英]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. 我正在使用Github库( https://github.com/PHPOffice/PHPExcel/ )通过PHP进行excel文件读写。 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 来源: 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. 请注意,输入验证只是帮助用户输入正确数据的a头。 You can not, on a later moment, assume the data in the excel spreadsheet is consistent with the validation. 您稍后不能假定excel电子表格中的数据与验证一致。 So on a re-upload of the excel file you will need to check for data validity. 因此,在重新上传excel文件时,您将需要检查数据有效性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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