Specify sheet names in exported files

Use custom sheet names instead of the default "WorksheetX"

Proposed resolution

use array keys in the $headers parameter if not numeric, otherwise use default sheet names. This can be easily achieved by modifying _phpexcel_set_headers in phpexcel.api.inc (line 149)

function _phpexcel_set_headers(&$xls, &$headers) {
	$sheet_names = array_keys($headers);
	$headers = array_values($headers);
	
	if (!is_array($headers[0])) {
		$headers = array($headers);
	}
	
	foreach ($headers as $sheet_id => $sheet_headers) {
		$xls->createSheet($sheet_id);
		
		$sheet = $xls->setActiveSheetIndex($sheet_id);
		
		if (!is_numeric($sheet_names[$sheet_id])) {
			$sheet->setTitle($sheet_names[$sheet_id]);
		}

Comments

aludescher’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new971 bytes
wadmiraal’s picture

Status: Needs review » Active

Working on it. Will patch and run through SimpleTest to see if it works. I'll keep you posted.

wadmiraal’s picture

SimpleTest is going red. I'm looking into it...

wadmiraal’s picture

Status: Fixed » Closed (fixed)

And we're back to green :-). Committed to DEV for now. I'm waiting for more info on #1408872: Cannot benefit from the power of PHPExcel (formatting, merging, calculations, templates) so I can create a single release for both a the same time.

Thank you for your help.

aludescher’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
Status: Active » Closed (fixed)

The implementation is even better than my initial proposal. There's just one line of code that I don't understand:

	if (!is_array(reset(array_values($headers)))) {
		$headers = array($headers);
	}

Wouldn't this be enough:

	if (!is_array($headers)) {
		$headers = (array)$headers;
	}

This would work not just for strings, but it would also cast objects into arrays istead of creating an array containing an object. It might be useful, depending on where your data is coming from.

wadmiraal’s picture

You're absolutely right ! Can't believe I wrote that :-D!

wadmiraal’s picture

Status: Closed (fixed) » Fixed

And I see I got the status wrong here as well :-).

wadmiraal’s picture

Status: Closed (fixed) » Fixed

No, wait, you're wrong. I wrote that on purpose :-) ! I remember why now:

I need to check if the first key ($headers[0]) is an array. But, allowing others to use an associative array (for the worksheet names), I can't use $headers[0]. And using the php reset() function won't do it, because it doesn't work properly with associative arrays. So, I retrieve the array values, reset the pointer (which returns the first entry) and check if it's an array. If not, put the headers inside an array.

That's what that line does. Reverting the change and committing to -dev :-)

aludescher’s picture

I see your point. How about adding a comment to that line?
To be honest, I didn't think about reset returning the first array element. I'd consider using array_shift here because that's all we need, and it might improve code readability.
I don't want to get too picky, sorry if I just did.
Thanks for the quick answers and for the feature implementations

EDIT: forget what I wrote about array_shift :) I don't think it would be a real improvement

wadmiraal’s picture

Indeed :-). array_shift() modifies the passed array, which is not what we want ;-). I'll comment the line in dev. Thanks again for your help.

aludescher’s picture

True, but in this case it would only modify the result of array_values(), not the $headers array. But still, it might not make a big difference in readability :)

wadmiraal’s picture

You're right :-). But indeed, it wouldn't change the code readability much ;-). Thanks for pointing it out though: I like my code readable and efficient, so any comments are always much appreciated.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.