From f79aa6392589db518f7292b89a4c8978f5c7c6a5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 28 Aug 2013 17:24:32 -0400 Subject: fix up the merge and autofilter stuff (#1) --- lib/Spreadsheet/Template/Helpers/Xslate.pm | 66 +++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) (limited to 'lib/Spreadsheet/Template/Helpers/Xslate.pm') diff --git a/lib/Spreadsheet/Template/Helpers/Xslate.pm b/lib/Spreadsheet/Template/Helpers/Xslate.pm index e611f1d..8712656 100644 --- a/lib/Spreadsheet/Template/Helpers/Xslate.pm +++ b/lib/Spreadsheet/Template/Helpers/Xslate.pm @@ -3,6 +3,7 @@ use strict; use warnings; use JSON; +use POSIX; my $JSON = JSON->new; @@ -31,30 +32,6 @@ sub format { return ''; } -sub merge { - my ( - $package, $contents, $format, $type, $first_row, - $first_col, $last_row, $last_col, %args - ) = @_; - - return $JSON->encode( - { - contents => "$contents", - format => _formats( $package, $format ), - type => $type, - first_row => $first_row, - first_col => $first_col, - last_row => $last_row, - last_col => $last_col, - ( - defined $args{formula} - ? ( formula => $args{formula} ) - : () - ), - } - ); -} - sub c { my ($package, $contents, $format, $type, %args) = @_; @@ -70,9 +47,50 @@ sub c { }); } +sub merge { + my ($package, $range, $contents, $format, $type, %args) = @_; + + $type = 'string' unless defined $type; + + return $JSON->encode({ + range => _parse_range($range), + contents => "$contents", + format => _formats($package, $format), + type => $type, + (defined $args{formula} + ? (formula => $args{formula}) + : ()), + }); +} + sub true { JSON::true } sub false { JSON::false } +sub _parse_range { + my ($range) = @_; + + $range = [ split ':', $range ] + if !ref($range); + + return [ map { _cell_to_row_col($_) } @$range ] +} + +sub _cell_to_row_col { + my ($cell) = @_; + + return $cell if ref($cell) eq 'ARRAY'; + + my ($col, $row) = $cell =~ /([A-Z]+)([0-9]+)/; + + (my $ncol = $col) =~ tr/A-Z/1-9A-Q/; + $ncol = POSIX::strtol($ncol, 27); + $ncol -= 1; + + my $nrow = $row - 1; + + return [ $nrow, $ncol ]; +} + sub _formats { my ($package, $format) = @_; -- cgit v1.2.3-54-g00ecf