sergioviniciuss
sergioviniciuss

Reputation: 4696

Can we set a limit of editable excel rows and columns using PHPExcel?

I'd like to find a way to allow the user to see an excel sheet with max 1000 rows and a specific limit of columns too. This link shows a similar example of what I need (and why).

I developed a template with 1000 rows having data validation lists (one for each cell), and I don't want to let the user try to enter data manually after ending the 1000 data validation lists I set. This way, it would be great if PHPExcel could set a range of editable (or visible) rows and columns. I tried to search in the documentation, but I couldn't find something for it (maybe I did a bad research)

Upvotes: 2

Views: 2645

Answers (1)

sergioviniciuss
sergioviniciuss

Reputation: 4696

As it's not possible yet to set a limit of visibility for rows/columns, my solution was this:

First I set protection to the sheet, then I remove the protection of the cells I want to let the user be able to edit:

    $latestTemplateColumn = $objPHPExcel->getActiveSheet()->getHighestDataColumn();
    $latestTemplateColumn++;
    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
    $column = 'A';
    for ($row = 3; $row <= $maxRows; $row++) {
        for ($column = 'A'; $column != $latestTemplateColumn; $column++) {
            $cell = $objPHPExcel->getActiveSheet()->getCell($column.$row);
            $objPHPExcel->getActiveSheet()->getStyle($column.$row)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
        }
    }

This code starts in the row 3 until the $maxRow I set, and also until the last column that has data. This way the user can only enter data in the range I set.

Upvotes: 1

Related Questions