Skip to content

Instantly share code, notes, and snippets.

@hit1205
Last active January 1, 2016 17:39
Show Gist options
  • Save hit1205/187a967168e8870fd3e7 to your computer and use it in GitHub Desktop.
Save hit1205/187a967168e8870fd3e7 to your computer and use it in GitHub Desktop.
給定參數 "末欄編號" 即可產生 Excel 欄位編號陣列 A ~ Z, AA ~ ZZ, AAA ~ ZZZ, ...,參考了 http://php.net/range#107440 這篇 comment 內的寫法,但更正其 3 位數及更高位數下的編號。
<?php
// 建立 Excel 欄位編號
function hit1205_excel_col_no_list_gen($end = 'Z', $prefix = ''){
$cols = array();
$len = strlen($end);
$a2z = range('A', 'Z');
foreach($a2z as $letter){
$col = $prefix . $letter;
$cols[] = $col;
if($col == $end){ return $cols; }
}
for($i = 0; $i <= count($cols); $i ++){
if(!in_array($end, $cols)){
$col = $cols[$i];
$cols2 = hit1205_excel_col_no_list_gen(substr($col, 0, $len - 1) != substr($end, 0, $len - 1) ? substr($col, 0, $len - 1) . 'Z' : $end, substr($col, 0, $len - 1));
$cols = array_merge($cols, $cols2);
}
}
return $cols;
}
var_dump(hit1205_excel_col_no_list_gen('ZZZ'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment