Closed (fixed)
Project:
PHPExcel
Version:
6.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Reporter:
Created:
18 Jan 2012 at 09:34 UTC
Updated:
9 Feb 2012 at 07:10 UTC
Jump to comment: Most recent file
Use custom sheet names instead of the default "WorksheetX"
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]);
}
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | phpexcel-custom-sheet-names-1408824-0.patch | 971 bytes | aludescher |
Comments
Comment #1
aludescher commentedComment #2
wadmiraal commentedWorking on it. Will patch and run through SimpleTest to see if it works. I'll keep you posted.
Comment #3
wadmiraal commentedSimpleTest is going red. I'm looking into it...
Comment #4
wadmiraal commentedAnd 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.
Comment #5
aludescher commentedThe implementation is even better than my initial proposal. There's just one line of code that I don't understand:
Wouldn't this be enough:
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.
Comment #6
wadmiraal commentedYou're absolutely right ! Can't believe I wrote that :-D!
Comment #7
wadmiraal commentedAnd I see I got the status wrong here as well :-).
Comment #8
wadmiraal commentedNo, 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 phpreset()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 :-)
Comment #9
aludescher commentedI 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
Comment #10
wadmiraal commentedIndeed :-). array_shift() modifies the passed array, which is not what we want ;-). I'll comment the line in dev. Thanks again for your help.
Comment #11
aludescher commentedTrue, 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 :)
Comment #12
wadmiraal commentedYou'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.