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

Added extractVariables method to IOFactory #2515

Conversation

sibalonat
Copy link
Contributor

@sibalonat sibalonat commented Nov 30, 2023

Description

Overview

This pull request introduces a new method extractVariables to the IOFactory class in PHPWord. The primary goal of this enhancement is to enable the extraction of variables from a Word document, particularly those that are linked to spreadsheet cell coordinates. This functionality lays the groundwork for more advanced integrations between Word documents and spreadsheets, allowing for dynamic data retrieval and manipulation.

Technical Details

The extractVariables method scans a Word document and extracts all instances of variables following a specific pattern (e.g., ${variable}). This is particularly useful in scenarios where these variables correspond to cell coordinates in a spreadsheet. By extracting these variables, we can programmatically search for corresponding values in linked spreadsheets, thereby automating the process of updating Word documents with dynamic data from spreadsheets.

Use Cases

  • Dynamic Document Generation: Automate the generation of Word documents with real-time data from spreadsheets. For instance, generating financial reports, invoices, or personalized documents where key figures are drawn from a spreadsheet.
  • Template Processing: Enhance template processing by allowing templates to specify spreadsheet data sources directly within the Word document.

Implementation

The implementation involved modifying the IOFactory class to include the extractVariables method. This method leverages existing structures within PHPWord to navigate through a document's sections and elements, identifying and aggregating variables.

Tests

Appropriate unit tests have been added to ensure the functionality works as expected and to prevent future regressions. These tests cover a range of scenarios, including documents with a variety of variable patterns and formats.

Conclusion

This enhancement is a step towards deeper integration between Word documents and spreadsheets, unlocking new potentials in document automation and data-driven report generation. It aligns with PHPWord's goal of providing robust and flexible tools for word processing and document manipulation.

Fixes #2514

Checklist:

  • [X ] I have run composer run-script check --timeout=0 and no errors were reported
  • [ X] The new code is covered by unit tests (check build/coverage for coverage report)
  • [ -] I have updated the documentation to describe the changes

@coveralls
Copy link

coveralls commented Nov 30, 2023

Coverage Status

coverage: 97.218% (+0.004%) from 97.214%
when pulling d3eac68 on sibalonat:Word2007-Reader---Extract-variables-from-word
into 9bf816b on PHPOffice:master.

@Progi1984 Progi1984 added this to the 2.0.0 milestone Nov 30, 2023
src/PhpWord/IOFactory.php Outdated Show resolved Hide resolved
Copy link
Member

@Progi1984 Progi1984 left a comment

Choose a reason for hiding this comment

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

@sibalonat Great improvement which improve the Developer Experience.

Could you fix my comments and update the changelog 2.0.0.md file, please ?

@sibalonat
Copy link
Contributor Author

I would gladly do so @Progi1984. Thanks for the comments and ill let you know as soon as it's done.

oleibman and others added 7 commits January 7, 2024 18:21
Fix PHPOffice#2526. Most of that issue has already been fixed. The one remaining problem was a deprecation message handling font size. The code used `dechex($operand / 2)`, and issued the deprecation message whenever `$operand` was odd because `dechex` is designed only for integer conversion. `$operand` is actually 2 times the point size, so it will be odd only when the point size is some integer plus half a point (no other fractions are allowed). At any rate, it seems that `dechex` should not be used here in the first place; font size is a numeric value, not a hex string.

There are many problems with MsDoc Reader at the moment. This PR is narrowly focused on the problem at hand. Its test is, at least, more detailed than the existing MsDoc Reader test, which does nothing more than confirm that read successfully creates a PhpWord object. The new test verifies that the font size is as expected, but does not validate any other aspect of the read.
Bumps [dompdf/dompdf](https://github.com/dompdf/dompdf) from 2.0.3 to 2.0.4.
- [Release notes](https://github.com/dompdf/dompdf/releases)
- [Commits](dompdf/dompdf@v2.0.3...v2.0.4)

---
updated-dependencies:
- dependency-name: dompdf/dompdf
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>
Bumps [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit) from 9.6.13 to 9.6.14.
- [Changelog](https://github.com/sebastianbergmann/phpunit/blob/9.6.14/ChangeLog-9.6.md)
- [Commits](sebastianbergmann/phpunit@9.6.13...9.6.14)

---
updated-dependencies:
- dependency-name: phpunit/phpunit
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Bumps [mpdf/mpdf](https://github.com/mpdf/mpdf) from 8.2.0 to 8.2.2.
- [Release notes](https://github.com/mpdf/mpdf/releases)
- [Changelog](https://github.com/mpdf/mpdf/blob/development/CHANGELOG.md)
- [Commits](mpdf/mpdf@v8.2.0...v8.2.2)

---
updated-dependencies:
- dependency-name: mpdf/mpdf
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
docs/changes/2.x/2.0.0.md Outdated Show resolved Hide resolved
src/PhpWord/IOFactory.php Outdated Show resolved Hide resolved
src/PhpWord/IOFactory.php Outdated Show resolved Hide resolved
Copy link
Member

@Progi1984 Progi1984 left a comment

Choose a reason for hiding this comment

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

Some last feedbacks

@sibalonat
Copy link
Contributor Author

Ill add them now, thanks again, much appreciated :) @Progi1984

@Progi1984 Progi1984 merged commit 98d038e into PHPOffice:master Jan 8, 2024
13 checks passed
@Progi1984
Copy link
Member

Thanks @sibalonat for your contribution.

@sibalonat
Copy link
Contributor Author

sibalonat commented Jan 8, 2024

Thank you very much to you sir :D

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

Successfully merging this pull request may close these issues.

Add Variable Extraction Feature to IOFactory for Enhanced Spreadsheet Integration
4 participants