2012-11-30 27 views
3

嗨我试图对产品及其属性进行排序,但问题是属性的标题并不基于产品的名称和说明。因此,例如我有:从数据行中堆积具有ID的列对

Product id | attribute 1 name | attribute 1 desc | attribute 2 name | attribute 2 desc 
1001  | screen size  | 15"    | DCR    | 10,000:1 
1002  | DCR    | 200,000:1  | Widescreen  | yes 

此行继续,直到产品的许多属性。

我需要的东西,就吐出:

Product id, attribute 1 name, attribute 1 desc 
Product id, attribute 2 name, attribute 2 desc 

因此,这将是这样的:

1001, screen size, 15" 
1001, DCR, 10,000:1 
1002, DCR, 200,000:1 
1002, widescreen, yes 

有谁知道什么是排序此信息的最佳方式?

我一直在尝试一些excel vba脚本,但我想知道是否有办法用ruby做到这一点,因为这是我现在正在学习的东西,这将是一个很好的现实世界的例子深入红宝石。

+0

这是真的吗? “属性1名称,属性1 desc”还是属性名称/描述更改? –

+0

我对Ruby一无所知,所以我无法帮到你,但我这样做的方式是,既然你知道你会一直在工作2列(1为名字,1为价值),我会创建一个宏来循环 - 一次跳2列 - 抓住数据并吐出来...相当容易循环。 –

+0

你能给我一个这个循环看起来是什么样子的例子吗? – dchun

回答

0

您可以通过将属性分隔到自己的模型中来大大简化此过程。

应用程序/模型/ product_attribute.rb

class ProductAttribute < ActiveRecord::Base 
    attr_accessible :name, :desc, :product_id 
    belongs_to :product 
    #... 
end 

应用程序/模型/ product.rb

class Product < ActiveRecord::Base 
    # ... 
    has_many :product_attributes 
    # ... 

    def self.sorted_attributes 
    Product.all.collect{|prod| prod.sorted_attributes} 
    end  

    def sorted_attributes 
    product_attributes.all(order: "name ASC").collect{|attr| [self.id, attr.name, attr]} 
    end 
end 

然后你就可以通过调用Product.sorted_attributes和写作得到你所需要的信息查看代码以显示生成的二维数组。

+0

这是我的头。通过#...你是指插入csv行数据? – dchun

+0

#...只是一个占位符。你可以把任何你想要的东西放在课堂上,我把## 主要观点是,通过使用关联控制属性而不是将它们保存为产品模型的一部分,您可以节省很多麻烦 – matt

0

下面是John Bustos在评论中提到的一个充实的版本。

我与这个样本数据(full workbook here

SampleData

的想法是通过对列使用VBA来循环工作,并在相当长的瘦表它们输出。

Sub MakeSkinny() 
    Dim rng As Range 
    Dim clOutput As Range 
    Dim cl As Range 

    Set rng = Range("A3").CurrentRegion ' raw data' 
    Set clOutput = Range("A9") ' Where to output results' 

    Set cl = clOutput 
    ' Add headers to the new table' 
    cl.Offset(0, 0).Value = "Item" 
    cl.Offset(0, 1).Value = "Attribute" 
    cl.Offset(0, 2).Value = "Value" 
    Set cl = cl.Offset(1, 0) ' go to the next row of output' 

    For i = 2 To rng.Rows.Count 
     iCol = 2 ' Start at column 2' 
     Do Until iCol >= 7 ' set to however many cols you have' 
      'Check for blank attribute name' 
      If rng.Cells(i, iCol) <> "" Then 
       cl.Offset(0, 0) = rng.Cells(i, 1) ' Item ID' 
       cl.Offset(0, 1) = rng.Cells(i, iCol) ' Attribute Name' 
       cl.Offset(0, 2) = rng.Cells(i, iCol + 1) ' Attribute Value' 
       Set cl = cl.Offset(1, 0) ' go to the next row of output' 
      End If 
      iCol = iCol + 2 'Advance to next set of attributes' 
     Loop 
    Next i 
End Sub 

希望有所帮助!

0

感谢您的帮助。我其实早就知道了。我只是对lineemup宏进行了一些微小的调整

Sub lineemupSAS() 

Dim i As Long 

Dim dr As Long 

For i = 2 To Cells(2, Columns.Count).End(xlToLeft).Column Step 2 

dr = Cells(Rows.Count, 1).End(xlUp).Row + 1 

Cells(2, 1).Resize(6500).Copy Cells(dr, 1) 

Cells(2, i).Resize(6500).Copy Cells(dr, 2) 

Cells(2, 1 + i).Resize(6500).Copy Cells(dr, 3) 

Next i 

End Sub 

其中6500代表数据集中的行数。