Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created July 23, 2025 23:22
Show Gist options
  • Save rafaehlers/2004f647fe2045abb1d43659c0432dcf to your computer and use it in GitHub Desktop.
Save rafaehlers/2004f647fe2045abb1d43659c0432dcf to your computer and use it in GitHub Desktop.
This code snippet will format field id 6 on form 2 as a date field in excel
<?php // DO NOT COPY THIS LINE
$binder = new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder();
add_filter( 'gfexcel_renderer_cell_properties_2', function ( \PhpOffice\PhpSpreadsheet\Cell\Cell $cell, \GFExcel\Values\BaseValue $value ) use ( $binder ) {
if ( $value->getFieldId() == 6 && $cell->getRow() > 1 ) {
$old_binder = \PhpOffice\PhpSpreadsheet\Cell\Cell::getValueBinder();
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $binder );
$cell->setValue( $value->getValue() );
$cell
->getStyle()
->getNumberFormat()
->setFormatCode( \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD );
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $old_binder );
}
}, 10, 2 );
@rafaehlers
Copy link
Author

Replace the form ID in the filter name: gfexcel_renderer_cell_properties_2 the 2 at the end here is meant to target the form ID = 2.

The '6' here on line 5 is to target the date field if ( $value->getFieldId() == 6

Here's where to add this code: https://docs.gravitykit.com/article/210-where-to-put-code-samples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment