Connecting Tech Pros Worldwide Forums | Help | Site Map

Unable to put array as a comment on excel sheet cell .

Newbie
 
Join Date: Nov 2008
Location: Bangalore
Posts: 15
#1: Oct 20 '09
Hi,

I m facing a problem to add an array as a comment to a MS excel cell using Spreadsheet :: WriteExcel .

I m using the below code

Expand|Select|Wrap|Line Numbers
  1. use Spreadsheet::WriteExcel;
  2. $workbook = Spreadsheet::WriteExcel->new('perl.xls');
  3. $worksheet = $workbook->add_worksheet();
  4. print "\n \n Enter a path value to take its contns \n";
  5.     $pth=<STDIN>;
  6.     chomp $pth;
  7.     $pth =~ tr#\\#/#;
  8.     open(MF, "$pth" ) || die "$!";
  9.     @contents = <MF>;
  10.     close(MF);
  11.         $worksheet->write_comment(10, 2, @contents);
  12.     $workbook->close();
  13.  

above script reads the contents of the specified text file and takes it in to an array @contents and writes it to the secified cell (ie is 10 th row 2nd column ) using write_comment method .

But executing this i got the following output .

Uneven number of additional arguments at s4.pl line 11.

also the last line $workbook->close(); is not executed after that .


Could anyone help me how to add an array or a filehandle in a MS excel cell comment .

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Oct 21 '09

re: Unable to put array as a comment on excel sheet cell .


The write_comment method accepts the comment string as a single argument. When you pass an array, each element of the array will be considered as an argument and this violates syntax of the method.
You can read entire chunk of the file as a single string and pass it as comment.
Expand|Select|Wrap|Line Numbers
  1. use Spreadsheet::WriteExcel; 
  2. $workbook = Spreadsheet::WriteExcel->new('perl.xls'); 
  3. $worksheet = $workbook->add_worksheet(); 
  4. print "\n \n Enter a path value to take its contns \n"; 
  5.     $pth=<STDIN>; 
  6.     chomp $pth; 
  7.     $pth =~ tr#\\#/#; 
  8.     $/= "" ;   # undefine input record separator to read entire file
  9.     open(MF, "$pth" ) || die "$!"; 
  10.     $contents = <MF>; # entire file is read into variable
  11.     close(MF); 
  12.         $worksheet->write_comment(10, 2, $contents); 
  13.     $workbook->close(); 
  14.  
Newbie
 
Join Date: Nov 2008
Location: Bangalore
Posts: 15
#3: Oct 21 '09

re: Unable to put array as a comment on excel sheet cell .


Yaa it worked peoperly ....!! thnx a lot.. for ur help
Reply