将整数单元格范围转换为Excel单元格范围

时间:2015-11-26 11:57:43

标签: arrays ruby excel cells

我在Ruby / Rails中编码,我有一个单元格范围,例如:

[0,1]..[0,3] with the convention [row, column]..[row..column]

Excel使用单元格区域,如:

A2:A4 with the convention <column><row>:<column><row>

我需要从前者转换为后者。 主要是我关注的那封信:

A => 0,
B => 1,
C => 2,
AA => 26
AB => 27

我将如何做到这一点?

我基本上需要做相反的事情:

Generate letters to represent number using ruby?

2 个答案:

答案 0 :(得分:0)

VBA解决方案

Sub test_JohnLinux()

MsgBox ColLet(4) & vbCrLf & InvColLet("D")

End Sub

从信件中获取数字:

Public Function InvColLet(x As String) As Long
ActiveSheet.Cells(1, x).EntireColumn.Column
End Function

从数字中获取信件:

Public Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
    ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function

答案 1 :(得分:0)

Ruby解决方案

归功于霍华德,我从这里调整了答案:https://codegolf.stackexchange.com/questions/3971/generate-excel-column-name-from-index

excel_column_ref =-> array_column_ref do
  ref = "A"
  array_column_ref.times{ ref.next! }
  ref
end

这会返回一个proc。

用法excel_column_ref[26] = 'AA'