Comments (10)
I wrote this quickly and tested against PHPExcel before realising that I should have used PHPSpreadsheet; but it should be easy enough for you to convert it:
$columnBindings = [
'C' => function($cell, $value) {
$cell->setValueExplicit($value);
return true;
},
'E' => function($cell, $value) {
$excelDateValue = PHPExcel_Shared_Date::PHPToExcel(DateTime::createFromFormat('d.m.Y', $value));
$cell->setValueExplicit($excelDateValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
$cell->getWorksheet()->getStyle($cell->getCoordinate())
->getNumberFormat()->setFormatCode($formatCode);
return true;
},
];
class PHPExcel_Cell_CustomValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder {
protected $bindings;
public function __construct(array $bindings = []) {
$this->bindings = $bindings;
}
public function bindValue(PHPExcel_Cell $cell, $value = null)
{
if (isset($this->bindings[$cell->getColumn()])) {
return $this->bindings[$cell->getColumn()]($cell, $value);
}
// Not bound yet? Use parent...
return parent::bindValue($cell, $value);
}
}
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_CustomValueBinder($columnBindings) );
echo date('H:i:s') , " Read from CSV format" , EOL;
$callStartTime = microtime(true);
$objReader = PHPExcel_IOFactory::createReader('CSV')->setDelimiter(';')
->setEnclosure('"')
->setSheetIndex(0);
$objPHPExcelFromCSV = $objReader->load('test.csv');
from phpspreadsheet.
The recommended solution is to create your own cell binder (extending the default binder) that defines how that particular value should be stored... you might also want to get it to handle that date format as well
from phpspreadsheet.
Okay.
from phpspreadsheet.
The problem here is that the format is not always valid. Sometimes they event send numbers with words like "mother" or "phone" in it. So filtering invalid out would result in many rows being dropped. :-(
Any ideas how can I pre-define formats for each column?
from phpspreadsheet.
@MarkBaker wrote:
The recommended solution is to create your own cell binder (extending the default binder) that defines
how that particular value should be stored... you might also want to get it to handle that date format as
well
Yes, also Excel/LibreCalc don't have German date (only ISOs?) and never a data format "phone number". So I have to write my own value binder. Okay, but how can I prefine, e.g. column 5 is "phone" (custom format), 10 is "date", 13 is "ZIP code" and then validate if the data matches the given format?
ZIP codes cannot be numberic (at least not here in Germany) because there are ZIP codes with leading zeros (but alsway 5 digits here in Germany). So an easy regex should do the trick with this at least. Also the German date is easy to validate with a regex (see regexlib.com).
from phpspreadsheet.
I think I have an idea:
- add helper field e.g. $columnFormats in value binder
- pre-set data type (from class
DataType
) per column/worksheet in helper field (through setter, of course) - in bindValue() check if a format is set, if so use it for validation of value and bind it with correct format
from phpspreadsheet.
Thank you for sharing it. :-) I may give it a try when my high priority task is finished.
from phpspreadsheet.
I would rewrite it a bit so not-found bindings are being "cached" ($bindings is something like an in-progress cache), too.
from phpspreadsheet.
It does not seems to an issue with PhpSpreadsheet per see, but rather a question on how to use it. So I close it, but feel free to keep the discussion going...
from phpspreadsheet.
The problem here is, C and E are not fixed to phone number and date format. It could also be S or T. So in the value binder class I need to find out if the column is a phone number/date column. :-(
from phpspreadsheet.
Related Issues (20)
- Theme added to the table, cant be fetched in the functionality HOT 9
- Uncaught PhpOffice\PhpSpreadsheet\Exception: Spreadsheet objects cannot be json encoded in \app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php:1519 HOT 3
- Data Validation HOT 2
- $reader->setTestAutodetect() not usefull when php version > 8.1 HOT 5
- Not able to add custom color in pie chart HOT 38
- Check if $header and $footer are not null
- Cannot set culumn width HOT 2
- Axis Bounds not Recognised Correctly HOT 2
- Unable to hide comments if saving using data that already contains VML drawings. HOT 2
- Incorrect Attribute Value for forceFullCalc in writeCalcPr Method HOT 2
- Your requested sheet index: -1 is out of bounds. The actual number of sheets is 1 HOT 3
- Bug in "Trunc" class HOT 13
- Certain currency formats are handled incorrectly by formatter HOT 1
- Currency and Accounting format wizards unable to generate formats with ISO code currencies. HOT 1
- unable to install in latest version HOT 1
- Error open file Xlsx when modify value HOT 13
- Issue saving PDF file from Excel
- Applying data validation to an entire column HOT 2
- iconv(): Wrong encoding, conversion from "CP936" to "UTF-8" is not allowed HOT 2
- [Bug]: Incorrect definition of date data type HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpspreadsheet.