Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim cells by default #4050

Closed
wants to merge 5 commits into from
Closed

Conversation

joelharkes
Copy link
Contributor

@joelharkes joelharkes commented Dec 15, 2023

Please take note of our contributing guidelines: https://docs.laravel-excel.com/3.1/getting-started/contributing.html
Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.

1️⃣ Why should it be added? What are the benefits of this change?

see my suggestion here: #4047

2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.

no, although i think i should make it backwards compatible with an option or static option method somehwere

3️⃣ Does it include tests, if possible?

not yet.

4️⃣ Any drawbacks? Possible breaking changes?

now yes, but not if i add it behind an option flag?

5️⃣ Mark the following tasks as done:

  • Checked the codebase to ensure that your feature doesn't already exist.
  • Take note of the contributing guidelines.
  • Checked the pull requests to ensure that another person hasn't already submitted a fix.
  • Added tests to ensure against regression.

6️⃣ Thanks for contributing! 🙌

@patrickbrouwers
Copy link
Member

How would you propose making this opt in?

@joelharkes
Copy link
Contributor Author

Ideally I'd like to add some form of Middleware but it would be too big a refactor.

I think either a static property on Row or other class or something that can be configured like the chunkSize.

@patrickbrouwers
Copy link
Member

Isn't this something that could already be implemented with a value binder?

@joelharkes
Copy link
Contributor Author

@patrickbrouwers I didn't see the Value Binder was also called in the constructor of a cell to bind the value. i thought it was only called when setting a Cell, not when loading a new spreadsheet. I think we can close this PR indeed

@joelharkes
Copy link
Contributor Author

joelharkes commented Dec 17, 2023

I've been doing some testing though but i don't see the CustomValue binder being hit on import?

actually the ValueBinder can only be used when Excell fields have no data types in them:
image

@joelharkes
Copy link
Contributor Author

@patrickbrouwers what would you advise to make this feature work? it would be great to have an option for this!

as most people using Excels don't understand whitespaces in their cells and often make this mistake, it would be great to trim them by import by default. 🚀

@joelharkes
Copy link
Contributor Author

@patrickbrouwers can i do something to land this change?
it would be great to have this feature!

@@ -97,7 +99,9 @@ public function toArray($nullValue = null, $calculateFormulas = false, $formatDa
$i = 0;
foreach ($this->row->getCellIterator('A', $endColumn) as $cell) {
$value = (new Cell($cell))->getValue($nullValue, $calculateFormulas, $formatData);

if (self::$trimStrings) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this logic to be within the Cell class https://github.com/SpartnerNL/Laravel-Excel/blob/3.1/src/Cell.php

Instead of a public property, I would prefer a method like: Cell::trimStrings() with a default value of true.

private function cleanValue($value)
{
if(!is_string($value)){

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whiteline can be removed here

if(!is_string($value)){

return $value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whiteline after {


return $value;
}
$cleaned = preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment what this preg_replace does.
Also I think this should be ?: instead of ??

@@ -173,4 +177,19 @@ public function setPreparationCallback(Closure $preparationCallback = null)
{
$this->preparationCallback = $preparationCallback;
}

private function cleanValue($value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trimValue

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Jan 2, 2024

Please also add a (feature) test that shows that this feature is actually working like expected when enabling it.

I would prefer the middleware option via the excel.php config, but I understand that will be more work. I'm okay with having a static method on the Cell class to enable trimming cell values.

@patrickbrouwers
Copy link
Member

Replaced this PR by a middleware implementation that can be found in the config: https://github.com/SpartnerNL/Laravel-Excel/blob/3.1/config/excel.php#L161

@joelharkes
Copy link
Contributor Author

@patrickbrouwers thanks, valid point, didnt see it existed.

@patrickbrouwers
Copy link
Member

No worries, I added it last week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants