-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcell.ado
86 lines (84 loc) · 2.33 KB
/
xcell.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
capture program drop xcell
program xcell, rclass
// --- Provide row and col information on an Excel cell
// Optionally offset by 'right' columns and 'down' rows
// If no cell is provided, it takes r(cell) from the previous call.
// Optionally provide a global macro name to store the cell, e.g.
// xcell my_cell = A3 -- stores A3 in $my_cell
version 13.1
syntax [anything(equalok)], [right(integer 0)] [down(integer 0)] [row(integer 0)] ///
[COLumn(integer 0)] [local(name local)]
local anything = subinstr(`"`anything'"', `"="', `" "', .)
local words: word count `anything'
if `words' == 0 {
local xcell = r(xcell)
}
else if `words' == 1 {
if regexm(`"`anything'"', "^[A-Za-z][A-Za-z]?[A-Za-z]?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?$") {
local xcell = upper(`"`anything'"')
}
else {
local global `anything'
local xcell $`global'
}
}
else if `words' == 2 {
local global: word 1 of `anything'
local xcell: word 2 of `anything'
}
else {
display "Syntax is xcell [global] [cell ref], ..."
error 198
}
if (`row' & `down') | (`column' & `right') {
display "Cell was not specified correctly"
error 198
}
local cell_column 0
*display "Cell `xcell'"
forvalues i = 1 / `= length(`"`xcell'"')' {
local ch = substr(`"`xcell'"', `i', 1)
*display `"Find `ch' in `c(ALPHA)'"'
quietly findtoken `ch', in(`c(ALPHA)')
if r(P) > 0 {
local cell_column = `cell_column' * 26 + r(P)
}
else {
local n = substr(`"`xcell'"', `i', length(`"`xcell'"') - `i' + 1)
local cell_row = real("`n'")
continue, break
}
}
if `column' {
local new_column = `column'
}
else {
local new_column = `cell_column' + `right'
}
if `row' {
local new_row = `row'
}
else {
local new_row = `cell_row' + `down'
}
local c `new_column'
local letters
while `c' > 0 {
local r = mod(`c' - 1, 26) + 1
local c = int((`c' - `r') / 26)
local letter: word `r' of `c(ALPHA)'
local letters `letter'`letters'
}
display "From cell `xcell' (column `cell_column', row `cell_row')," ///
"right `right', down `down' => `letters'`new_row' (column `new_column', row `new_row')"
return scalar row = `new_row'
return scalar column = `new_column'
local new_cell `letters'`new_row'
return local xcell = `"`letters'`new_row'"'
if `"`local'"' != `""' {
c_local `local' `new_cell'
}
if `"`global'"' != `""' {
global `global' `new_cell'
}
end