Skip to content

Commit

Permalink
piter
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrnov committed Feb 12, 2017
1 parent db4f4d7 commit e59b998
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 22 deletions.
10 changes: 9 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
Change Log: `yii2-postman4excel`
===================================

## Version 2.4.6,
**Date:** 2017-02-08
1. (chg): font-size.
2. (enh): ColumnGrouping (Grouping Rows).
3. (enh): Active Column.
4. (chg): Postman4ExcelBehavior::excelDataFormat. (can used/not).
5. (chg): autoSize. (Autosize per-sheet content),

## Version 2.4.5,
**Date:** 2017-02-06
1. (chg): columnAutoSize
1. (chg): columnAutoSize.
2. (chg): headerStyle width (columnAutoSize==false)
3. (enh): valign
4. (enh): Text Wrap
Expand Down
177 changes: 160 additions & 17 deletions Postman4ExcelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function save4Excel($excel_content, $excel_file
if (empty($excel_file)) {
return FALSE;
}

$objPHPExcel = new PHPExcel();
$objProps = $objPHPExcel->getProperties();
$objProps->setCreator($excel_props['creator']);
Expand All @@ -212,23 +213,87 @@ public function save4Excel($excel_content, $excel_file
$style_obj->applyFromArray($style_array);
//start export excel
for ($i = 0; $i < count($excel_content); $i++) {
$each_sheet_content = $excel_content[$i];
$each_sheet_content = $excel_content[$i]; //looping sheet for Array Data Content. -ptr.nov-
if ($i == 0) {
//There will be a default sheet, so no need create
$objPHPExcel->setActiveSheetIndex(intval(0));
$current_sheet = $objPHPExcel->getActiveSheet();
} else {
//create sheet
$objPHPExcel->createSheet();
$current_sheet = $objPHPExcel->getSheet($i);
$current_sheet = $objPHPExcel->getSheet($i); //Set Active sheet & proprties -ptr.nov-
}

/**
* GROUPING CEIL DATA.
* Group rows from Column Set.
* Author : ptr,[email protected].
* Update : 11/02/2017
*/
$ceilsDataContent=$each_sheet_content['ceils']; //Ceils Data Definition. -ptr.nov-
$validateDataInput=ArrayHelper::getColumn($ceilsDataContent,$each_sheet_content["columnGroup"]);
if(self::checkErrorArray($validateDataInput)){ // Jika Input data bukan self::excelDataFormat. -ptr.nov-
if($each_sheet_content["columnGroup"]){ // Jika ColumGroup di set. -ptr.nov-
//$ceilsDataContentSort=self::array_sorting($ceilsDataContent, 'CUST_NM', SORT_ASC);
$columnGroup=$each_sheet_content["columnGroup"];
$ceilsDataContentSortFirst=self::array_sorting($ceilsDataContent, $columnGroup, SORT_ASC); //Grouping Array Column Selected. -ptr.nov-
$ttlColumns=count($ceilsDataContentSortFirst[0]); //Total columns. -ptr.nov-
$ttlRows=count($ceilsDataContentSortFirst); //Total rows. -ptr.nov-
$selectColum = ArrayHelper::getColumn($ceilsDataContentSortFirst,$columnGroup);
$ttlRowsGroupCount=array_count_values($selectColum); //Grouping count of colomn select.
$cnt_sheet_title = count($each_sheet_content['sheet_title']); // count rows of header title
$startRowContent=$cnt_sheet_title+1; // Start Content Ceil = header +1
$jarak=$startRowContent;
foreach($ttlRowsGroupCount as $value){
/**
* Rumus:
* Valus=Count per-item group data.
* jarak=looping jarak + Value, pertama hanya Jarak.
* jika row=2 dan count-grp=3 hitung di mulai "2=1,3=2,4=3.
* (dikurang 2) jika jarak awal=2 maka (2+3)=5, kemudian 5-2 = 3.
*/
$rumusGrp[]=['row'=>$jarak,'jarak'=>($jarak+$value-2)]; // Testing
if($value>1){//jika group lebih besar dari 1,lebih kecil no.group
for($a=$jarak;$a <=($jarak+$value-2); $a++){
$current_sheet->getRowDimension($a)->setOutlineLevel(1);
$current_sheet->getRowDimension($a)->setVisible(false);
$current_sheet->getRowDimension($a)->setCollapsed(true);
}
}
$jarak=$jarak+$value;
}
// print_r($rumusGrp); //validasi input data.
// die();
//Set Data Format Excel.
$rsltData1=self::excelDataFormat($ceilsDataContentSortFirst);
$ceilsDataContentSort=$rsltData1['excel_ceils'];
// print_r($ceilsDataContentSort);
// die();
$current_sheet->getColumnDimension()->setAutoSize($each_sheet_content["autoSize"]);
}else{
$rsltData2=self::excelDataFormat($ceilsDataContent);
$ceilsDataContentSort=$rsltData2['excel_ceils'];
// print_r($ceilsDataContentSort);
// die();
$current_sheet->getColumnDimension()->setAutoSize($each_sheet_content["autoSize"]);
};
}else{
//Old version
$rsltData3=self::excelDataFormat($each_sheet_content['ceils']);
$ceilsDataContentSort=$rsltData3['excel_ceils'];
$current_sheet->getColumnDimension()->setAutoSize($each_sheet_content["autoSize"]);
// print_r($ceilsDataContentSort);
// die();
}
// print_r(self::checkErrorArray($validateDataInput));
// die();

/*
* --HEADER set sheet
*/
$current_sheet->setTitle(str_replace(array('/', '*', '?', '\\', ':', '[', ']'), array('_', '_', '_', '_', '_', '_', '_'), substr($each_sheet_content['sheet_name'], 0, 30))); //add by Scott
// $current_sheet->getColumnDimension()->setAutoSize(true); //Scott, set column autosize
//$current_sheet->getColumnDimension()->setAutoSize(true); //Scott, set column autosize
//$current_sheet->getColumnDimension()->setAutoSize($each_sheet_content["autoSize"]);
//set sheet's current title
$_columnIndex = 'A';

Expand Down Expand Up @@ -307,7 +372,7 @@ public function save4Excel($excel_content, $excel_file



//header Colomn Aligin
//header Colomn And Rows Maintain
for ($y = 0; $y < $cnt_sheet_title; $y++) { //Count Array sheet_title
for ($x = 0; $x < count($each_sheet_content['sheet_title'][$y]); $x++) { //Count sub Array sheet_title by [$y]
//start handle hearder column css
Expand All @@ -322,7 +387,10 @@ public function save4Excel($excel_content, $excel_file
//col width
//update by : [email protected]
//update at : 06/02/2017
$current_sheet->getColumnDimension($_columnIndex)->setAutoSize($autoSize=='true'?true:false);
//$current_sheet->getColumnDimension($_columnIndex)->setAutoSize($autoSize=='true'?true:false);
//$current_sheet->getColumnDimension($_columnIndex)->setAutoSize($each_sheet_content["autoSize"]);
// print_r($each_sheet_content["autoSize"]);
// die();
if (isset($tempStyle["width"]) and $tempStyle['width']) {
$current_sheet->getColumnDimension($tempColumnPosition)->setWidth($tempStyle['width']);
}
Expand Down Expand Up @@ -370,10 +438,7 @@ public function save4Excel($excel_content, $excel_file
}else{
$current_sheet->getStyle($tempColumn)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
}
}



}
//font color
if (isset($tempStyle["color-font"]) and $tempStyle['color-font']){
$current_sheet->getStyle($tempColumn)->getFont()->getColor()->setARGB($tempStyle['color-font']);
Expand All @@ -385,8 +450,18 @@ public function save4Excel($excel_content, $excel_file
$current_sheet->getStyle($tempColumn)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
}

//Font-Size
if (isset($tempStyle["font-size"]) and $tempStyle['font-size']) {
$current_sheet->getStyle($tempColumn)->getFont()->setSize($tempStyle["font-size"]);
}


/* //$current_sheet->getStyle($tempColumn)->setOutlineLevel(1);
$current_sheet->getRowDimension(2+1,1)->setOutlineLevel(1);
//$current_sheet->getRowDimension($x,3)->setVisible(false);
$current_sheet->getRowDimension(2+1,1)->setCollapsed(true);
$current_sheet->getRowDimension(4+1,1)->setOutlineLevel(1);
//$current_sheet->getRowDimension($x,3)->setVisible(false);
$current_sheet->getRowDimension(4+1,1)->setCollapsed(true); */
}
}
}
Expand Down Expand Up @@ -454,17 +529,20 @@ public function save4Excel($excel_content, $excel_file
*/
if (array_key_exists('freezePane', $each_sheet_content) && !empty($each_sheet_content['freezePane'])) {
$current_sheet->freezePane($each_sheet_content['freezePane']);
// $current_sheet->setSelectedCell('A2');
// $current_sheet->getColumnDimension()->setAutoSize($each_sheet_content["autoSize"]);
}

/*
* -- CONTENT ceils
* check State, $startRowContent
* write sheet content
*/
if (array_key_exists('ceils', $each_sheet_content) && !empty($each_sheet_content['ceils'])) {
for ($row = 0; $row < count($each_sheet_content['ceils']); $row++) {
if (array_key_exists('ceils', $each_sheet_content) && !empty($ceilsDataContentSort)) {

for ($row = 0; $row < count($ceilsDataContentSort); $row++) {
//setting row css
$lineRange = "A" . ($row + $startRowContent) . ":" . self::excelColumnName(count($each_sheet_content['ceils'][$row])) . ($row + $startRowContent); //[email protected] - $startRowContent -> mulai rows nilai data warna
$lineRange = "A" . ($row + $startRowContent) . ":" . self::excelColumnName(count($ceilsDataContentSort[$row])) . ($row + $startRowContent); //[email protected] - $startRowContent -> mulai rows nilai data warna
if (($row + 1) % 2 == 1 and isset($each_sheet_content["oddCssClass"])) {
if ($each_sheet_content["oddCssClass"]["color"])
$current_sheet->getStyle($lineRange)->getFont()->getColor()->setARGB($each_sheet_content["oddCssClass"]["color"]);
Expand All @@ -484,8 +562,8 @@ public function save4Excel($excel_content, $excel_file
}
}
//write content
for ($l = 0; $l < count($each_sheet_content['ceils'][$row]); $l++) {
$current_sheet->setCellValueByColumnAndRow($l, $row + $startRowContent, $each_sheet_content['ceils'][$row][$l]); //[email protected] - $startRowContent -> mulai rows nilai data
for ($l = 0; $l < count($ceilsDataContentSort[$row]); $l++) {
$current_sheet->setCellValueByColumnAndRow($l, $row + $startRowContent, $ceilsDataContentSort[$row][$l]); //[email protected] - $startRowContent -> mulai rows nilai data
}
//All column AutoSize, Not last Header more the one
//$current_sheet->getColumnDimension($_columnIndex)->setAutoSize($autoSize=='true'?true:false); //
Expand All @@ -498,7 +576,7 @@ public function save4Excel($excel_content, $excel_file
//print_r($lineRange);
//die();
}
for ($row = 0; $row < (count($each_sheet_content['ceils'])+1); $row++) {
for ($row = 0; $row < (count($ceilsDataContentSort)+1); $row++) {
//if(isset($each_sheet_content['sheet_title'][0])){ // if not have sheet_title -- next PR ptr.nov
$content_sheet_titleFirst = count($each_sheet_content['sheet_title']);
$content_sheet_title=$content_sheet_titleFirst==0?$content_sheet_titleFirst:$content_sheet_titleFirst-1;
Expand Down Expand Up @@ -548,6 +626,11 @@ public function save4Excel($excel_content, $excel_file
$current_sheet->getStyle($tempColumnContent)->getFill()->getStartColor()->setRGB($tempStyle["color-background"]);
$current_sheet->getStyle($tempColumnContent)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
}

//Font-Size
if (isset($tempStyleContent["font-size"]) and $tempStyleContent['font-size']) {
$current_sheet->getStyle($tempColumnContent)->getFont()->setSize($tempStyleContent["font-size"]);
}
}
}
}
Expand All @@ -556,7 +639,9 @@ public function save4Excel($excel_content, $excel_file
}
//}
// print_r($tempColumnContent);
// die();
// die();
//Active cursor. -ptr.nov-
$current_sheet->setSelectedCell('A2');
}
}

Expand Down Expand Up @@ -754,4 +839,62 @@ private static function is_multidim_array($arr) {
}
return true;
}

/**
* @var array
* PHP ARRAY - array_sort and revers increment.
* @author ptrnov [[email protected]]
* @since 1.4.5
* Update : 10/02/2017
*/
private function array_sorting($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();

if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}

switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}

foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}

//refresh row increment.
foreach ($new_array as $row =>$value){
$reversIncrement[]=$value;
}
return $reversIncrement;
}

/**
* Validasi Input data array :Postman4ExcelBehavior::excelDataFormat Atau Direct Langsung.
* 0 = Postman4ExcelBehavior::excelDataFormat.
* 1 = Input data Direct Array langsung Tanpa mengunakan Postman4ExcelBehavior::excelDataFormat.
*/
private function checkErrorArray($data){
foreach($data as $rows){
$rslt=$rows;
}
return $rslt==''?false:true;
}
}
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Once the extension is installed, simply use it in your code by :
//'widgetType'=>'' //Empty same download, file stay on folder "tmp_mix"
//'prefixStr' => yii::$app->user->identity->username,
//'suffixStr' => date('Ymd-His'),
'columnAutoSize'=>'true', //false/true; default True


],
}
Expand All @@ -57,7 +57,7 @@ Once the extension is installed, simply use it in your code by :
$arySqlDataProvider=$sqlDataProvider->allModels;

/*Array Model Data*/
$excel_dataAll = Postman4ExcelBehavior::excelDataFormat($arySqlDataProvider);
$excel_dataAll = Postman4ExcelBehavior::excelDataFormat($arySqlDataProvider); (not used for columnGroup just put 'ceils' => $arySqlDataProvider)
$excel_ceilsAll = $excel_dataAll['excel_ceils'];
$excel_title1 = $excel_dataAll['excel_title'];
$excel_title2 = ['ID','USERNAME'];
Expand All @@ -77,8 +77,13 @@ Once the extension is installed, simply use it in your code by :
[
'sheet_name' => 'TEST EXPORT 1 old version',
'sheet_title' => $excel_title1, //old version
'ceils' => $excel_ceilsAll,
//'freezePane' => 'E2',
'ceils' => $excel_ceilsAll,// atau langsung $arySqlDataProvider;
//for use column Group.
//noted: 'ceils' => "Array Source", not need difinition "Postman4ExcelBehavior::excelDataFormat($array source);"
//'columnGroup'=>Name of Field//column group difinition.
'columnGroup'=>'CUST_NM',//column for grouping.
'autoSize'=>true, //true/false.
//'freezePane' => 'A2',
'headerColor' => Postman4ExcelBehavior::getCssClass("header"), //All Header Color font and Backgroud
'headerColumnCssClass' => [ //old version
[
Expand Down

0 comments on commit e59b998

Please sign in to comment.