[英]stacking pairs of columns with ids from rows of data
Hi I'm trying to sort products and their attributes but the problem is that the headers for the attributes are not based on the name and description of the product. 嗨,我正在尝试对产品及其属性进行排序,但问题是属性的标头不是基于产品的名称和描述。 So for example I have: 所以我举例说:
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
This row goes on until however many attributes there are for the product. 这一行继续进行,直到该产品有许多属性。
What I need is something that would spit out: 我需要的东西会吐出来:
Product id, attribute 1 name, attribute 1 desc
Product id, attribute 2 name, attribute 2 desc
So it would look like this: 所以它看起来像这样:
1001, screen size, 15"
1001, DCR, 10,000:1
1002, DCR, 200,000:1
1002, widescreen, yes
Does anyone know what would be the best way to sort this information? 有谁知道对这些信息进行排序的最佳方法是什么?
I've been trying with a bit of excel vba scripts but I was wondering if there was a way of doing it with ruby since that's what I'm learning right now and it would be a good real world example to delve deeper into ruby. 我一直在尝试使用一些excel vba脚本,但我想知道是否有一种方法可以用ruby来实现它,因为这就是我现在正在学习的东西,这将是一个很好的真实世界的例子来深入研究ruby。
You can simplify this process greatly by separating the attributes into their own model. 通过将属性分离到自己的模型中,可以大大简化此过程。
app/models/product_attribute.rb 应用程序/模型/ product_attribute.rb
class ProductAttribute < ActiveRecord::Base
attr_accessible :name, :desc, :product_id
belongs_to :product
#...
end
app/models/product.rb 应用程序/模型/ 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
You would then be able to get the information you require by calling Product.sorted_attributes and writing view code to display the resulting 2D array. 然后,您可以通过调用Product.sorted_attributes并编写视图代码来显示生成的2D数组,从而获得所需的信息。
Here is a fleshed out version of what John Bustos mentioned in the comments. 这是John Bustos在评论中提到的充实版本。
I'm working with this sample data ( full workbook here ) 我正在处理这个示例数据( 这里是完整的工作簿 )
The idea is to use VBA to loop through pairs of columns, and output them in a long skinny table. 我们的想法是使用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
Hope that helps! 希望有所帮助!
Thanks for the help. 谢谢您的帮助。 I actually figured it out a while ago. 我实际上已经知道了。 I just made some slight adjustments to the lineemup macro 我只是对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
Where 6500 represents the number of rows in the dataset. 其中6500表示数据集中的行数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.