Skip to content

Instantly share code, notes, and snippets.

@iansltx
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iansltx/0b3244c3b055c398a02b to your computer and use it in GitHub Desktop.
Save iansltx/0b3244c3b055c398a02b to your computer and use it in GitHub Desktop.
PHPExcel Convenience Functions
<?php // use as-is with PHPExcel, or build a utility class around them
// $addColumn($sheet, 'A', 1, ['value_in_A1', 'value_in_A2']);
$addColumn = function(\PHPExcel_Worksheet $worksheet, $column, $row, $values) {
for ($i = 0, $count = count($values); $i < $count; $i++, $row++)
$worksheet->getCell($column . $row)->setValue($values[$i]);
};
// $addRow($sheet, 'A', 1, ['value_in_A1', 'value_in_B1']);
$addRow = function(\PHPExcel_Worksheet $worksheet, $column, $row, $values) {
for ($i = 0, $count = count($values); $i < $count; $i++, $column++)
$worksheet->getCell($column . $row)->setValue($values[$i]);
};
// $addMatrix($sheet, 'A', 1, [['value_in_A1', 'value_in_B1'], ['value_in_A2', 'value_in_B2']]);
$addMatrix = function(\PHPExcel_Worksheet $worksheet, $column, $row, $values) use ($addRow) {
for ($i = 0, $count = count($values); $i < $count; $i++, $row++)
$addRow($worksheet, $column, $row, $values[$i]);
};
// $setWidths($sheet, [10, 15, 20, 15], 'B');
$setWidths = function(\PHPExcel_Worksheet $worksheet, $values, $left_column = 'A') {
for ($i = 0, $count = count($values); $i < $count; $i++, $left_column++)
$worksheet->getColumnDimension($left_column)->setWidth($values[$i]);
}
@iansltx
Copy link
Author

iansltx commented Nov 21, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment