【问题标题】:Transform Integer Cell Range into Excel Cell Range将整数单元格范围转换为 Excel 单元格范围
【发布时间】:2016-02-29 12:27:10
【问题描述】:

我在 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?

【问题讨论】:

  • 您在寻找 Excel 或 Ruby 中的解决方案吗?

标签: arrays ruby excel cells


【解决方案1】:

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

这会返回一个过程。

使用excel_column_ref[26] = 'AA'

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

    • 抱歉,我认为我不够清楚,我正在寻找 Ruby 解决方案。
    • 没问题,我已经完成了,因为我经常在 Excel 中使用它!如果你没问题,我会在这里回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多