我正在使用库 PhpSpreadsheet ,并且需要为此库创建一个自定义函数。
我的情况:
所以
test.php
....
include "..../functions.php";
//for PhpSpreadsheet library
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Cell as Excel_Cell;
require_once '..../..../autoload.php';
....
$newfile = new Spreadsheet();
$first = $newfile->getActiveSheet();
$first->setTitle("test");
$writer = IOFactory::createWriter($newfile, 'Xlsx');
$writer->save(.....);
我需要创建一个使用 Excel_Cell 别名的函数,并将该函数放入function.php文件中。
所以
functions.php
...
function testForSpreadsheet($p1,$p2){
$a1 = Excel_Cell\Coordinate::coordinateFromString($p1);
$a2 = Excel_Cell\Coordinate::columnIndexFromString($a1[0]);
$a3 = Excel_Cell\Coordinate::stringFromColumnIndex($a2 - $p2);
return $a3;
}
...
我发现它使功能正常工作的唯一方法是在use.php中插入use语句
functions.php
...
use PhpOffice\PhpSpreadsheet\Cell as Excel_Cell;
function testForSpreadsheet($p1,$p2){
$a1 = Excel_Cell\Coordinate::coordinateFromString($p1);
$a2 = Excel_Cell\Coordinate::columnIndexFromString($a1[0]);
$a3 = Excel_Cell\Coordinate::stringFromColumnIndex($a2 - $p2);
return $a3;
}
...
这是最好的方法吗?还有另一种方法吗?