Loop Excel columns - ruby
The solution below solves my problem when there is a vertical column that validates the record in the cell with the field on the application screen.
But if the spreadsheet is horizontal, I cannot read the records and validate them on the application screen.
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open('c:/temp/gab.xls', "r")
sheet = book.worksheet 4
xls_offset = 1
row_offset = 2
sheet.each_with_index(xls_offset) do |row, idx|
expected_tooltip = row[1]
break if row[1].nil?
expect(page).to have_css("tr.tvRow:nth-child(#{row_offset+idx}) .tvCell:nth-child(6) img[tooltip='#{expected_tooltip}']")
end
OK: I get the values from the status column [invisible....etc]
fields
status
Leitura
Invisível
Hidrometro
Invisíve
Diametro
Invisível
Lacre
Obrigatório
NOK
code
situation
tab
leitura
hidrometro
diametro
lacre
8
ok
B
Invisivel
Invisivel
Habilitado
Invisivel
9
ok
C
Invisivel
Invisivel
Habilitado
Invisivel
10
ok
C
Invisivel
Invisivel
Habilitado
Invisivel
How to adjust the code above to do this reading horizontally to read the records [Invisivel, Invisivel,Habilitado,Invisivel] in line 3 from the Leitura column?
You can try getting a specific row and then iterating over cells in this row skipping the first N cells:
row = sheet.row(2) # get the third row
row.each_with_index do |cell, index|
next if index < 3 # skip first three cells
# do something with the data specified in cell variable
end
Related
Loop in excel column with Ruby horizontally. No one will help me?
Friends, the solution below solves my problem when there is a vertical column that validates the record in the cell with the field on the application screen. But if the spreadsheet is horizontally, I am not able to read the records and validate on the application screen. here OK I get the values from the status column [invisible....etc] fields status Leitura Invisível Hidrometro Invisíve Diametro Invisível Lacre Obrigatório Spreadsheet.client_encoding = 'UTF-8' book = Spreadsheet.open('c:/temp/gab.xls', "r") sheet = book.worksheet 4 xls_offset = 1 row_offset = 2 sheet.each_with_index(xls_offset) do |row, idx| expected_tooltip = row[1] break if row[1].nil? expect(page).to have_css("tr.tvRow:nth-child(#{row_offset+idx}) .tvCell:nth-child(6) img[tooltip='#{expected_tooltip}']") end end How to adjust the code above to do this reading horizontally to read the records [Invisivel, Invisivel,Habilitado,Invisivel] in line 3 from the Leitura column? Here NOK code situation tab leitura hidrometro diametro lacre 8 ok B Invisivel Invisivel Habilitado Invisivel 9 ok C Invisivel Invisivel Habilitado Invisivel 10 ok C Invisivel Invisivel Habilitado Invisivel enter image description here
Remove nTh record from array using loop [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 6 years ago. Improve this question I'm writing a program that reads a .csv file, and then loops through it removing every 10th record it encounters before outputting it. I've been stuck on what I believe is a syntax issue for a while now and just can't seem to nail it. Anyone mind having a look? lines = [] i = 0 elements = [] element2 = [] output = [] file = File.open("./properties.csv", "r") while (line = file.gets) i += 1 # use split to break array up using commas arr = line.split(',') elements.push({ id: arr[0], streetAddress: arr[1], town: arr[2], valuationDate: arr[3], value: arr[4] }) end file.close # filter out blanks and nill rows x = elements.select { |elements| elements[:id].to_i >= 0.1} # Loop to remove every 10th record e = 0 d = 1 loop do x.length if e == (10 * d) d ++ e ++ else x = elements.select[e] e ++ end puts x puts "#{x.length} house in list, #{d} records skipped." CSV FILE ID,Street address,Town,Valuation date,Value 1,1 Northburn RD,WANAKA,1/1/2015,280000 2,1 Mount Ida PL,WANAKA,1/1/2015,280000 3,1 Mount Linton AVE,WANAKA,1/1/2015,780000 4,1 Kamahi ST,WANAKA,1/1/2015,155000 5,1 Kapuka LANE,WANAKA,1/1/2015,149000 6,1 Mohua MEWS,WANAKA,1/1/2015,560000 7,1 Kakapo CT,WANAKA,1/1/2015,430000 8,1 Mt Gold PL,WANAKA,1/1/2015,1260000 9,1 Penrith Park DR,WANAKA,1/1/2015,1250000 10,1 ATHERTON PL,WANAKA,1/1/2015,650000 11,1 WAIMANA PL,WANAKA,1/1/2015,780000 12,1 ROTO PL,WANAKA,1/1/2015,1470000 13,1 Toms WAY,WANAKA,1/1/2015,2230000 14,1 MULBERRY LANE,WANAKA,1/1/2015,415000 15,1 Range View PL,WANAKA,1/1/2015,300000 16,1 Clearview ST,WANAKA,1/1/2015,1230000 17,1 Clutha PL,WANAKA,1/1/2015,700000 18,1 Centre CRES,WANAKA,1/1/2015,295000 19,1 Valley CRES,WANAKA,1/1/2015,790000 20,1 Edgewood PL,WANAKA,1/1/2015,365000 21,1 HUNTER CRES,WANAKA,1/1/2015,335000 22,1 KOWHAI DR,WANAKA,1/1/2015,480000 23,1 RIMU LANE,WANAKA,1/1/2015,465000 24,1 CHERRY CT,WANAKA,1/1/2015,495000 25,1 COLLINS ST,WANAKA,1/1/2015,520000 26,1 AUBREY RD,WANAKA,1/1/2015,985000 27,1 EELY POINT RD,WANAKA,1/1/2015,560000 28,1 LINDSAY PL,WANAKA,1/1/2015,385000 29,1 WINDERS ST,WANAKA,1/1/2015,760000 30,1 Manuka CRES,WANAKA,1/1/2015,510000 31,1 WILEY RD,WANAKA,1/1/2015,420000 32,1 Baker GR,WANAKA,1/1/2015,820000 33,1 Briar Bank DR,WANAKA,1/1/2015,1260000 34,1 LAKESIDE RD,WANAKA,1/1/2015,440000 35,1 PLANTATION RD,WANAKA,1/1/2015,345000 36,1 Allenby PL,WANAKA,1/1/2015,640000 37,1 ROB ROY LANE,WANAKA,1/1/2015,380000 38,1 Ansted PL,WANAKA,1/1/2015,590000 39,1 Fastness CRES,WANAKA,1/1/2015,640000 40,1 APOLLO PL,WANAKA,1/1/2015,385000 41,1 AEOLUS PL,WANAKA,1/1/2015,370000 42,1 Peak View RDGE,WANAKA,1/1/2015,1750000 43,1 Moncrieff PL,WANAKA,1/1/2015,530000 44,1 Islington PL,WANAKA,1/1/2015,190000 45,1 Hidden Hills DR,WANAKA,1/1/2015,1280000 46,1 Weatherall CL,WANAKA,1/1/2015,425000 47,1 Terranova PL,WANAKA,1/1/2015,900000 48,1 Cliff Wilson ST,WANAKA,1/1/2015,1200000 49,1 TOTARA TCE,WANAKA,1/1/2015,460000 50,1 Koru WAY,WANAKA,1/1/2015,570000 51,1 Bovett PL,Wanaka,1/1/2015,495000 52,1 Pearce PL,Wanaka,1/1/2015,675000 53,1 Ironside DR,WANAKA,1/1/2015,570000 54,1 Bob Lee PL,WANAKA,1/1/2015,610000 55,1 Hogan LANE,WANAKA,1/1/2015,395000 56,1 ARDMORE ST,WANAKA,1/1/2015,1190000 57,1 Bullock Creek LANE,WANAKA,1/1/2015,11125000 58,1 DUNMORE ST,WANAKA,1/1/2015,1300000 59,1 Primary LANE,WANAKA,1/1/2015,430000 60,1 SYCAMORE PL,WANAKA,1/1/2015,720000 61,1 FAULKS TCE,WANAKA,1/1/2015,780000 62,1 Alpha CL,WANAKA,1/1/2015,500000 63,1 Coromandel ST,WANAKA,1/1/2015,530000 64,1 Niger ST,WANAKA,1/1/2015,475000 65,1 Maggies Way,WANAKA,1/1/2015,375000 66,1 Hollyhock LANE,QUEENSTOWN,1/1/2015,1080000 67,1 ELDERBERRY CRES,WANAKA,1/1/2015,1340000 68,1 Foxglove HTS,WANAKA,1/1/2015,2520000 69,1 MEADOWSTONE DR,WANAKA,1/1/2015,650000 70,1 OAKWOOD PL,WANAKA,1/1/2015,580000 71,1 MEADOWBROOK PL,WANAKA,1/1/2015,645000 72,1 Jessies CRES,WANAKA,1/1/2015,320000 73,1 Lansdown ST,WANAKA,1/1/2015,700000 74,1 Stonebrook DR,WANAKA,1/1/2015,640000 75,1 Hyland ST,WANAKA,1/1/2015,500000 76,1 TAPLEY PADDOCK,WANAKA,1/1/2015,720000 77,1 Homestead CL,WANAKA,1/1/2015,1750000 78,1 NORMAN TCE,WANAKA,1/1/2015,620000 79,1 Sunrise Bay DR,WANAKA,1/1/2015,3000000 80,1 LARCH PL,WANAKA,1/1/2015,570000 81,1 MILL END,WANAKA,1/1/2015,600000 82,1 Bills WAY,WANAKA,1/1/2015,750000 83,1 Heuchan LANE,WANAKA,1/1/2015,610000 84,1 SARGOOD DR,WANAKA,1/1/2015,455000 85,1 Frederick ST,WANAKA,1/1/2015,455000 86,1 Connell TCE,WANAKA,1/1/2015,600000 87,1 Soho ST,QUEENSTOWN,1/1/2015,320000 88,1 Hikuwai DR,ALBERT TOWN,1/1/2015,280000 89,1 Harrier LANE,WANAKA,1/1/2015,1000000 90,1 Ewing PL,WANAKA,1/1/2015,780000 91,1 Sherwin AVE,ALBERT TOWN,1/1/2015,440000 92,1 Hardie PL,WANAKA,1/1/2015,830000 93,1 Finch ST,ALBERT TOWN,1/1/2015,540000 94,1 Poppy LANE,ALBERT TOWN,1/1/2015,395000 95,1 Warbler LANE,ALBERT TOWN,1/1/2015,410000 96,1 Balneaves LANE,WANAKA,1/1/2015,250000 97,1 Mill Green,Arrowtown,1/1/2015,800000
require 'csv' elements = {} CSV.foreach("properties.csv", :headers => true, :header_converters => :symbol) do |row| elements[row.fields[0]] = Hash[row.headers[1..-1].zip(row.fields[1..-1])] end d = 0 e = 0 elements.delete_if do |key, value| e += 1 if e == 10 e = 0 d += 1 end e == 0 end puts "#{elements.length} house in list, #{d} records skipped." At the end of this, elements will have every 10th row removed, and d contains the number of rows removed.
Ruby - How to subtract numbers of two files and save the result in one of them on a specified position?
I have 2 txt files with different strings and numbers in them splitted with ; Now I need to subtract the ((number on position 2 in file1) - (number on position 25 in file2)) = result Now I want to replace the (number on position 2 in file1) with the result. I tried my code below but it only appends the number in the end of the file and its not the result of the calculation which got appended. def calc f1 = File.open("./file1.txt", File::RDWR) f2 = File.open("./file2.txt", File::RDWR) f1.flock(File::LOCK_EX) f2.flock(File::LOCK_EX) f1.each.zip(f2.each).each do |line, line2| bg = line.split(";").compact.collect(&:strip) bd = line2.split(";").compact.collect(&:strip) n = bd[2].to_i - bg[25].to_i f2.print bd[2] << n #puts "#{n}" Only for testing end f1.flock(File::LOCK_UN) f2.flock(File::LOCK_UN) f1.close && f2.close end
Use something like this: lines1 = File.readlines('file1.txt').map(&:to_i) lines2 = File.readlines('file2.txt').map(&:to_i) result = lines1.zip(lines2).map do |value1, value2| value1 - value2 } File.write('file1.txt', result.join(?\n)) This code load all files in memory, then calculate result and write it to first file. FYI: If you want to use your code just save result to other file (i.e. result.txt) and at the end copy it to original file.
String search with in the excel column values row wise
I have an Excel sheet which has 200 columns. Now number of rows are 3500. So i have to search a string if it presents within any column for each row. Now to make the process fatser,I am looking for any alternate instead of Looping technique. Is there any such? IntRow6 = 2 DataCount = 0 Do While objSheet6.Cells(IntRow6,1).Value <> "" For DataCount = 0 to UBound(VMHArray) IntClmn3 = 1 Do While 1 = 1 If objSheet6.Cells(IntRow6,IntClmn3).Value = VMHArray(DataCount) Then objSheet6.Cells(IntRow6,IntClmn3+2).Value=objSheet6.Cells(IntRow6,IntClmn3+5).Value Exit Do End If IntClmn3 = IntClmn3 + 1 Loop Next IntRow6 = IntRow6 + 1 Loop The above is taking to much time, thus i am looking for an equivalent VBScript code which can run more faster search. EDIT: ParentColmnCount=ParentColmnCount-1 IntRow6=2 DataCount=0 Do While objSheet6.Cells(IntRow6,1).Value <> "" For DataCount=0 to UBound(VMHArray) If objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCount)).Find(VMHArray(DataCount)) <> Nothing Then MsgBox("Hi") End If Next IntRow6=IntRow6+1 Loop I'm getting any error saying that, "Object variable not set" error at the Range line of the above code. UPDATE I have updated my code as per your suggestion,and modified the declaration of variables as below: Option Explicit Dim objExcel1,objWorkbook Dim strPathExcel1 Dim objSheet6,objSheet3 Dim IntRow6,IntRow3 Dim IntClmn3 Dim DataCount,ParentColmnCount Dim Falg Dim TaskCounter Dim r As Range Dim s As Variant But I am getting the error again: Expected end of statement" in the line "Dim r As Range"
Check out Range.Find (which returns a Range object). It's faster than a loop. Example: Sub Test() Dim r As Range Dim matched As Range 'Get the second row Set r = Sheet1.Range("2:2") Set matched = r.Find("myString") 'Do stuff with matched End Sub UDPATE: Assuming you are using CreateObject("Excel.Application"), then the Range object will be present (as CreateObject will return an instance of Excel which contains Range objects and functionality). For further proof, you're already looping through the worksheet using Range objects (Cells is a Range object). UPDATE: Here's a more advanced example. Assume the following data in on Sheet1: fred ethel ricky bobby 1 2 3 4 lucy myrtle fonzy rickie 1 2 3 4 joanie chachie donna patty 1 2 3 4 selma homer lisa bart 1 2 3 4 You can loop through the ranges like so: Sub test() Dim r As Range Dim names(3) As String Dim s As Variant names(0) = "ethel" names(1) = "fonzy" names(2) = "patty" names(3) = "selma" For Each s In names For Each r In Sheet1.Cells.CurrentRegion.Find(s) r.Offset(0, 4).Value = r.Offset(0, 4).Value + 1000 Next r Next s End Sub After the routine is done, the data is now: fred ethel ricky bobby 1 1002 3 4 lucy myrtle fonzy rickie 1 2 1003 4 joanie chachie donna patty 1 2 3 1004 selma homer lisa bart 1001 2 3 4 UPDATE: I just saw your comment on your question (and edited your question to include the updated code). You're getting "Object variable not set" on that line because you cannot use <> comparisons with objects. Change the line to read: If Not objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCount)).Find(VMHArray(DataCount)) Is Nothing Then
ruby multiple loop sets but with limited rows per set
Alrightie, so I'm building an CSV file this time with ruby. The outer loop will run up to length of num_of_loops, but it runs for an entire set rather than up to the specified row. I want to change the first column of a CSV file to a new name for each row. If I do this: class_days = %w[Wednesday Thursday Friday] num_of_loops = (num_of_loops / class_days.size).ceil num_of_loops.times { ["Wednesday","Thursday","Friday"].each do |x| data[0] = x data[4] = classname() # Write all to file # csv << data end } Then the loop will run only 3 times for a 5 row request. I'd like it to run the full 5 rows such that instead of stopping at Wed/Thurs/Fri it goes to Wed/Thurs/Fri/Wed/Thurs instead.
class_days = %w[Wednesday Thursday Friday] num_of_loops.times do |i| data[0] = class_days[i % class_days.size] data[4] = classname csv << data end The interesting part is here: class_days[i % class_days.size] We need an index into class_days that is between 0 and class_days.size - 1. We can get that with the % (modulo) operator. That operator yields the remainder after dividing i by class_days.size. This table shows how it works: i i % 3 0 0 1 1 2 2 3 0 4 1 5 2 ... The other key part is that the times method yields indices starting with 0.