~ubuntu-branches/ubuntu/gutsy/horae/gutsy

« back to all changes in this revision

Viewing changes to 0CPAN/Spreadsheet-WriteExcel-2.15/examples/convertA1.pl

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2006-12-28 12:36:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061228123648-9xnjr76wfthd92cq
Tags: 064-1
New upstream release, dropped dependency on libtk-filedialog-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -w
2
 
 
3
 
###############################################################################
4
 
#
5
 
# This program contains helper functions to deal with the Excel A1 cell
6
 
# reference  notation.
7
 
#
8
 
# These functions have been superceded by Spreadsheet::WriteExcel::Utility.
9
 
#
10
 
# reverse('�'), March 2001, John McNamara, jmcnamara@cpan.org
11
 
#
12
 
 
13
 
use strict;
14
 
 
15
 
print "\n";
16
 
print "Cell B7   is equivalent to (";
17
 
print join " ", cell_to_rowcol('B7');
18
 
print ") in row column notation.\n";
19
 
 
20
 
print "Cell \$B7  is equivalent to (";
21
 
print join " ", cell_to_rowcol('$B7');
22
 
print ") in row column notation.\n";
23
 
 
24
 
print "Cell B\$7  is equivalent to (";
25
 
print join " ", cell_to_rowcol('B$7');
26
 
print ") in row column notation.\n";
27
 
 
28
 
print "Cell \$B\$7 is equivalent to (";
29
 
print join " ", cell_to_rowcol('$B$7');
30
 
print ") in row column notation.\n\n";
31
 
 
32
 
print "Row and column (1999, 29)       are equivalent to ";
33
 
print rowcol_to_cell(1999, 29),   ".\n";
34
 
 
35
 
print "Row and column (1999, 29, 0, 1) are equivalent to ";
36
 
print rowcol_to_cell(1999, 29, 0, 1),   ".\n\n";
37
 
 
38
 
print "The base cell is:     Z7\n";
39
 
print "Increment the row:    ", inc_cell_row('Z7'), "\n";
40
 
print "Decrement the row:    ", dec_cell_row('Z7'), "\n";
41
 
print "Increment the column: ", inc_cell_col('Z7'), "\n";
42
 
print "Decrement the column: ", dec_cell_col('Z7'), "\n\n";
43
 
 
44
 
 
45
 
###############################################################################
46
 
#
47
 
# rowcol_to_cell($row, $col, $row_absolute, $col_absolute)
48
 
#
49
 
# Convert a zero based row and column reference to a A1 reference. For example
50
 
# (0, 2) to C1. $row_absolute, $col_absolute are optional. They are boolean
51
 
# values used to indicate if the row or column value is absolute, i.e. if it is
52
 
# prefixed by a $ sign: eg. (0, 2, 0, 1) converts to $C1.
53
 
#
54
 
# Returns: a cell reference string.
55
 
#
56
 
sub rowcol_to_cell {
57
 
 
58
 
    my $row     = $_[0];
59
 
    my $col     = $_[1];
60
 
    my $row_abs = $_[2] || 0;
61
 
    my $col_abs = $_[3] || 0;
62
 
 
63
 
 
64
 
    if ($row_abs) {
65
 
        $row_abs = '$'
66
 
    }
67
 
    else {
68
 
        $row_abs = ''
69
 
    }
70
 
 
71
 
    if ($col_abs) {
72
 
        $col_abs = '$'
73
 
    }
74
 
    else {
75
 
        $col_abs = ''
76
 
    }
77
 
 
78
 
 
79
 
    my $int  = int ($col / 26);
80
 
    my $frac = $col % 26 +1;
81
 
 
82
 
    my $chr1 ='';
83
 
    my $chr2 ='';
84
 
 
85
 
 
86
 
    if ($frac != 0) {
87
 
        $chr2 = chr (ord('A') + $frac -1);;
88
 
    }
89
 
 
90
 
    if ($int > 0) {
91
 
        $chr1 = chr (ord('A') + $int  -1);
92
 
    }
93
 
 
94
 
    $row++;     # Zero index to 1-index
95
 
 
96
 
    return $col_abs . $chr1 . $chr2 . $row_abs. $row;
97
 
}
98
 
 
99
 
 
100
 
