简体   繁体   English

使用excel将结果记录在ruby watir中。 如何使用puts保持不同单元格中的值

[英]Using excel to log results in ruby watir. how to keep values in different cells using puts

I am new to ruby watir and need your help. 我是ruby watir的新手,需要你的帮助。

I am using the following commands to log my script results into an excel sheet. 我使用以下命令将我的脚本结果记录到Excel工作表中。

File.open('c:\log.txt', 'w') do |file|
  file.puts("TEST PASSED" + "#{Time.now}")
end

Here the test passed and the time is getting displayed in a single cell itself. 这里测试通过,时间显示在单个单元格中。

I want to display both of it in different cells. 我想在不同的单元格中显示它们。

Please suggest a solution. 请提出解决方案。 Thanks in advance! 提前致谢!

The easiest solution would probably be to log the results in a CSV (comma separated values) file. 最简单的解决方案可能是将结果记录在CSV(逗号分隔值)文件中。 These can be written like a text file, as well as read by Excel as a grid. 这些可以像文本文件一样编写,也可以作为网格读取。

For a CSV file, each line represents a row in the table. 对于CSV文件,每行代表表中的一行。 Within each line, the column values (or cells) are separated by commas. 在每行中,列值(或单元格)用逗号分隔。 For example, the following will create 2 rows with 3 columns: 例如,以下将创建包含3列的2行:

a1, b1, c1
a2, b2, c2

For your logging, you could: 对于您的日志记录,您可以:

  • Create a log.csv instead of log.txt 创建log.csv而不是log.txt
  • Output the values as comma separated 以逗号分隔输出值

The code would be: 代码是:

File.open('c:\log.csv', 'w') do |file|
  file.puts("TEST PASSED, #{Time.now}")
end

you are logging to a file called log.txt which appears to be a plain text file. 您正在登录名为log.txt的文件,该文件似乎是纯文本文件。 if you want your file to be an excel file you will need a format, the easiest one to write to is either .csv or .tsv which stands for comma separated variable and tab separated variables. 如果您希望您的文件是excel文件,则需要格式,最容易写入的文件是.csv.tsv ,它代表逗号分隔变量和制表符分隔变量。 You could then write in a few different ways. 然后你可以用几种不同的方式写作。 You could write as you were with: 你可以像以前一样写:

File.open('c:\log.tsv', 'w') do |file|
  file.puts("TEST PASSED\t" + "#{Time.now}")
end

for a tsv (note that it doesn't need to be called .tsv) 对于ts​​v(请注意,它不需要被称为.tsv)

File.open('c:\log.csv', 'w') do |file|
  file.puts("TEST PASSED," + "#{Time.now}")
end

for a csv 对于csv

or you could use the standard csv library . 或者你可以使用标准的csv库 like so: 像这样:

CSV.open("c:\log.csv", "wb") do |csv|
  csv << ["TEST PASSED", "#{Time.now}"]
end

which you can manipulate for tsv's: 你可以操纵tsv的:

CSV.open("c:\log.csv", "wb", { :col_sep => "\t" }) do |csv|
  csv << ["TEST PASSED", "#{Time.now}"]
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM