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   View files Example   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-14 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
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.

Picture of András Zoltán-Gyárfás
Name: András Zoltán-Gyárfás <contact>
Classes: 6 packages by
Country: Romania Romania
Innovation award
Innovation award
Nominee: 3x

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  
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  /  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  /  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  /  tests  
File Role Description
Files folder imageExcelConverter (2 files)

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

 Version Control Unique User Downloads  
 100%
Total:0
This week:0