trickyq has asked for the wisdom of the Perl Monks concerning the following question:

I have one final question then this project is done

I am having trouble writing to the spreadsheet that the data comes from. Let me explain. I use spreadsheet::xlsx to read two columns from a spreadsheet called build.xlsx. It then compares column A with column D. If there is a match that means there is a price change on that product ID. If not, the product is new. The new products write to a new spreadsheet (per my last post that I got yelled at for reposting) and that works. If there is a match which indicates a price change, then I need the script to write the price from column B to column R (17) in the same spreadsheet. here's the code.

#!/usr/bin/perl use strict; use Spreadsheet::XLSX; use SpreadSheet::WriteExcel; my $excel = Spreadsheet::XLSX -> new ('build.xlsx'); my $sheet = $excel->Worksheet('Sheet1'); my ($row_min,$row_max) = $sheet->row_range(); # scan col D and store values my %colD=(); for my $row ($row_min..$row_max){ my $valD = $sheet->{Cells}[$row][3]->{Val}; $colD{$valD} = $row+1; # excel row number } # scan col A starting at row 2 open FILE,'>','feckyou.txt' or die $!; my $workbook1 = Spreadsheet::WriteExcel->new('newproduct.xls'); my $worksheet1 = $workbook1->add_worksheet(); my $write_row = 1; for my $row (1..$row_max){ my $valA = $sheet->{Cells}[$row][0]->{Val}; my $valB = $sheet->{Cells}[$row][1]->{Val}; # does this value exist in Col D if (exists $colD{$valA}) { my $xlrow = $row+1; #this is where it would write to column R print FILE "price change [A$xlrow]=[D$colD{$valA}] Value=$valB\n"; } else { #output new products to text file # ... $worksheet1->write ($write_row, 0, "$valA"); $worksheet1->write ($write_row, 1, "$valB"); $write_row++; # ... } }

When i try to write to that sheet in that column using

$sheet->write ($write_row, 17, "$valB:);

in place of the txt file it is currently writing to, it errors out. Not sure if there is some way i need to somehow redeclare it or if its locked out from the earlier declaration. It seems like it should be easy.

this is not the same question. same script, totally different problem

thanks

Replies are listed 'Best First'.
Re: Writing to a spreadsheet that the data came from
by chromatic (Archbishop) on Jun 06, 2012 at 18:40 UTC

    What does "it errors out" mean? We can only guess at the problem without the exact text of the error message.

    With that said, the easiest approach for this problem in general is to write a new file, delete (unlink) the old file, then rename the new file.


    Improve your skills with Modern Perl: the free book.