To get values from 2D array - mysql

I have used this query to retrieve the dates for one particular user's approved leaves -
LeaveRequest.where(user_id: 6).where(status: 1).pluck(:from_date, :to_date)
and I'm getting this array as result -
[[Mon, 12 Sep 2016, Fri, 16 Sep 2016], [Tue, 06 Sep 2016, Tue, 06 Sep 2016], [Thu, 01 Sep 2016, Fri, 02 Sep 2016], [Tue, 30 Aug 2016, Wed, 31 Aug 2016]]
what I want is to fetch all the dates as well as the dates between 12 Sep 2016 and 16 Sep, 2016 (13th 14th and 15th).

I am assuming you mean something like this
require 'date'
#This is to simulate your current Array
current_array = 5.times.map {|n [Date.new(2016,n+1,1).<<(1),Date.new(2016,n+1,1)]}
#map the 2 dates to a Range
new_array = current_array.map{|start_date,end_date| (start_date..end_date)}
new_array.first.class
#=> Range
Calling to_a on the Range will blow it out into all the dates between start_date and end_date
With a rails you could do something like
class LeaveRequest
def self.user_requested_ranges(user_id, status_id)
scoped.
where(user_id: user_id, status: status_id).
pluck(:from_date, :to_date).
map do |from_date, to_date|
#optionally to output the full Array in each Range you could use
#(from_date..to_date).to_a
(from_date..to_date)
end
end
end
Then call as
LeaveRequest.user_requested_ranges(6,1)

Related

Set age group based on DOB

I would like to create a Google Sheet function that if supplied with a date of birth, looks up the below table and gives the appropriate age group. I will be using this table to group 200 kids for surf lifesaving.
for example...date of Birth is 11 Oct 2011, then the age group will be U7. I have use the query function to get this data but I would like to contain it in a function and store the table as a array and compare the date to the array table.
- U 5 01 Oct 2013 30 Sep 2017
- U 6 01 Oct 2012 30 Sep 2013
- U 7 01 Oct 2011 30 Sep 2012
- U 8 01 Oct 2010 30 Sep 2011
- U 9 01 Oct 2009 30 Sep 2010
- U 10 01 Oct 2008 30 Sep 2009
- U 11 01 Oct 2007 30 Sep 2008
- U 12 01 Oct 2006 30 Sep 2007
- U 13 01 Oct 2005 30 Sep 2006
- U 14 01 Oct 2004 30 Sep 2005
Dates can be a little tricky. See This question to get a better idea. But this script can at least get you started.
This Google Sheet demonstrates a custom formula (AGEGROUP) to compare the input date to a "age-groups" sheet.
Note: Only add to "age-groups" sheet if needed.
The last row with a value is used to consider if someone is too old.
The first row of the sheet is used to see if someone is too young.
AGEGROUP() will accept a cell reference to look up
Example AGEGROUP(B2)would check cell B2, assuming it contains a date, and return a group assignment (U 5, U 6, U7...).
Looking up dates manually (instead of referencing another cell) can be accomplished by nesting DATE inside of AGEGROUP. You must follow the right arguments for DATE (year, month, day).
Example AGEGROUP(DATE(2010,1,21))
I'm new to custom functions and did not look into the options for supporting function details like #param or #return and so on.
Apps Script
/**
* Finds age group of the input value (date).
*
* #param {date} Input the date to compare to date ranges.
* #return Returns age groupd based on provided date.
* #customfunction
*/
function AGEGROUP(date) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var ages = ss.getSheetByName("age-groups");
var range = ages.getDataRange();
var values = range.getValues();
var i = 0;
var ranges = values.length;
var lastRow = ranges -1;
// loop through dateRanges
for(i; i < ranges; i++) {
// if date in AGEGROUP()
if(date >= values[i][1] && date <= values[i][2]) {
// return age group
return values[i][0];
}
// if child is too old
if(date > values[0][2]) {
return "Child is too young.";
}
// if child is too young
if(date < values[lastRow][1]) {
return "Child is too old.";
}
}
}

How to fetch JSON content values dynamically using JSON slurper in groovy

I am trying to filter out the JSON response values by passing on few parameters dynamically. Below is the code.
import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("Request 1").getPropertyValue("Response")
jsonresponse = new JsonSlurper().parseText(responseContent)
log.info jsonresponse.context["parameter"]
context["parameter"] stores the values from an Excel sheet and doing only a log.info for context["parameter"], it shows those values correctly. Below is the output of the code log.info context["parameter"].
Thu Dec 14 07:18:53 CST 2017:INFO:firstName
Thu Dec 14 07:18:53 CST 2017:INFO:lastName
Thu Dec 14 07:18:54 CST 2017:INFO:frNum
Thu Dec 14 07:18:54 CST 2017:INFO:notes
But when I execute the code
log.info jsonresponse.context["parameter"]
Result:
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:42 CST 2017:INFO:[]
Thu Dec 14 07:20:42 CST 2017:INFO:[]
log.info jsonresponse yields the below response out of which I need to get certain values (values stored in the context["parameter"])
Thu Dec 14 07:21:45 CST 2017:INFO:[{errorMessage=null, middleName=null, lastName=Kosnick, frName=Kosnick Steven H , mplastName=null, competeRgnTxt=null, doNum=null, gddUnitStDate=null, fdNum=null, mpfirstName=null, legalEntityID=null, noNum=null, contractType=Financial Rep, frNum=046426, offcNum=NO 091, notes=Active, fullTmeSrvDt=null, firstName=Steven, networkOfficeNum=091}]
Instead of this :
log.info jsonresponse.context["parameter"]
Try this :
log.info jsonresponse[context["parameter"]]
This should resolve your issue :)

Get date without offset

I'm using MySQL to store my data. I'm recorded dates with different offset according to each timezone. What is important on those dates are only "effective" time. Here is an example:
date1 = "Thu, 27 Oct 2016 07:00:00 CEST +02:00"
# I want to get 700
date2 = "Thu, 27 Oct 2016 22:00:00 CEST +02:00"
# I want to get 2200
I would like to get these values to compare them with my current time. Then if it's currently "2016-11-17 10:12:00 +0100", I would like to compare on a where clause:
"1012" > "2200"
# or
"1012" > "700"
Here is a fake record value:
# my_table
# | id | my_hour
# | XX | "Thu, 27 Oct 2016 07:00:00 CEST +02:00"
# | XY | "Thu, 27 Oct 2016 22:00:00 CEST +11:00"
Here is my code:
hour = Time.zone.now
# "2016-11-18 21:10:00 +0100"
where("CAST(DATE_FORMAT(my_hour,'%H%i') AS UNSIGNED) < ?", hour.strftime("%H%M").to_i)
# => Get XX record?
Use strftime to format date in Rails.
hour = Time.zone.now
MyTable.where("cast(strftime('%H%M', my_hour) as int) < ?", hour.strftime("%H%M").to_i)
here MyTable is your Model, my_hour is your date column and hour is current time.

How to Display 4week per month with condition in sql

I have this logic but I cant figure out what to do to come up with the results. I want to achieve the results below, the idea is (Ex. month of October 2014), normally it has 5 weeks. If there is a 5th week and days is greater than 3 then consider whole week in the next month else add it to 4th week.
Input:
date range
from: 10-01-2014
to: 11-30-2014
Sample Output:
Date Customer week# Dates covered
10-01-2014 Cust01 1 Oct 01 - Oct 11, 2014
10-08-2014 Cust02 1 Oct 01 - Oct 11, 2014
10-17-2014 Cust02 2 Oct 12 - Oct 18, 2014
10-25-2014 Cust03 3 Oct 19 - Oct 25, 2014
10-31-2014 Cust01 4 Oct 26 - Oct 31, 2014
11-01-2014 Cust01 1 Nov 01 - Nov 08, 2014
11-28-2014 Cust02 4 Nov 23 - Nov 30, 2014
11-30-2014 Cust05 4 Nov 23 - Nov 30, 2014
Thanks
Use datepart(ww,[date]) to get week number in a year, then compare the week number by the begin date of a month.
Below is the detail approach to calculate for one date:
declare #dt date, #month_begin date, #month_end date, #week_of_month int
set #dt='20141031'
set #month_begin = convert(date,(convert(char(6),#dt,112)+'01'))
set #month_end = dateadd(day, -1, dateadd(month,1,#month_begin))
set #week_of_month = datepart(ww,#dt) - datepart(ww,convert(date,(convert(char(6),#dt,112)+'01'))) + 1
select
case when #week_of_month > 4 and datepart(dw,#month_end)>3
then 1
else #week_of_month
end as week,
case when #week_of_month > 4 and datepart(dw,#month_end)>3
then datepart(month,dateadd(month,1,#dt))
else datepart(month,#dt)
end as month

Dealing with empty cells in Google Apps Script

Why doesn't this work?
function sendDuesReminder() {
var paid = 3;
var name = 1;
var submitted = 0;
var allowance = 9;
var ms = 86400000; // Number of milliseconds in a day
var today = new Date();
var reminder = 72;
var data = SpreadsheetApp.openById('___').getSheetByName('Master').getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
if (data[i][paid] != 'Yes' &&
data[i][paid] != 'yes' &&
data[i][reminder] == '' &&
((((today.valueOf()) - (data[i][submitted].valueOf()))/ms) > allowance)) {
MailApp.sendEmail("___", "___") ;
SpreadsheetApp.openById('___').getSheetByName('Master').getRange(i+1, reminder).setValue(today);
}
if (data[i][paid] != 'Yes' &&
data[i][paid] != 'yes' &&
data[i][reminder] != null &&
((((today.valueOf()) - (data[i][reminder].valueOf()))/ms) > allowance)) {
MailApp.sendEmail("___", "___") ;
SpreadsheetApp.openById('___').getSheetByName('Master').getRange(i+1, reminder).setValue(today);
}
SpreadsheetApp.flush();
}
Browser.msgBox("OK. Reminder e-mails have been sent. !")
}
What I really want to do is say send the e-mail if column BT is empty OR if the date in column BT is older than 9 days. But I didn't know how to include an or statement within the if statement. So I just set up two separate if statements.
When I use '', though, for the second if statement, I get an error saying that it can't get the value of something that's undefined.
When I use null or undefined, it doesn't work.
Anybody have any thoughts?
Are you sure you have 72 columns in your spreadsheet ? when you use data[i][reminder] == '' the error returned concerns the first term of the equal, not the second... so I guess data doesn't have 72 secondary index and this is returning an error.
(EDIT : to be more clear : you cannot get the value of an undefined item using .valueOf and divide it by something, that's why you get "an error saying that it can't get the value of something that's undefined ".)
If you want to learn about how to implement OR statement you can have a look at w3school, 'Comparison Operators'
Note that you can compare date object directly, you don't need to convert them to milliseconds explicitely, javascript does it internally.
here is an example of how you could write this shortly
var tendaysBefore = new Date(new Date().getTime()-10*24*60*60*1000);// 10 days before
if (data[i][paid].toLowerCase() != 'yes' && (data[i][reminder] == ''||(data[i][reminder] == null || data[i][submitted]< tendaysBefore)) { // not 'yes' and one of the 3 other conditions
MailApp.sendEmail("___", "___") ;
SpreadsheetApp.openById('___').getSheetByName('Master').getRange(i+1, reminder).setValue(today);
}
So here's the code for the loop I tried based on Serge's very kind initial thought.
for (var i = 1; i < data.length; i++) {
if (data[i][paid].toLowerCase() != 'yes' &&
(data[i][submitted]<tendaysBefore &&
(data[i][reminder] == ''||
data[i][reminder] == null))) {
MailApp.sendEmail("___", "Reminder About Dues for Newcomers & Neighbors", "___") ;
SpreadsheetApp.openById('___').getSheetByName('Master').getRange(i+1, reminder).setValue(today);
}
SpreadsheetApp.flush();
}
I changed the order of the if statement because I'm not sure I've adequately articulated in English what I'm trying to get it to do.
What I WANT it to say is IF they haven't paid AND 10 days have passed since they submitted their form AND EITHER the reminder cell is blank or has a null value, THEN send the e-mail.
(Once I get THIS statement working, I'll go on to say if 10 days go by after we send the first e-mail and they STILL haven't paid, we send another e-mail. That's why I'm going through all this machination to put a date in reminder rather than just something like "sent".)
The code I have works. EXCEPT, if you run it, send out the first batch of e-mails, let it add today's date to reminder, and then run it again, it sends the e-mails out all over again.
In short,
(data[i][reminder] == ''||
data[i][reminder] == null))
doesn't seem to be accomplishing what is supposed to do, which is make the if statement fail for any row that has ANYTHING in the reminder column. That's why I initially came at this with "how do I deal with empty cells"? Because--for the life of me, I can't understand why
(data[i][reminder] == ''||
data[i][reminder] == null))
isn't bulletproof.
As I suggested in my comment to Serge's answer, this is starting to make me crazy. I, therefore, cannot thank anybody enough for ANY thoughts you have. Thanks in advance!
In response to Serge's question that I capture some values from the log, here's the output from the log after I added his requested code to the loop:
Paid=yes date subm=Sat Jun 02 2012 14:44:27 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=reminder=undefined
Paid=yes date subm=Wed Jun 27 2012 16:22:35 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Fri Jun 29 2012 09:07:21 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 10:11:20 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 10:16:56 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 10:17:00 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 10:26:36 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 10:30:23 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 11:02:19 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 12:33:57 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Mon Jul 09 2012 13:48:54 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 18:19:22 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 21:07:05 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 21:44:13 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Mon Jul 09 2012 21:53:55 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Tue Jul 10 2012 11:09:03 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Tue Jul 10 2012 12:57:41 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Wed Jul 11 2012 18:36:49 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Thu Jul 12 2012 07:14:53 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Tue Jul 17 2012 14:54:16 GMT-0400 (EDT)reminder=undefined
Paid=yes date subm=Wed Jul 18 2012 20:26:23 GMT-0400 (EDT)reminder=undefined
Paid= date subm=Thu Jul 19 2012 13:43:09 GMT-0400 (EDT)reminder=undefined
I have no idea what to make of this, but I'm sure smarter people than I do see something interesting here?
OK, following Serge's diligent trouble shooting with me, here's the code that worked for this problem. Many, MANY thanks to Serge!
function sendDuesReminder() {
var paid = 3;
var name = 1;
var submitted = 0;
var allowance = 9;
var ms = 86400000; // Number of milliseconds in a day
var today = new Date();
var reminder = 72;
var reminderArray = reminder - 1;
var tendaysBefore = new Date(new Date().getTime()-10*24*60*60*1000);
var data = SpreadsheetApp.openById('___').getSheetByName('Master').getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
if (data[i][paid].toLowerCase() != 'yes' &&
(data[i][submitted]<tendaysBefore &&
(data[i][reminderArray] == ''||
data[i][reminderArray] == null))) {
MailApp.sendEmail("___", "___", "___") ;
SpreadsheetApp.openById('___').getSheetByName('Master').getRange(i+1, reminder).setValue(today);
}
SpreadsheetApp.flush();
Logger.log('Paid='+data[i][paid].toLowerCase()+' date subm='+data[i][submitted]+'reminder='+data[i][reminder])
}
Browser.msgBox("OK. Reminder e-mails have been sent. !")
}