protected $_template;
protected $_templateName;
protected $_feedbacks = [];
+ protected static $_maxCol = 26 * 4;
+ protected static $_absoluteMaxCol = 16384;
+ protected static $_pictureNames = ['picture references', 'pictures references', 'picture reference', 'pictures reference'];
public function __construct($id)
{
$feedbackSheets[$name] = [];
}
$feedbackSheets[$name][] = $sheet;
+
+ $sheet->removeColumnByIndex(self::$_maxCol, self::$_absoluteMaxCol - self::$_maxCol);
}
}
continue;
}
$name = $this->_getSheetName($sheet);
+ $sheet->removeColumnByIndex(self::$_maxCol, self::$_absoluteMaxCol - self::$_maxCol);
$this->_processSheet($sheet, $feedbackSheets[$name] ?? []);
}
// Make first sheet active
*/
protected function _getSheetName($sheet)
{
- return mb_strtolower(trim($sheet->getCellByColumnAndRow(1, 1)->getValue()));
+ $highestCol = 300;
+ for ($j = 1; $j <= $highestCol; $j++) {
+ $v = $sheet->getCellByColumnAndRow($j, 1)->getValue();
+ if (null === $v) {
+ continue;
+ }
+ $v = trim($v);
+ if ($v === '') {
+ continue;
+ }
+ return mb_strtolower($v);
+ }
}
/**
protected function _getHeadersRow($sheet)
{
$highestRow = $sheet->getHighestRow();
+ $highestCol = self::$_maxCol;
for ($i = 1; $i <= $highestRow; $i++) {
- if ($sheet->getCellByColumnAndRow(1, $i)->getValue() === 'Picture References') {
- $headersRow = $i;
- break;
+ for ($j = 0; $j <= $highestCol; $j++) {
+ try {
+ $v = $sheet->getCellByColumnAndRow($j, $i)->getValue();
+ if (null === $v) {
+ continue;
+ }
+ $v = mb_strtolower(trim($v));
+ if (in_array($v, self::$_pictureNames)) {
+ $headersRow = $j;
+ break 2;
+ }
+ } catch (\Exception $e) {
+ break;
+ }
}
}
if (!isset($headersRow)) {
protected function _getMaxCol($sheet)
{
- $hr = $this->_getHeadersRow($sheet);
- $maxcol = Coordinate::columnIndexFromString($sheet->getHighestColumn());
- $max = 0;
- $values = [];
- for ($i = 15; $i <= $maxcol; $i++) {
- $v = $sheet->getCellByColumnAndRow($i, $hr)->getValue();
- $values[] = $v;
- if ($v === null || trim($v) === '') {
- break;
- }
- $max = max($i, $max);
- }
- return $max;
+ return self::$_maxCol;
}
/**
if (null === $headersRow) {
return;
}
- $templateMaxCol = $this->_getMaxCol($templateSheet);
+ $templateMaxCol = $this->_getMaxCol($templateSheet);
+ $hr = $this->_getHeadersRow($templateSheet);
+ $templateStartCol = $this->_getStartCol($templateSheet, $hr);
// Add ranking column
- $templateSheet->insertNewColumnBefore('O', 2);
- $h = $templateSheet->getCell('O' . $headersRow);
+
+ $templateSheet->insertNewColumnBefore(Coordinate::stringFromColumnIndex($templateStartCol ), 2);
+
+ $h = $templateSheet->getCell(Coordinate::stringFromColumnIndex($templateStartCol ) . $headersRow);
// Style it
$h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
->getStartColor()
// Set the caption
$h->setValue('Rank');
// Score column
- $h = $templateSheet->getCell('P' . $headersRow);
+ $h = $templateSheet->getCell(Coordinate::stringFromColumnIndex($templateStartCol + 1) . $headersRow);
// Style it
$h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
->getStartColor()
$h->setValue('Global score');
// Get data from feedbacks
- $startCol = 15;
- $eanCol = 4;
$merged = [];
$feedbackId = 0;
foreach ($feedbacks as $feedback) {
$hr = $this->_getHeadersRow($feedback);
+ $eanCol = $this->_getEANCol($feedback, $hr);
$maxcol = $this->_getMaxCol($feedback);
$maxrow = $feedback->getHighestRow();
+ $startCol = $this->_getStartCol($feedback, $hr);
for ($j = $hr + 1; $j <= $maxrow; $j++) {
$ean = trim($feedback->getCellByColumnAndRow($eanCol, $j)->getValue());
if (null === $ean || $ean === '') {
$offset = 0;
// Add score & rank local columns
- for ($i = $startCol; $i <= $templateMaxCol; $i += 4) {
+ for ($i = $templateStartCol; $i <= $templateMaxCol; $i += 4) {
// Add ranking column
- $colname = Coordinate::stringFromColumnIndex(2 + 3 + $i + $offset + 1);
+ $colname = Coordinate::stringFromColumnIndex( 2+3 + $i + $offset + 1);
$templateSheet->insertNewColumnBefore($colname, 2);
$h = $templateSheet->getCell($colname . $headersRow);
// Style it
$offset++;
- $colname = Coordinate::stringFromColumnIndex(2 + 3 + $i + $offset + 1);
+ $colname = Coordinate::stringFromColumnIndex(2+3 + $i + $offset + 1);
$h = $templateSheet->getCell($colname . $headersRow);
// Style it
$h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
foreach ($data as $col => $value) {
$i = $col % 4;
// Set the value
- $templateSheet->setCellValueByColumnAndRow($startCol + 2 + $col + $offset, $row, $value);
+ $templateSheet->setCellValueByColumnAndRow($templateStartCol + 2 + $col + $offset, $row, $value);
// Gather data to make the ranking
if ($i === 0) {
$k = count($rankingData[$row]);
$formerScore = $score;
$formerRank = $rank;
}
- $templateSheet->setCellValueByColumnAndRow($startCol, $row, $orank);
- $templateSheet->setCellValueByColumnAndRow($startCol + 1, $row, $score);
- $templateSheet->getStyleByColumnAndRow($startCol, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
- $templateSheet->getStyleByColumnAndRow($startCol + 1, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
+ $templateSheet->setCellValueByColumnAndRow($templateStartCol, $row, $orank);
+ $templateSheet->setCellValueByColumnAndRow($templateStartCol + 1, $row, $score);
+ $templateSheet->getStyleByColumnAndRow($templateStartCol, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
+ $templateSheet->getStyleByColumnAndRow($templateStartCol + 1, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
$offset = 0;
foreach ($detailScores[$row] as $locale => $detailScore) {
- $col = $startCol + 1 + 5 + $offset;
+ $col = $templateStartCol + 1 + 5 + $offset;
$templateSheet->setCellValueByColumnAndRow($col, $row, $localeRanks[$locale][$row]);
$templateSheet->setCellValueByColumnAndRow($col + 1, $row, $detailScore);
$templateSheet->getStyleByColumnAndRow($col, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
}
+ public function _getStartCol($sheet, $headersRow)
+ {
+ return $this->_getColContaining($sheet, $headersRow, ['yes/no', 'yes / no', 'yes /no', 'yes/ no']);
+ }
+
+ public function _getEANCol($sheet, $headersRow)
+ {
+ return $this->_getColContaining($sheet, $headersRow, ['ean', 'upc', 'ean code']);
+ }
+
+ public function _getPicturesCol($sheet, $headersRow)
+ {
+ return $this->_getColContaining($sheet, $headersRow, self::$_pictureNames);
+ }
+
+ /**
+ * @param $sheet Worksheet
+ * @param $headersRow
+ * @param array $contents
+ */
+ public function _getColContaining($sheet, $headersRow, $contents = [])
+ {
+ $maxcol = $sheet->getHighestColumn($headersRow);
+ for ($j = 1; $j <= $maxcol; $j++) {
+ $v = $sheet->getCellByColumnAndRow($j, $headersRow)->getValue();
+ if (null === $v) {
+ if ($j > 100) {
+ return 1;
+ }
+ continue;
+ }
+ $v = mb_strtolower(trim($v));
+ if (in_array($v, $contents)) {
+ return $j;
+ }
+ }
+ return 1;
+ }
+
public function getFinalFilename()
{
$e = explode('/', $this->getTemplateName());