I had some time on my hands to provide you; and any of you who is wondering how to do something similar, an example.
I’m aware the code quite verbose; but I’m trying to illustrate how stuff works. So I added some comments, and nested if’s.
You can set a date to filter against, by adding ?from_date={date}
where {date}
is something like 2018-02-14
.
Hope this helps someone out.
<?php
// functions.php
/**
* Filtering rows example
* Include row by default, but exclude based on a certain condition
* In this case we exclude every row before $from_date
* And we take $from_date as a query_string parameter
*/
add_filter('gfexcel_output_rows', function ($rows) {
if (!array_key_exists('from_date', $_GET)) {
return true; // nothing to filter
}
$from_date = DateTime::createFromFormat('Y-m-d', $_GET['from_date'])->setTime(0, 0); //2018-07-10 00:00:00
return array_filter($rows, function ($row) use ($from_date) {
foreach ($row as $i => $field) {
// $field can either be a string, or an (extended) BaseValue-class.
// lets say column 2 is a date, and we only want rows after a given date
if ($i === 1) {
// column 2, because the array starts at 0
if ($date = DateTime::createFromFormat('Y-m-d H:i:s', (string) $field)) {
/**
* Note the '(string) $field' ! This is necessary, because it might be a BaseValue-class.
* This converts that class to a string version of the actual value.
*/
if ($date < $from_date) {
return false; //exclude, because date is earlier than we want
}
}
}
}
return true; //include this row by default
});
});