PHPackages                             outlaws/spreadsheet-column-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. outlaws/spreadsheet-column-generator

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

outlaws/spreadsheet-column-generator
====================================

Generate the column names for use in spreadsheets

v1.1.1(5y ago)02.8kGPL-2.0-or-laterPHPPHP &gt;=7.1

Since Oct 23Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jurriaanr/spreadsheet-column-generator)[ Packagist](https://packagist.org/packages/outlaws/spreadsheet-column-generator)[ Docs](https://github.com/jurriaanr/spreadsheet-column-generator)[ RSS](/packages/outlaws-spreadsheet-column-generator/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Spreadsheet Column Generator
============================

[](#spreadsheet-column-generator)

When you have a dynamic number of columns to generate a spreadsheet for it can become a real burden to generate them. Especially if you also want to set styling information on those columns as well, f.e. in Excel.

This library was written with PhpOffice\\PhpSpreadsheet () in mind. An excellent library to generate csv or xlsx files.

Each time you call the generator it will generate the next column/cell name. So A-Z and than AA-ZZ etc. Optionally with a row number.

```
$columnGenerator = new ColumnGenerator();
$columnGenerator->getColumn(); // A
$columnGenerator->getColumn(); // B
$columnGenerator->getColumn(); // C
...
$columnGenerator->getColumn(); // Z
$columnGenerator->getColumn(); // AA

```

A real world example would be this

```
$spreadsheet = new Spreadsheet();
$productsSheet = $spreadsheet->createSheet();
$productsSheet->setTitle("Products");

$columnGenerator = new ColumnGenerator(1);

$productsSheet->setCellValue($columnGenerator->getColumn(), 'name'); // A1
$productsSheet->setCellValue($columnGenerator->getColumn(), 'sku');  // B1

$properties = $this->getBigListOfProperties();
foreach ($properties as $property) {
    $productsSheet->setCellValue($columnGenerator->getColumn(), $property->getName());
}

```

Walking the previously generated columns
----------------------------------------

[](#walking-the-previously-generated-columns)

For setting the autosize f.e. on each cell you could of course add another line in the for loop for each cell. Less distracting though is to walk through all generated cells and execute a callback on each generated column:

```
$columnGenerator->walk(function ($columnName) use ($productsSheet) {
    $productsSheet->getColumnDimension(substr($columnName, 0, -1))->setAutoSize(true);
});

```

It is also possible to do this without first generating cells

```
$columnGenerator->walkTo('ZZ1', function ($columnName) use ($productsSheet) {
    ...
});

```

You don't have to be to strict on the given column name, this will work as well (lowercase and leaving out row number):

```
$columnGenerator->walkTo('zz', function ($columnName) use ($productsSheet) {
    ...
});

```

The difference between walk and walkTo is that walk will reset the generator, and then visit all the columns again, up unto where the generator previously stopped. walkto() Will take off from the current position and walk to wherever you say it must go to.

Forwarding / Skipping the first *n* columns
-------------------------------------------

[](#forwarding--skipping-the-first-n-columns)

It is possible you already added multiple columns and just want the rest to be dynamic. You can start from a given number of columns:

```
$columnGenerator = new ColumnGenerator(null, 10);
$columnGenerator->getColumn(); // K

```

It is also possible to forward later on, although the walk methods will still walk these

```
$columnGenerator = new ColumnGenerator(null, 10);
$columnGenerator->getColumn(); // A
$columnGenerator->getColumn(); // B
$columnGenerator->forward(5);
$columnGenerator->getColumn(); // H

```

Getting the right value
-----------------------

[](#getting-the-right-value)

Not all values are created equal. Depending on what you want to do with the generated column name and when there are different options. First of all, the getColumn() method has an optional $movePointerForward parameter. This defaults to true. That means that after receiving the value from the generator, internally the pointer will move forward.

```
$columnGenerator = new ColumnGenerator();
$columnGenerator->getColumn();          // A, internally moves to B
$columnGenerator->getColumn(false);     // B
$columnGenerator->getColumn(false);     // B
$columnGenerator->getColumn(true);      // B, internally moves to C
$columnGenerator->getColumn(false);     // C

```

So after calling $columnGenerator-&gt;getColumn(true) it is not easy to get the value that was returned *again*, since internally the state is already at the next column. For this reason you can call the getCurrentColumn() method, which will return whatever the last call to getColumn(true) returned before moving the pointer.

```
$columnGenerator = new ColumnGenerator();
$columnGenerator->getCurrentColumn();  // A, this is for convenience, in reality getColumn(true) has not yet been called
$columnGenerator->getColumn();         // A, internally moves to B
$columnGenerator->getCurrentColumn();  // A
$columnGenerator->getColumn(false);    // B
$columnGenerator->getCurrentColumn();  // A

```

Some real world use for this could be:

```
$spreadsheet = new Spreadsheet();
$productsSheet = $spreadsheet->createSheet();
$properties = $this->getBigListOfProperties();

$columnGenerator = new ColumnGenerator(1);

foreach ($properties as $property) {
    $productsSheet->setCellValue($columnGenerator->getColumn(), $property->getName());
}

// set bold on each generated column
$productsSheet->getStyle("A1:" . $columnGenerator->getCurrentColumn())->getFont()->setBold(true);

```

Reset
-----

[](#reset)

You can reset the generator, it will start all over again

```
$columnGenerator->getColumn(); // ZX
$columnGenerator->getColumn(); // ZY
$columnGenerator->reset();
$columnGenerator->getColumn(); // A

```

Forever
-------

[](#forever)

Since it is a generator, potentially you can keep going forever. Of course there is a limit to what Excel actually supports.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2024d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7426848?v=4)[jurriaanr](/maintainers/jurriaanr)[@jurriaanr](https://github.com/jurriaanr)

---

Top Contributors

[![jurriaanr](https://avatars.githubusercontent.com/u/7426848?v=4)](https://github.com/jurriaanr "jurriaanr (2 commits)")

---

Tags

generatorexcelxlsxPhpOfficeiteratorspreadsheetworksheet

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/outlaws-spreadsheet-column-generator/health.svg)

```
[![Health](https://phpackages.com/badges/outlaws-spreadsheet-column-generator/health.svg)](https://phpackages.com/packages/outlaws-spreadsheet-column-generator)
```

###  Alternatives

[phpoffice/phpspreadsheet

PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine

13.9k293.5M1.2k](/packages/phpoffice-phpspreadsheet)[openspout/openspout

PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

1.1k57.6M128](/packages/openspout-openspout)[shuchkin/simplexlsxgen

Export data to Excel XLSx file. PHP XLSX generator.

1.1k2.2M16](/packages/shuchkin-simplexlsxgen)[nuovo/spreadsheet-reader

Spreadsheet reader library for Excel, OpenOffice and structured text files

669863.2k8](/packages/nuovo-spreadsheet-reader)[avadim/fast-excel-writer

Lightweight and very fast XLSX Excel Spreadsheet Writer in PHP

2951.2M7](/packages/avadim-fast-excel-writer)[akeneo-labs/spreadsheet-parser

Akeneo Spreadsheet parser. Reads XLXS files from Microsoft Excel and Open Office

147598.3k6](/packages/akeneo-labs-spreadsheet-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
