You could use the excel function AVERAGE
  $sheet->write_formula($i-1, 9, "=AVERAGE(B$i:I$i)", $fmt_avg  );

#!/usr/bin/perl use strict; use DBI; use Spreadsheet::WriteExcel; my $listId = shift; my $p_filename = shift; use constant C_HEADING => 0; use constant C_WIDTH => 1; my @columns = ( ['AgentID', 10 ], ['Qu 1', 5 ], ['Qu 2', 5 ], ['Qu 3', 5 ], ['Qu 4', 5 ], ['Qu 5', 5 ], ['Qu 6', 5 ], ['Qu 7', 5 ], ['Qu 8', 5 ], ['Avg', 10 ] ); my $workbook = Spreadsheet::WriteExcel->new($p_filename); my $sheet = $workbook->add_worksheet('Data'); my $fmt_default = $workbook->add_format( font => 'Verdana', border => 1, align => 'center'); my $fmt_bold = $workbook->add_format( font => 'Verdana', border => 1, align => 'center', bold => 1); my $fmt_avg = $workbook->add_format( num_format => '0.00', border => 1, align => 'center', bold => 1, ); for (0..$#columns){ $sheet->write(0,$_,$columns[$_]->[C_HEADING], $fmt_bold); $sheet->set_column($_, $_, $columns[$_]->[C_WIDTH]); } my %score = ( 'strongly agree'=>5,'agree'=>4,'neutral'=>3, 'disagree'=>2,'strongly disagree'=>1 ); my $dsn = 'Server=server;Database=database;Trusted_Connection=Yes'; my $dbh = DBI->connect("DBI:ODBC:Driver={SQLServer};$dsn") or die("\n\nCONNECT ERROR:\n\n$DBI::errstr"); my $sth = $dbh->prepare(<<EOQ SELECT Custom1, Custom13, Custom14, Custom15, Custom16, Custom17, Custom18, Custom19, Custom20 FROM [purl].[dbo].[MailFiles] WHERE ListId=? AND Completed =? EOQ ); $sth->execute($listId,1); my $i = 1; while ( my $row = $sth->fetchrow_arrayref ) { #print "$_ = $ans{lc $_} ; " for @$row; print "\n"; $sheet->write_string($i,0,$row->[0], $fmt_default); $sheet->write_number($i,$_,$score{lc $row->[$_]},$fmt_default) for ( +1..$#$row); $i++; $sheet->write_formula($i-1, 9, "=AVERAGE(B$i:I$i)", $fmt_avg ); } # average $sheet->write_string($i,0,'Average', $fmt_default); for my $c (1..8){ my $col = chr(65+$c); $sheet->write_formula($i, $c, "=AVERAGE(${col}2:${col}$i)",$fmt_avg + ); } $sheet->activate(); exit;
poj

In reply to Re^3: Trying to manipulate data by poj
in thread Trying to manipulate data by emadmahou

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.