summaryrefslogtreecommitdiffstats
path: root/lib/Spreadsheet/Template/Helpers/Xslate.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Spreadsheet/Template/Helpers/Xslate.pm')
-rw-r--r--lib/Spreadsheet/Template/Helpers/Xslate.pm66
1 files changed, 42 insertions, 24 deletions
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) = @_;