Re: PEAR Spreadsheet_Excel_Writer setLocked method

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Thodoris wrote:
I've used it for some time but never needed to lock a cell. Here is a piece of code that shows how to apply a format to a cell:
 > Hope it helps. I think that by doing something like this:

$format_bold->setLocked();

while creating the format could do the trick but it is not tested.


No, this didn't do it. I already have some formats I've created for dollar amounts, right align, etc, and applying the setLocked() method to them had no affect.

Skip


I have noticed that when you use setLocked to the cell formatting the cell has the protected flag set. My openoffice tells me that the cell protection is meaningful only if the data sheet is protected. I think that the following code does what you need as long as you save the xls before opening it.

<?php
// Include the PEAR script
require 'Spreadsheet/Excel/Writer.php';

// Stop displaying the errors so that the warnings don't get in your spreadsheet
// ini_set('display_errors',0);

// Instantiate a workbook
$workbook = new Spreadsheet_Excel_Writer();

// Send it directly to the browser
$workbook->send("test.xls");

// Set the version (very useful for compatibility)
$workbook->setVersion(8);

// Create a worksheet in the workbook
$worksheet =& $workbook->addWorksheet('Test');

// Set input encoding
$worksheet->setInputEncoding('UTF-8');

// Set protection for the worksheet
$worksheet->protect("");

// Create the formats format
$format_locked =& $workbook->addFormat();
$format_locked->setBold();
$format_locked->setHAlign('center');
$format_locked->setFgColor('yellow');
$format_locked->setLocked();

// Create a format
$format_unlocked =& $workbook->addFormat();
$format_unlocked->setBold();
$format_unlocked->setHAlign('center');
$format_unlocked->setFgColor('yellow');

// Apply the format to a cell
$worksheet->writeString(0, 0, "Locked", $format_locked);
$worksheet->writeString(0, 1, "Unocked", $format_unlocked);

// Close the workbook
$workbook->close();
?>

This protects the file with a blank password. I don't know if this is the proper way to do this but it has been tested and works.

--
Thodoris


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux