Stepan Riha

NAME

Spreadsheet::WriteExcel::Worksheet::SheetProtection - Sheet Protection extension for Spreadsheet::WriteExcel::Worksheet

SYNOPSIS

        use Spreadsheet::WriteExcel;
        use Spreadsheet::WriteExcel::Worksheet::SheetProtection;

        my $workbook = new Spreadsheet::WriteExcel("file.xls");
        my $worksheet = $workbook->add_worksheet;
        
        ...

        # Protect workseet
        $worksheet->protect;
        
        ## Specify protection settings
        ## Disallow selection of locked cells but allow column formatting
        $worksheet->sheet_protection(
                                -select_locked_cells => 0,
                                -format_columns => 1 );

DESCRIPTION

This module allows you to specify the sheet protection attribute available in recent versions of Microsoft Excel (Menu item: Tools > Protection > Protect Sheet...).

It extends the Spreadsheet::WriteExcel::Worksheet class by adding a sheet_protection method which you use to specify the protection attributes.

Protection Flags

The following flags can be set (or cleared) to specify which aspects of a worksheet are protected.

        SelectLockedCells    (Default set)
        SelectUnlockedCells  (Default set)

        FormatCells
        FormatColumns
        FormatRows

        InsertColumns
        InsertRows
        InsertHyperlinks

        DeleteColumns
        DeleteRows

        Sort

        UseAutoFilters
        UsePivotTableReports

        EditObjects
        EditScenarios

The flag names are case insensitive and non-letter characters are ignored, so the following are all valid and equivalent:

        SelectLockedCells
        "select locked cells"
        -select_locked_cells

METHODS

sheet_protection()

The sheet_protection method sets or returns the current sheetprotection settings.

        print "0x%04x\n", $worksheet->sheet_protection; ## Default protection is 0x4400
        
        ## Allow column formatting but disallow selection of locked cells
        $worksheet->sheet_protection(0x4008);

        print "0x%04x\n", $worksheet->sheet_protection; ## Protection is now 0x4008

Protection settings can also be specified as a hash. If the value is true, the specified protection is enabled, otherwise it's disabled.

        ## Allow column formatting but disallow selection of locked cells
        $worksheet->sheet_protection(
                                -select_locked_cells => 0,
                                -format_columns => 1 );

        print "0x%04x\n", $worksheet->sheet_protection; ## Protection is now 0x4008

        $worksheet->sheet_protection( -sort => 1 );

        print "0x%04x\n", $worksheet->sheet_protection; ## Protection is now 0x4808

CAVEATS

This module depends on the internal workings of Spreadsheet::WriteExcel and has only been tested with version 2.13. It may or may not work with previous version.

It would be better if the functionality of this module were directly incorporated into Spreadsheet::WriteExcel::Worksheet, when that happens this module will become obsolete.

SEE ALSO

Spreadsheet::WriteExcel#protect($password) and Spreadsheet::WriteExcel#set_locked()

The BIFF record format is based on documentation in http://sc.openoffice.org/excelfileformat.pdf. However, that documentation (as of 5/29/2005) contains some errors.

AUTHOR

Stepan Riha, <sriha@cpan.org<gt>

COPYRIGHT AND LICENSE

Copyright 2005 by Stepan Riha

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.