前提・実現したいこと
PHPでExcel 形式のファイルをHTMLのtableタグに変換するシステムを作っています。
機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Fatal error: Uncaught ArgumentCountError: Too few arguments to function tomk79\excel2html\render_html_by_excel::__construct(), 1 passed in /xxxx
該当のソースコード
PHP
1<?php 2require_once(__DIR__.'/data/php/render_html_by_excel.php'); 3 4$src = (new \tomk79\excel2html\render_html_by_excel(__DIR__.'/data/excel.xlsx'))->get_html(array( 5 'renderer'=>'simplify' 6)); 7 8print $src; 9?>
php
1<?php 2/** 3 * excel2html - render_html_by_excel.php 4 */ 5namespace tomk79\excel2html; 6/** 7 * class render_html_by_excel 8 */ 9class render_html_by_excel{ 10 /** 11 * filesystem utility 12 */ 13 private $fs; 14 /** 15 * input xlsx filename 16 */ 17 private $filename; 18 /** 19 * PHPExcel Object 20 */ 21 private $objPHPExcel; 22 /** 23 * constructor 24 */ 25 public function __construct( $fs, $filename ){ 26 $this->fs = $fs; 27 $this->filename = $filename; 28 $this->objPHPExcel = \PHPExcel_IOFactory::load( $this->filename ); 29 } 30 /** 31 * Excel ファイルからHTMLを描画する 32 */ 33 public function render( $options ){ 34 $skipCell = array(); 35 $objWorksheet = $this->objPHPExcel->getActiveSheet(); 36 $mergedCells = $objWorksheet->getMergeCells(); 37 // var_dump($mergedCells); 38 // セル幅を記憶 39 $col_widths = array(); 40 foreach ($objWorksheet->getRowIterator() as $rowIdx=>$row) { 41 $cellIterator = $row->getCellIterator(); 42 try { 43 $cellIterator->setIterateOnlyExistingCells(true); 44 // This loops through all cells, 45 // even if a cell value is not set. 46 // By default, only cells that have a value 47 // set will be iterated. 48 } catch (\PHPExcel_Exception $e) { 49 } 50 foreach ($cellIterator as $colIdxName=>$cell) { 51 $colIdx = \PHPExcel_Cell::columnIndexFromString( $colIdxName ); 52 $col_widths[$colIdx] = intval( $objWorksheet->getColumnDimension($colIdxName)->getWidth() ); 53 } 54 break; 55 } 56 $col_width_sum = array_sum($col_widths); 57 ob_start(); 58 $thead = ''; 59 $tbody = ''; 60 foreach ($objWorksheet->getRowIterator() as $rowIdx=>$row) { 61 // var_dump($rowIdx); //← $rowIdx は1から始まります 62 $tmpRow = ''; 63 $tmpRow .= '<tr>'.PHP_EOL; 64 $cellIterator = $row->getCellIterator(); 65 try { 66 $cellIterator->setIterateOnlyExistingCells(true); 67 // This loops through all cells, 68 // even if a cell value is not set. 69 // By default, only cells that have a value 70 // set will be iterated. 71 } catch (\PHPExcel_Exception $e) { 72 } 73 foreach ($cellIterator as $colIdxName=>$cell) { 74 $colIdx = \PHPExcel_Cell::columnIndexFromString( $colIdxName ); 75 // var_dump($colIdx); 76 $rowspan = 1; 77 $colspan = 1; 78 if( @$skipCell[$colIdxName.$rowIdx] ){ 79 continue; 80 } 81 foreach($mergedCells as $mergedCell){//連結セルの検索 82 if( preg_match('/^'.preg_quote($colIdxName.$rowIdx).'\:([a-zA-Z]+)([0-9]+)$/', $mergedCell, $matched) ){ 83 $maxIdxC = \PHPExcel_Cell::columnIndexFromString( $matched[1] ); 84 // var_dump($colIdx); 85 // var_dump(\PHPExcel_Cell::stringFromColumnIndex($colIdx-1)); 86 // var_dump($maxIdxC); 87 $maxIdxR = intval($matched[2]); 88 for( $idxC=$colIdx; $idxC<=$maxIdxC; $idxC++ ){ 89 for( $idxR=$rowIdx; $idxR<=$maxIdxR; $idxR++ ){ 90 $skipCell[\PHPExcel_Cell::stringFromColumnIndex($idxC-1).$idxR] = \PHPExcel_Cell::stringFromColumnIndex($idxC-1).$idxR; 91 } 92 } 93 $colspan = $maxIdxC-$colIdx+1; 94 $rowspan = $maxIdxR-$rowIdx+1; 95 break; 96 } 97 } 98 // var_dump($colIdx); //← $colIdx は1から始まります 99 $cellTagName = 'td'; 100 if( $rowIdx <= $options['header_row'] || $colIdx <= $options['header_col'] ){ 101 $cellTagName = 'th'; 102 } 103 $cellValue = $cell->getFormattedValue(); 104 switch( $options['cell_renderer'] ){ 105 case 'text': 106 $cellValue = htmlspecialchars($cellValue); 107 $cellValue = preg_replace('/\r\n|\r|\n/', '<br />', $cellValue); 108 break; 109 case 'html': 110 break; 111 case 'markdown': 112 $cellValue = \Michelf\MarkdownExtra::defaultTransform($cellValue); 113 break; 114 } 115 // var_dump( $cell->getNumberFormat() ); 116 // セルのスタイルを調べて、CSSを生成 117 $styles = array(); 118 $cellStyle = $cell->getStyle(); 119 // print('<pre>'); 120 // // $cellStyle->getBorders()->getOutline(); 121 // var_dump($cellStyle->getBorders()->getLeft()->getColor()->getRGB()); 122 // print('</pre>'); 123 if( $options['render_cell_align'] ){ 124 if( $cellStyle->getAlignment()->getHorizontal() != 'general' ){ 125 // text-align は、単純化設定でも出力する 126 array_push( $styles, 'text-align: '.strtolower($cellStyle->getAlignment()->getHorizontal()).';' ); 127 } 128 } 129 if( $options['render_cell_height'] ){ 130 array_push( $styles, 'height: '.intval($objWorksheet->getRowDimension($rowIdx)->getRowHeight()).'px;' ); 131 } 132 if( $options['render_cell_font'] ){ 133 array_push( $styles, 'color: #'.strtolower($cellStyle->getFont()->getColor()->getRGB()).';' ); 134 array_push( $styles, 'font-weight: '.($cellStyle->getFont()->getBold()?'bold':'normal').';' ); 135 array_push( $styles, 'font-size: '.intval($cellStyle->getFont()->getsize()/12*100).'%;' ); 136 } 137 if( $options['render_cell_background'] ){ 138 array_push( $styles, 'background-color: #'.strtolower($cellStyle->getFill()->getStartColor()->getRGB()).';' ); 139 } 140 if( $options['render_cell_vertical_align'] ){ 141 $verticalAlign = strtolower($cellStyle->getAlignment()->getVertical()); 142 array_push( $styles, 'vertical-align: '.($verticalAlign=='center'?'middle':$verticalAlign).';' ); 143 } 144 if( $options['render_cell_borders'] ){ 145 array_push( $styles, 'border-top: '.$this->get_borderstyle_by_border($cellStyle->getBorders()->getTop()).';' ); 146 array_push( $styles, 'border-right: '.$this->get_borderstyle_by_border($cellStyle->getBorders()->getRight()).';' ); 147 array_push( $styles, 'border-bottom: '.$this->get_borderstyle_by_border($cellStyle->getBorders()->getBottom()).';' ); 148 array_push( $styles, 'border-left: '.$this->get_borderstyle_by_border($cellStyle->getBorders()->getLeft()).';' ); 149 } 150 $tmpRow .= '<'.$cellTagName.($rowspan>1?' rowspan="'.$rowspan.'"':'').($colspan>1?' colspan="'.$colspan.'"':'').''.(count($styles)?' style="'.htmlspecialchars(implode(' ',$styles)).'"':'').'>'; 151 $tmpRow .= $cellValue; 152 // $tmpRow .= $cellStyle->getFill()->getFillType(); 153 $tmpRow .= '</'.$cellTagName.'>'.PHP_EOL; 154 } 155 $tmpRow .= '</tr>'.PHP_EOL; 156 if( $rowIdx <= $options['header_row'] ){ 157 $thead .= $tmpRow; 158 }else{ 159 $tbody .= $tmpRow; 160 } 161 } 162 // var_dump($skipCell); 163 if( !@$options['strip_table_tag'] ){ 164 print '<table>'.PHP_EOL; 165 } 166 if( $options['render_cell_width'] ){ 167 print '<colgroup>'.PHP_EOL; 168 foreach( $col_widths as $colIdx=>$colRow ){ 169 print '<col style="width:'.floatval($colRow/$col_width_sum*100).'%;" />'.PHP_EOL; 170 } 171 print '</colgroup>'.PHP_EOL; 172 } 173 if( strlen($thead) ){ 174 print '<thead>'.PHP_EOL; 175 print $thead; 176 print '</thead>'.PHP_EOL; 177 } 178 print '<tbody>'.PHP_EOL; 179 print $tbody; 180 print '</tbody>'.PHP_EOL; 181 if( !@$options['strip_table_tag'] ){ 182 print '</table>'.PHP_EOL; 183 } 184 $rtn = ob_get_clean(); 185 return $rtn; 186 } // render() 187 /** 188 * ボーダーオブジェクトからHTMLのborder-style名を得る 189 */ 190 private function get_borderstyle_by_border( $border ){ 191 $style = $border->getBorderStyle(); 192 $border_width = '1px'; 193 $border_style = 'solid'; 194 switch( $style ){ 195 case 'none': 196 $border_width = '0'; 197 $border_style = 'none'; 198 break; 199 case 'dashDot': 200 $border_style = 'dashed'; 201 break; 202 case 'dashDotDot': 203 $border_style = 'dashed'; 204 break; 205 case 'dashed': 206 $border_style = 'dashed'; 207 break; 208 case 'dotted': 209 $border_style = 'dotted'; 210 break; 211 case 'double': 212 $border_width = '3px'; 213 $border_style = 'double'; 214 break; 215 case 'hair': 216 break; 217 case 'medium': 218 $border_width = '3px'; 219 break; 220 case 'mediumDashDot': 221 $border_width = '3px'; 222 $border_style = 'dashed'; 223 break; 224 case 'mediumDashDotDot': 225 $border_width = '3px'; 226 $border_style = 'dashed'; 227 break; 228 case 'mediumDashed': 229 $border_width = '3px'; 230 $border_style = 'dashed'; 231 break; 232 case 'slantDashDot': 233 $border_width = '3px'; 234 $border_style = 'solid'; 235 break; 236 case 'thick': 237 $border_width = '5px'; 238 $border_style = 'solid'; 239 break; 240 case 'thin': 241 $border_width = '1px'; 242 $border_style = 'solid'; 243 break; 244 } 245 $rtn = $border_width.' '.$border_style.' #'.strtolower($border->getColor()->getRGB()).''; 246 return $rtn; 247 } // get_borderstyle_by_border() 248}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。