You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add trimValues() and formatValuesUsing() to the reader (#195)
* @
Add trimValues() and formatValuesUsing() to the reader
The reader could already clean up header names (trimHeaderRow,
headersToSnakeCase, formatHeadersUsing) but had no equivalent for the
actual cell values. This adds two symmetric reader methods:
- trimValues(?string $characters = null)
- formatValuesUsing(callable $callback) // receives ($value, $key)
Non-string values (e.g. dates) are left untouched by trimValues().
Formatting runs per-row inside the LazyCollection, preserving the
package low memory usage. Includes tests and README docs.
@
* Strengthen no-header-row trim test to assert trimming on whitespace rows
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Freek Van der Herten <freek@spatie.be>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Just like you can clean up header names, you can clean up the data values themselves. This is handy when importing files where cells contain stray whitespace or need to be normalized.
266
+
267
+
Use `trimValues()` to strip whitespace from every value.
Like `trim`, `trimValues()` accepts an optional argument specifying which characters to trim. This argument is a *set of characters* stripped from both ends of each value (exactly like PHP's [`trim`](https://www.php.net/manual/en/function.trim.php)), **not** a suffix. For example, `trimValues('*')` removes any leading or trailing `*` from every value. Be careful with letters: `trimValues('.com')` would also turn `Tom` into `T`.
286
+
287
+
```php
288
+
$rows = SimpleExcelReader::create($pathToCsv)
289
+
->trimValues('*')
290
+
->getRows();
291
+
```
292
+
293
+
For full control, use `formatValuesUsing()` and pass a closure. The closure receives the value and its header key, so you can normalize values per column. Non-string values (such as dates read from an Excel file) are passed through untouched by `trimValues()`, but your own closure is responsible for handling them.
Under the hood this package uses the [openspout/spout](https://github.com/openspout/openspout) package. You can get to the underlying reader that implements `\OpenSpout\Reader\ReaderInterface` by calling the `getReader` method.
0 commit comments