1

我正在学习 Ruby 并尝试操作 Excel 数据。

我的目标:

为了能够从 excel 文件中提取电子邮件地址并将它们放在一个文本文件中,每行一个,并在末尾添加一个逗号。

我的想法:

我认为我的答案在于使用电子表格和 File.new。

我要找的是方向。我想听听任何提示或提示来实现我的目标。谢谢

请不要发布仅寻找方向的确切代码想自己弄清楚...

谢谢,凯伦

更新::

因此,正则表达式似乎能够找到所有匹配的字符串并将它们存储到一个数组中。我在设置它时遇到了一些麻烦,但应该能够弄清楚......但现在开始我将只提取标有“电子邮件”的列......我现在的问题是:

`parse_csv = CSV.parse(read_csv, :headers => true)`

:skip_blanks 的默认值设置为 false .. 我需要将其设置为 true 但我找不到正确的语法来这样做...我假设类似

`parse_csv = CSV.parse(read_csv, :headers => true :skip_blanks => true)`

但不是.....

4

3 回答 3

1

将您的 excel 文件保存为 csv(逗号分隔值)并使用Ruby 的库

于 2013-03-04T16:40:47.320 回答
0

除此之外spreadsheet(可以读写),你可以用 . 读取 Excel 和其他文件类型RemoteTable

gem install remote_table

require 'remote_table'
t = RemoteTable.new('/path/to/file.xlsx', headers: :first_row)

当您编写 CSV 时,正如@aug2uag 所说,您可以使用 ruby​​ 的标准库(无需安装 gem):

require 'csv'
puts [name, email].to_csv
于 2013-03-04T17:38:14.217 回答
0

就个人而言,我会尽可能简单并使用CSV.

这是一些如何工作的伪代码:

read in your file line by line
extract your fields using regex, or cell count (depending on how consistent the email address location is), and insert into an arry
iterate through the array and write the values in the fashion you wish (to console, or file)

您拥有的评论中的代码是一个很好的开始,但是,puts只会写入控制台,而不是文件。您还需要弄清楚您将如何知道您正在获取电子邮件地址。

希望这可以帮助。

于 2013-03-04T21:22:17.087 回答