###############################################################################
101
 
#
102
 
# cell_to_rowcol($cell_ref)
103
 
#
104
 
# Convert an Excel cell reference in A1 notation to a zero based row and column
105
 
# reference; converts C1 to (0, 2, 0, 0).
106
 
#
107
 
# Returns: row, column, row_is_absolute, column_is_absolute
108
 
#
109
 
#
110
 
sub cell_to_rowcol {
111
 
 
112
 
    my $cell = shift;
113
 
 
114
 
    $cell =~ /(\$?)([A-I]?[A-Z])(\$?)(\d+)/;
115
 
 
116
 
    my $col_abs = $1 eq "" ? 0 : 1;
117
 
    my $col     = $2;
118
 
    my $row_abs = $3 eq "" ? 0 : 1;
119
 
    my $row     = $4;
120
 
 
121
 
    # Convert base26 column string to number
122
 
    # All your Base are belong to us.
123
 
    my @chars  = split //, $col;
124
 
    my $expn   = 0;
125
 
    $col       = 0;
126
 
 
127
 
    while (@chars) {
128
 
        my $char = pop(@chars); # LS char first
129
 
        $col += (ord($char) -ord('A') +1) * (26**$expn);
130
 
        $expn++;
131
 
    }
132
 
 
133
 
    # Convert 1-index to zero-index
134
 
    $row--;
135
 
    $col--;
136
 
 
137
 
    return $row, $col, $row_abs, $col_abs;
138
 
}
139
 
 
140
 
 
141
 
###############################################################################
142
 
#
143
 
# inc_cell_row($cell_ref)
144
 
#
145
 
# Increments the row number of an Excel cell reference in A1 notation.
146
 
# For example C3 to C4
147
 
#
148
 
# Returns: a cell reference string.
149
 
#
150
 
sub inc_cell_row {
151
 
 
152
 
    my $cell = shift;
153
 
    my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
154
 
 
155
 
    $row++;
156
 
 
157
 
    return rowcol_to_cell($row, $col, $row_abs, $col_abs);
158
 
}
159
 
 
160
 
 
161
 
###############################################################################
162
 
#
163
 
# dec_cell_row($cell_ref)
164
 
#
165
 
# Decrements the row number of an Excel cell reference in A1 notation.
166
 
# For example C4 to C3
167
 
#
168
 
# Returns: a cell reference string.
169
 
#
170
 
sub dec_cell_row {
171
 
 
172
 
    my $cell = shift;
173
 
    my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
174
 
 
175
 
    $row--;
176
 
 
177
 
    return rowcol_to_cell($row, $col, $row_abs, $col_abs);
178
 
}
179
 
 
180
 
 
181
 
###############################################################################
182
 
#
183
 
# inc_cell_col($cell_ref)
184
 
#
185
 
# Increments the column number of an Excel cell reference in A1 notation.
186
 
# For example C3 to D3
187
 
#
188
 
# Returns: a cell reference string.
189
 
#
190
 
sub inc_cell_col {
191
 
 
192
 
    my $cell = shift;
193
 
    my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
194
 
 
195
 
    $col++;
196
 
 
197
 
    return rowcol_to_cell($row, $col, $row_abs, $col_abs);
198
 
}
199
 
 
200
 
 
201
 
###############################################################################
202
 
#
203
 
# dec_cell_col($cell_ref)
204
 
#
205
 
# Decrements the column number of an Excel cell reference in A1 notation.
206
 
# For example D3 to C3
207
 
#
208
 
# Returns: a cell reference string.
209
 
#
210
 
sub dec_cell_col {
211
 
 
212
 
    my $cell = shift;
213
 
    my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
214
 
 
215
 
    $col--;
216
 
 
217
 
    return rowcol_to_cell($row, $col, $row_abs, $col_abs);
218
 
}
219