PHP Classes

How to Implement a PHP Excel Convert Column Letter to Number Script Using the Package IrisSG Technical Test: Convert spreadsheet column letters to numbers

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-14 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 10 All time: 11,412 This week: 59Up
Version License PHP version Categories
irissg-technical-tes 1.0MIT/X Consortium ...7Text processing, Parsers, PHP 7, Docu...
Description 

Author

This package can convert spreadsheet column letters to numbers.

It can take a string as a parameter with the letters of the spreadsheet column. Then it returns the respective numbers of the column.

The package can also do the opposite, i.e. covert a column number and return a string with the column letters.

Innovation Award
PHP Programming Innovation award nominee
June 2024
Number 3
Spreadsheets are popular applications often used to show financial values in a table that users can modify to suit their purposes.

Spreadsheet table columns have identifiers for the column position using letters.

This package can convert the spreadsheet position letters to the respective number of the column.

Manuel Lemos
Picture of András Zoltán-Gyárfás
Name: András Zoltán-Gyárfás is available for providing paid consulting. Contact András Zoltán-Gyárfás .
Classes: 7 packages by
Country: Romania Romania
Age: 44
All time rank: 184441 in Romania Romania
Week rank: 180 Up4 in Romania Romania Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php
require_once "vendor/autoload.php";

use
IrisSG\Test\ExcelConverter;
use
IrisSG\Test\DisplayExcelColumns;

// Check if at least one command-line argument is provided
if (count($argv) < 2) {
    echo
"Please provide at least one value to be converted as argument.\n";
    exit(
1);
}


$arguments = $argv;
// lets remove the caller file
array_shift($arguments);

$converter = new ExcelConverter();
$convertedExcelColumnToConsole = new DisplayExcelColumns();

foreach(
$arguments as $argument) {
    echo
$convertedExcelColumnToConsole->format(
       
columnToBeConverted: $argument,
       
conversionResult: (is_numeric($argument) ? $converter->numberToTitle($argument) : $converter->titleToNumber($argument))
    );
}


Details

Task Name: Excel Sheet Column Letter to Column Number And Vice Versa.

Pick from C#, JavaScript or PHP to complete this task, it can be done in any IDE.

  1. __PART 1__: Given a string (columnTitle) that represents the column title as it appears in an Excel sheet, return its corresponding column number.

Example:

| Input | Output | |-------|--------| | A | 1 | | B | 2 | | C | 3 | | Z | 26 | | AA | 27 | | AB | 28 |

  1. __PART 2__: Given a positive number, convert the number to the corresponding Excel column name.

Example:

| Input | Output | |-------|--------| | 26 | Z | | 51 | AY | | 52 | AZ | | 80 | CB | | 676 | YZ | | 702 | ZZ | | 705 | AAC |

Usage

Test from console

php ./index.php A 1 76 AAZ 

Run test

./vendor/bin/phpunit ./tests/ExcelConverter/*

  Files folder image Files (11)  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file Readme.md Doc. Documentation

  Files folder image Files (11)  /  src  
File Role Description
Files folder imageStrategies (3 files)
  Plain text file DisplayExcelColumns.php Class Class source
  Plain text file ExcelConverter.php Class Class source

  Files folder image Files (11)  /  src  /  Strategies  
File Role Description
  Plain text file DisplayAsHtml.php Class Class source
  Plain text file DisplayExcelColumn...erPairInterface.php Class Class source
  Plain text file DisplayOnTerminal.php Class Class source

  Files folder image Files (11)  /  tests  
File Role Description
Files folder imageExcelConverter (2 files)

  Files folder image Files (11)  /  tests  /  ExcelConverter  
File Role Description
  Plain text file ExcelConverter.php Class Class source
  Plain text file ExcelConverterForTerminal.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:10
This week:0
All time:11,412
This week:59Up