3

こんにちは私は製品とその属性を並べ替えようとしていますが、問題は属性のヘッダーが製品の名前と説明に基づいていないことです。たとえば、私は次のようにしています。

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スクリプトを試してきましたが、それが今私が学んでいることであり、ルビーをより深く掘り下げるのは良い実例になるので、ルビーでそれを行う方法があるかどうか疑問に思いました。

4

3 に答える 3

0

これは、John Bustos がコメントで言及した内容の具体化されたバージョンです。

このサンプル データを使用しています (完全なワークブックはこちら)

サンプルデータ

アイデアは、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

それが役立つことを願っています!

于 2013-06-20T00:27:35.873 に答える
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 は、データセット内の行数を表します。

于 2013-06-21T00:57:52.303 に答える
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 を呼び出し、ビュー コードを記述して結果の 2D 配列を表示することで、必要な情報を取得できます。

于 2012-11-30T19:54:06.560 に答える