我不知道如何将哈希转换为多维数组。阅读 Excel 电子表格后,我从 roo 生成了以下哈希:
{[1, 1]=>"string-1", [1, 2]=>"string-2", [1, 3]=>"string-3", [1, 4]=>"string-4",
[1, 5]=>"string-5", [1, 6]=>"string-6", [1, 7]=>"string-7", [2, 1]=>"string-1",
[2, 2]=>"string-2", [2, 3]=>numeric-1, [2, 4]=>numeric-2, [2, 5]=>"string-3",
[2, 6]=>"string-4", [2, 7]=>numeric-3, [3, 1]=>"string-1", [3, 2]=>"string-2",
[3, 3]=>numeric-1, [3, 4]=>numeric-2, [3, 5]=>"string-3", [3, 6]=>"string-4",
[3, 7]=>numeric-3, ... etc}
我需要将其转换为:
[["string-1", "string-2", "string-3", "string-4", "string-5", "string-6", "string-7"],
["string-1", "string-2", numeric-1, numeric-2, "string-3", "string-4", numeric-3],
["string-1", "string-2", numeric-1, numeric-2, "string-3", "string-4", numeric-3],
... etc]
我尝试了以下方法,但它只是将所有内容复制到一个数组中,而不是嵌入数组:
2.upto(input.last_row).each do |row|
((input.first_column)..(input.last_column)).map{ |col|
$rows << input.cell(row, col) if col != 5 and col <= 10}.join(" ")
end
我已经搜索了几个小时的答案,但找不到解决方案。
以下最终成为我可行的解决方案:
((xlsx.first_row)..(xlsx.last_row)).each do |row|
((xlsx.first_column)..(xlsx.last_column)).each do |col|
$tmp_row << xlsx.cell(row, col) if col != 5 and col <= 10
end
remove_newlines_from_strings($tmp_row)
$rows_sheet_0 << $tmp_row
$tmp_row = []
end