How to apply Excel “Center Across Selection” in PhpSpreadsheet XLSX files

Someone suggested replacing Merged Cells in our Excel-formatted (.xlsx) reports with “Center Across Selection” which gives the same visual result without the undesirable side effects of merged cells.

We currently use PhpSpreadsheet v1.16 to generate our Excel-formatted (.xlsx) reports.

I could NOT find any PhpSpreadsheet documentation or discussion of this feature, other than the following PhpSpreadsheet CHANGELOG reference from over a decade ago:

Horizontal center across selection - @Erik Tilt

I dug into Microsoft file format reference material and finally determined that the PhpSpreadsheet equivalent to Excel “Center Across Selection” is the “Alignment::HORIZONTAL_CENTER_CONTINUOUS” value (aka “centerContinuous”).

Here’s how to apply Excel “Center Across Selection” to a range in PhpSpreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle("A1:C1")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER_CONTINUOUS);
$sheet->setCellValue("A1", "Example");

The code above will write text “Example” to cell A1. The text will be centered across 3 cells (A1,B1,C1).

Did you find this helpful? Let me know by sending me a comment. I tend to update and maintain posts more frequently if I know others find them helpful. Thanks for visiting!

How to Update a Fork from Head in Github

I frequently fork repositories for internal use so that the code we are using does not change. I manually update my fork to match the latest upstream code, and then test the updated code with our system.

I had previously been using git on my local machine to perform this merge and push the updates to my fork on GitHub. I now follow these steps to update my fork with any changes from the upstream/base repository. Thank you to Rick Cogley for posting these steps!

How to Update a Fork in Github

  1. Browse to your forked repository on Github.
  2. Click the “Pull request” link on the right of “This branch is N commits behind”
  3. Click the “switching the base” link to change your fork to the base, and the original to the head fork. Now you should see all of the head commits that need to be pushed to your fork. By default, Github will compare the base fork with your fork, and will show the changes you have made to your fork OR will not find any changes if you made no changes to your fork.
  4. Click the green “Create Pull Request” button, enter a Title for your pull request (e.g. “Merge upstream changes”), then click the green “Create Pull Request” button.
  5. Click the green “Merge pull request” button, then click the green “Confirm Merge” button.

Assuming you had not made changes to your fork, the upstream changes will be merged automatically.