将数据放入特定标头

发布于 2024-10-20 06:46:29 字数 944 浏览 1 评论 0原文

headers = ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"] 

data = ["05:01", "05:23", "05:43", "06:03", "06:33", "06:53", "07:03", "07:23", "07:46", "08:04", "08:24", "08:44", "09:14", "09:24", "09:44", "10:19", "10:39", "11:02", "11:22", "11:42", "12:12", "12:32", "12:52", "13:12", "13:32", "13:52", "14:12", "14:32", "14:42", "15:02", "15:32", "15:52", "16:12", "16:32", "16:52", "17:12", "17:32", "17:52", "18:12", "18:47", "19:23", "19:48", "20:28", "21:03", "21:33", "22:08", "22:40"] 

我必须将特定数据放入正确的标题中。例如

    05    06

    05:01 06:03
    05:23 06:33
    05:43 06:53

Rails 3.0.3
Ruby 1.9.2
Prawn 0.8.4

我怎样才能做到这一点?

基本上我想做这样的事情。

Prawn::Document.generate(path) do
  table([['05','06']] + [['05:01','06:03'],['05:23','06:33'],['05:43','06:53']], :header => true) do
  end
end
headers = ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"] 

data = ["05:01", "05:23", "05:43", "06:03", "06:33", "06:53", "07:03", "07:23", "07:46", "08:04", "08:24", "08:44", "09:14", "09:24", "09:44", "10:19", "10:39", "11:02", "11:22", "11:42", "12:12", "12:32", "12:52", "13:12", "13:32", "13:52", "14:12", "14:32", "14:42", "15:02", "15:32", "15:52", "16:12", "16:32", "16:52", "17:12", "17:32", "17:52", "18:12", "18:47", "19:23", "19:48", "20:28", "21:03", "21:33", "22:08", "22:40"] 

I must put specific data to correct header. For example

    05    06

    05:01 06:03
    05:23 06:33
    05:43 06:53

Rails 3.0.3
Ruby 1.9.2
Prawn 0.8.4

How I can do that ?

Basically i want do something like this.

Prawn::Document.generate(path) do
  table([['05','06']] + [['05:01','06:03'],['05:23','06:33'],['05:43','06:53']], :header => true) do
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-10-27 06:46:29
mapped_headers = headers.map do |header|
  data_array = data.select {|d| header == d[0,2]}
  [header, data_array]
end

此代码将给出如下输出:

[["05", ["05:01", "05:23", "05:43"]], ["06", ["06:03", "06:33", "06:53"]], ["07", ["07:03", "07:23", "07:46"]], ["08", ["08:04", "08:24", "08:44"]], ["09", ["09:14", "09:24", "09:44"]], ["10", ["10:19", "10:39"]], ["11", ["11:02", "11:22", "11:42"]], ["12", ["12:12", "12:32", "12:52"]], ["13", ["13:12", "13:32", "13:52"]], ["14", ["14:12", "14:32", "14:42"]], ["15", ["15:02", "15:32", "15:52"]], ["16", ["16:12", "16:32", "16:52"]], ["17", ["17:12", "17:32", "17:52"]], ["18", ["18:12", "18:47"]], ["19", ["19:23", "19:48"]], ["20", ["20:28"]], ["21", ["21:03", "21:33"]], ["22", ["22:08", "22:40"]]]

You can also create a hash by edit last line

{header => data_array}

你可以通过这样做来达到 Prawn 的要求

rows = 3
datas = (0...rows).map do |row|
  mapped_headers.map{|header| header[1][row]}
end
Prawn::Document.generate(path) do
  table([headers] + datas, :header => true) do
  end
end
mapped_headers = headers.map do |header|
  data_array = data.select {|d| header == d[0,2]}
  [header, data_array]
end

This code will give output like this:

[["05", ["05:01", "05:23", "05:43"]], ["06", ["06:03", "06:33", "06:53"]], ["07", ["07:03", "07:23", "07:46"]], ["08", ["08:04", "08:24", "08:44"]], ["09", ["09:14", "09:24", "09:44"]], ["10", ["10:19", "10:39"]], ["11", ["11:02", "11:22", "11:42"]], ["12", ["12:12", "12:32", "12:52"]], ["13", ["13:12", "13:32", "13:52"]], ["14", ["14:12", "14:32", "14:42"]], ["15", ["15:02", "15:32", "15:52"]], ["16", ["16:12", "16:32", "16:52"]], ["17", ["17:12", "17:32", "17:52"]], ["18", ["18:12", "18:47"]], ["19", ["19:23", "19:48"]], ["20", ["20:28"]], ["21", ["21:03", "21:33"]], ["22", ["22:08", "22:40"]]]

You can also create a hash by editing last line

{header => data_array}

You can acheive Prawn requirement by doing this

rows = 3
datas = (0...rows).map do |row|
  mapped_headers.map{|header| header[1][row]}
end
Prawn::Document.generate(path) do
  table([headers] + datas, :header => true) do
  end
end
注定孤独终老 2024-10-27 06:46:29

如果您询问如何向数组添加新元素,方法 push 将完成这项工作:

headers.push "05"
data.push "05:01"

If you are asking about how to add a new element to an array, method push will do the job:

headers.push "05"
data.push "05:01"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文