GithubHelp home page GithubHelp logo

Comments (10)

MarkBaker avatar MarkBaker commented on August 17, 2024 3

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.

MarkBaker avatar MarkBaker commented on August 17, 2024

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.

Quix0r avatar Quix0r commented on August 17, 2024

Okay.

from phpspreadsheet.

Quix0r avatar Quix0r commented on August 17, 2024

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.

Quix0r avatar Quix0r commented on August 17, 2024

@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.

Quix0r avatar Quix0r commented on August 17, 2024

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.

Quix0r avatar Quix0r commented on August 17, 2024

Thank you for sharing it. :-) I may give it a try when my high priority task is finished.

from phpspreadsheet.

Quix0r avatar Quix0r commented on August 17, 2024

I would rewrite it a bit so not-found bindings are being "cached" ($bindings is something like an in-progress cache), too.

from phpspreadsheet.

PowerKiKi avatar PowerKiKi commented on August 17, 2024

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.

Quix0r avatar Quix0r commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.