Dynamic data in Cucumber tables - cucumber

I have a Cucumber table, one of the fields is a date which I would like to have populated with todays date.
Is there a way of doing this without having to hard code todays date into the table?
Basically I would like to enter Time.now.strftime("%Y-%m-%d") into the table and not have it break.

Since the table is being processed by your step definition, you could put a special place holder in the table, such as the string "TODAYS_DATE", and then use map_column! to process the data in the column to the format you want.
For example given the following table
Given the following user records
| username | date |
| alice | 2001-01-01 |
| bob | TODAYS_DATE |
In your step definition you would have
Given /^the following user records$/ do |table|
table.map_column!('date') do |date|
if date == 'TODAYS_DATE'
date = Time.now.strftime("%Y-%m-%d")
end
date
end
table.hashes.each do |hash|
#Whatever you need to do
end
end
Note this only changes the values when you ask for the hash. table and table.raw will remain the same, but whenever you need the row hashes, they will be converted by the code within the map_column!

I know it's been ages since this question was asked but I was doing something similar with Cucumber recently so here's an alternative solution if anyone's interested...
Given the following user records
| username | date |
| bob | Time.now.strftime("%Y-%m-%d") |
And then in your step definition just eval() the date string
Given /^the following user records$/ do |table|
table.hashes.each do |hash|
date = eval(hash["date"])
end
end
Though unlike Brandon's example this wont let you put in exact dates as well without some further logic.

bodnarbm's answer is pretty good if that is what you want to do. My own suggestion would be to take a look at the timecop gem. Use it to set time to a known day then adjust your tables accordingly.

Based on fixtures files I created this code:
Feature:
Given the following "Inquirers":
| id | email | start_date |
| 1 | alberto#deco.proteste.pt | <%= Time.now %> |
Helper:
Given(/^the following "(.*?)":$/) do |model, table|
table.hashes.each do |hash|
attributes = Rack::Utils.parse_nested_query(hash.to_query)
object = model_name.classify.constantize.new
attributes.keys.each do |key|
object.send("#{key}=", ERB.new(value).result())
end
...
end
end

Related

Excel: Spill out all matching rows

For an Excel documenten I am fitlering the data to create a "view". I got several rows of data containing the following data
| type | sender | duration | price |
In my view I want the following columns:
| sender | duration | price |
Type = data / call
Sender = phone number (several different)
Duration = time in seconds
Price = is total price for seconds
In the view I want the unique list of phone numbers if type is data, then I want the total duration and total price. The latter of these is done using SUMIFS
I know that there's an option by filtering by hand. But I assume you already found that I want this in code.
I already tried XLOOKUP but this only returns one result as cell reference. XMATCH isn't the holy grail either.
I ended up using =UNIQUE(FILTER(...)) I'd hoped that XLOOKUP and XMATCH were there to help with this.
Feel free to feedback if you have a better formula, please!

How to check data from two worksheets in Excel 2016?

