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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somsub
    New Member
    • Nov 2008
    • 17

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

    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

    Code:
    use Spreadsheet::WriteExcel;
    $workbook = Spreadsheet::WriteExcel->new('perl.xls');
    $worksheet = $workbook->add_worksheet();
    print "\n \n Enter a path value to take its contns \n";
        $pth=<STDIN>;
        chomp $pth;
        $pth =~ tr#\\#/#;
    	open(MF, "$pth" ) || die "$!";
    	@contents = <MF>;
    	close(MF);
        	$worksheet->write_comment(10, 2, @contents);
        $workbook->close();

    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
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    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.
    Code:
    use Spreadsheet::WriteExcel; 
    $workbook = Spreadsheet::WriteExcel->new('perl.xls'); 
    $worksheet = $workbook->add_worksheet(); 
    print "\n \n Enter a path value to take its contns \n"; 
        $pth=<STDIN>; 
        chomp $pth; 
        $pth =~ tr#\\#/#; 
        $/= "" ;   # undefine input record separator to read entire file
        open(MF, "$pth" ) || die "$!"; 
        $contents = <MF>; # entire file is read into variable
        close(MF); 
            $worksheet->write_comment(10, 2, $contents); 
        $workbook->close();

    Comment

    • somsub
      New Member
      • Nov 2008
      • 17

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

      Comment

      • lanurk
        New Member
        • Oct 2011
        • 1

        #4
        while rewritting Excel Removes the Comments

        My Perl Script is generating excel and updating it daily by extracting contents from server logs, which are writtent o various sheets and colums,
        I have used Spreadsheet::Wr iteExcel and Spreadsheet::Pa rseExcel . I am able to comment also, But when the file is saved again with older data,
        simply by
        Code:
        my $workbook;
            {
                local $^W = 0;
                # Rewrite the file or save as a new file
                $workbook = $existingReport->SaveAs($FILE);
            }
        The older comments on other cells are removed.
        In short, The new comments are still written along with all data except the comments from cells which were written in last run. thank you in advance. Please let me know if detail are needed. Thanks again :)
        Last edited by numberwhun; Oct 25 '11, 04:49 AM. Reason: You need to please use code tags in the forums.

        Comment

        Working...