Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Spreadsheet::ParseExcel / WriteExcel question ....

by pryrt (Abbot)
on Nov 29, 2017 at 20:08 UTC ( [id://1204552]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Spreadsheet::ParseExcel / WriteExcel question ....
in thread Spreadsheet::ParseExcel / WriteExcel question ....

Sorry, I don't download random files, especially spreadsheets -- Excel just doesn't have enough security.

poj++ is probably right: if you want to copy one spreadsheet to another, and then just change a couple of things, OLE is probably the way to go.

However, if you want to understand what you're missing, and what's going wrong:

  • Since you're hardcoding border => 1, as I pointed out, that possibly explains the "missing vertical borders" (well, maybe not, since that should presumably set all borders the same...)
  • My gut reaction to why some cells are copying formats correctly, and others not: the Spreadsheet::WriteExcel ->add_format() method creates a new ::Format object, even if all the parameters match. I am wondering if maybe there is a limit to the number of ::Format objects that can be added before there is loss of information.
  • For a full comparison of what's different between the two spreadsheets, after your $workbook->close(), I would instantiate another parser, this time for 'new.xls', and then do a cell-by-cell comparison of the properties for old vs new, using something like Data::Dump's dd() make sure you aren't omitting critical parameters (example code not tested):
    ... # your code above here ## Housekeeping $workbook->close() or die "Error closing file: $!"; close(PROP); { ## COMPARISON use Data::Dump qw/dd dump/; my $old_ws_parse = $worksheet_parse; # copy to new name for symme +try, below my $new_wb_parse = $parser->parse( 'new.xls' ) or die $parser->err +or(), "Error processing new workbook parse.\n"; my $new_ws_parse = $new_wb_parse->worksheet( 'Sheet1' ); open(my $compare_fh, '>', 'compare_formats.txt') or die "compare_f +ormats.txt: $!"; # get in the habit of using lexical filehandles for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $oldcell = $old_ws_parse->get_cell($row,$col) or next; my $newcell = $new_ws_parse->get_cell($row,$col) or next; # to STDERR dd 'oldformat => ', my $oldformat = $oldcell->get_format() +; # you could use Data::Dump::dump() instead, if you want to print t +o your own file (similar to dd 'newformat => ', my $newformat = $newcell->get_format() +; # to file handle print $compare_fh 'oldformat => ', dump($oldformat), "\n"; print $compare_fh 'newformat => ', dump($newformat), "\n"; } } } exit;
    (or, if you used a separate filehandle for old and new for this output, you could use diff or similar utility to quickly find the differences
  • At some point, you may need to dd on the Spreadsheet::WriteExcel object or sub-objects as well, to see if you can figure out when data is getting corrupted while doing the writing.

But, really, based on your statement "I'm really only going to be changing some of the values of a cell, as well as font color and the fill color of the cell - that's all I'll ever need to change.", I really think you should just follow poj's advice.

Replies are listed 'Best First'.
Re^4: Spreadsheet::ParseExcel / WriteExcel question ....
by jhalbrook (Initiate) on Nov 29, 2017 at 21:36 UTC
    Thank you, pyrt, for the source code. Looking at the diffs, I think I'll just give Win32::OLE a shot, and see if I have quicker success.

    Thank you so much for the code, I've learned quite a bit from you!

    Joe
      Having all kinds of problems with Win32::OLE (mostly me, I'm a perl newbie). Would anyone have an example using Spreadsheet::ParseExcel and Spreadsheet::WriteExcel where they simply read in an existing spreadsheet and output a new spreadsheet identical to the original, but perhaps changing the font color and fill color of a few cells?

      Another way of asking, is how do I preserve all of the existing formatting of a cell and simply change one or more attributes such as a font color or fill color on some cells?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found