I have raw data from an Excel Query that gives me details for Account Numbers(Field 2). I also have a list of Account Numbers that I want to EXCLUDE from the raw data.
Initially, I thought I could exclude them in SQL with the NOT IN condition. But I was then told that there are about 20,000 Accounts to Exclude.
So, I thought, I could export the raw data to Excel in one Worksheet and Add the Account Numbers to exclude in another worksheet and then have Excel check for those Account Numbers and give a Comment.
Worksheet 1 (Raw Data):
---------------------------------
|Field 1|Field 2|Field 3|Field 4|
---------------------------------
| 1234| A1234B| XYZ| 258.00|
---------------------------------
| 2678| B1234C| ABC| 457.25|
---------------------------------
| 5465| C1234D| DEF| 652.47|
---------------------------------
| 4587| D1234E| GHI| 458.36|
---------------------------------
| 3589| E1234F| JKL| 685.47|
---------------------------------
Worksheet 2 (Accounts to Exclude):
---------
|Field 2|
---------
| A1234B|
---------
| J1234L|
---------
| K1234Z|
---------
| D1234E|
---------
| L1234M|
---------
Intended Result:
------------------------------------------
|Field 1|Field 2|Field 3|Field 4|Result |
------------------------------------------
| 1234| A1234B| XYZ| 258.00|Excluded|
------------------------------------------
| 2678| B1234C| ABC| 457.25| |
------------------------------------------
| 5465| C1234D| DEF| 652.47| |
------------------------------------------
| 4587| D1234E| GHI| 458.36|Excluded|
------------------------------------------
| 3589| E1234F| JKL| 685.47| |
------------------------------------------
Initially, I started by sorting the Account Numbers on both worksheets and then ran the formula:
=IFERROR(VLOOKUP(B2,ExcludedAccounts,2,FALSE), "Excluded")
But then I realised, I am only looking for exact match for each cell. That won't be right.
Question:
What is the Formula to check two columns from different worksheets for similarities across the whole column?
Once I have the flag "Excluded" in another column then I can highlight cell with Conditional Formatting and then segregate those accounts.
Not enough rep to post a comment. The OPs formula is fine, if the "ExcludedRange" covers two columns (as he is returning what is in the second column) with the second column containing only blank spaces.
I'm not sure though what is meant by the below - could you please explain what the similarities are...
...different worksheets for similarities across the whole column
This is another way of doing it with a single column for the excludedrange
=IF(ISERROR(VLOOKUP(A2,ExcludedAccounts,1,0)),"Excluded","")
I think the formula you're actually looking for is
=IF(ISNUMBER(MATCH(B2,ExcludedAccounts,0)),"Excluded","")
When I used your formula, all I received were "Excluded" answers for everything because it was returning an error (this was possibly caused by missing information in your question, but I don't know). EDIT: As stated in the other answer that was submitted, yes, the original formula was looking to return a value from a second column, which would mean that ExcludedAccounts covers a minimum of two columns, not the assumed one.
There are several ways you can handle your request. However, what my above formula does is look for an exact match of the value in B2 against those values in the ExcludedAccounts named range (which I assumed is Sheet2!A2:A6). If MATCH finds one, a number will be returned (this number represents the row within ExcludedAccounts where the value from B2 was found). By wrapping this in the ISNUMBER function and placing it inside an IF statement, we're asking Excel to give us the result of "Excluded" if the excluded account is found on the list, and a result of "" if it isn't on the ExcludedAccounts list.

Pandas sort row by values of a column

My data looks something like this:
given dataframe
just with many more rows (and different timestamps).
I now want to transform the table, so that I have one row per timestamp and the row is sorted by the the JI value and each JI value contains X,Y,Z,RX,RY,RZ and confidence. So it should look like this
timestamp | JI0 | JI1 | JI2 | ... | J21 | phase
timestamp1 | (confidence|X|Y|Z|RX|RY|RZ) of JI0 | ... | (confidence|X|Y|Z|RX|RY|RZ) of JI21 | phasevalue
timestamp2 |
The ID value is not needed for now. I already tried to get this result by using pivot (and pivot_table), but I was not able to get all values for one joint after another, but rather all (actually not even all for some reason) values from RX for every Joint, then all values from RY for every joint and so on.
The code:
df.pivot_table(index='timestamp', columns='JI', values=['confidence','X','Y','Z','RX','RY','RZ'])
The result using the code from above: result
I hope my question is understandable, otherwise I am of course willing to answer any questions.

Conditional Formatting using dates and weekends

I have data in a spreadsheet like this
Name | 9/1/2016 | 9/2/2016 | .... | 6/8/2017
Abe | | | |
Jonas | | | |
I want to highlight every column where the date is either a weekend or a user defined date (for instance, a holiday....does excel already know federal holidays?).
The conditional format I have is:
Applies to
=$b$2:$KI$62
Format values where the following formula is true
#1 =WEEKDAY($B$1:$KI$1,1)=1
#2 =WEEKDAY($B$1:$KI$1,1)=7
Yet nothing shows up. At the best I've been able to only make the bottom row show up through playing with the function. Why?
With Name in A1, select B2 to the extents of your data and use the following to create a conditional formatting rule.
=WORKDAY(B$1-1, 1, $L$2:$L$4)<>B$1

Automatically Show (Un-hide) Columns in Excel

Is there a method whereby columns in Excel (2003, 2007 and/or 2010) can be automatically shown (un-hidden) when the column to the left contains data?
Something like this:
IF column to the left contains data
THEN show column
+-----+-----+
| C | C | //If column1 contains data
| O | O | //Then reveal/show (unhide) column2
| L | L |
| U | U |
| M | M |
| N | N |
| 1 | 2 |
+-----+-----+
I'm guessing that VB code is required but am unsure as to what this would be.
Further to this, is there a way to automatically show the column going by the date (first day of each month)? This is a little more complicated. For example:
FOR all dates
IF system date = year(month.day1) //If it is the first day of a new month
THEN show column(month) //Then show the corresponding column for that month
ENDIF
ENDFOR
i.e. IF system date = 01/09/2012
THEN show column(September)
Is this possible?
Thank you.
Correct, you need VBA to achieve that. Use the Worksheet_Change event which fires whenever something changes. Then, use one of the various methods to determine if a column is not empty (http://ewbi.blogs.com/develops/2006/03/determine_if_a_.html, or just google). Alternatively, if this is to slow because if fires almost all the time, you could use the Worksheet_Activate() event (an overview of all Excel events can be found here).
If your column 7 contains data, you can unhide column 8 using MyWorksheet.Columns("8:8").Hidden = False.
Your second problem can be solved in a similar way. In the Worksheet_Activate(), check if today is the first day in a month using Day(Date) = 1 (I guess it needs to take into account that the user may not be using Excel this day, so the code should be a little more complex) and show that column using MyWorksheet.Columns("12:12").Hidden = False (or whichever it is) for December 1st.
All this code assumes that the columns are already there, just hidden.

Resources