Box\Spout\Writer\Common\Helper\CellHelper::getCellIndexFromColumnIndex PHP Method

getCellIndexFromColumnIndex() public static method

Excel uses A to Z letters for column indexing, where A is the 1st column, Z is the 26th and AA is the 27th. The mapping is zero based, so that 0 maps to A, B maps to 1, Z to 25 and AA to 26.
public static getCellIndexFromColumnIndex ( integer $columnIndex ) : string
$columnIndex integer The Excel column index (0, 42, ...)
return string The associated cell index ('A', 'BC', ...)
    public static function getCellIndexFromColumnIndex($columnIndex)
    {
        $originalColumnIndex = $columnIndex;
        // Using isset here because it is way faster than array_key_exists...
        if (!isset(self::$columnIndexToCellIndexCache[$originalColumnIndex])) {
            $cellIndex = '';
            $capitalAAsciiValue = ord('A');
            do {
                $modulus = $columnIndex % 26;
                $cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
                // substracting 1 because it's zero-based
                $columnIndex = intval($columnIndex / 26) - 1;
            } while ($columnIndex >= 0);
            self::$columnIndexToCellIndexCache[$originalColumnIndex] = $cellIndex;
        }
        return self::$columnIndexToCellIndexCache[$originalColumnIndex];
    }

Usage Example

Example #1
0
 /**
  * Adds data to the worksheet.
  *
  * @param array $dataRow Array containing data to be written. Cannot be empty.
  *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
  * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style.
  * @return void
  * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
  * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
  */
 public function addRow($dataRow, $style)
 {
     $cellNumber = 0;
     $rowIndex = $this->lastWrittenRowIndex + 1;
     $numCells = count($dataRow);
     $rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
     foreach ($dataRow as $cellValue) {
         $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
         $cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
         $cellXML .= ' s="' . $style->getId() . '"';
         if (CellHelper::isNonEmptyString($cellValue)) {
             // CFDB EDIT BEGIN: Special case added to handle HYPERLINK functions
             // this IF wrapping exiting code in ELSE
             $matches = array();
             if (preg_match('/=HYPERLINK\\("(.*)","(.*)"\\)/', $cellValue, $matches)) {
                 // Create a Formula
                 $url = $this->stringsEscaper->escape($matches[1]);
                 $text = $this->stringsEscaper->escape($matches[2]);
                 $formula = sprintf('HYPERLINK("%s","%s")', $url, $text);
                 $cellXML = sprintf('<c r="%s%s" t="str"><f>%s</f><v>%s</v></c>', $columnIndex, $rowIndex, $formula, $text);
             } else {
                 // CFDB EDIT END
                 if ($this->shouldUseInlineStrings) {
                     $cellXML .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>';
                 } else {
                     $sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
                     $cellXML .= ' t="s"><v>' . $sharedStringId . '</v></c>';
                 }
             }
             // CFDB EDIT this line
         } else {
             if (CellHelper::isBoolean($cellValue)) {
                 $cellXML .= ' t="b"><v>' . $cellValue . '</v></c>';
             } else {
                 if (CellHelper::isNumeric($cellValue)) {
                     $cellXML .= '><v>' . $cellValue . '</v></c>';
                 } else {
                     if (empty($cellValue)) {
                         // don't write empty cells (not appending to $cellXML is the right behavior!)
                         $cellXML = '';
                     } else {
                         throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
                     }
                 }
             }
         }
         $rowXML .= $cellXML;
         $cellNumber++;
     }
     $rowXML .= '</row>';
     $wasWriteSuccessful = fwrite($this->sheetFilePointer, $rowXML);
     if ($wasWriteSuccessful === false) {
         throw new IOException("Unable to write data in {$this->worksheetFilePath}");
     }
     // only update the count if the write worked
     $this->lastWrittenRowIndex++;
 }
All Usage Examples Of Box\Spout\Writer\Common\Helper\CellHelper::getCellIndexFromColumnIndex