ODK Survey: Passing arguments to calculations in Calculates sheets


#1

Hi all,

I am testing out Survey and I was wondering if the following would be possible to pass an argument to a formula entered in the "Caculates" sheet. Let's consider the following use:

  • I am using the Calculate sheet, say to calculate an age from a birthday entered by the user. I would use the data entered by the user in a formula to calculate the age, like so:

Calcuates Sheet:

calculation_name: calculateAge
calculation: (now().getTime()-new Date(data('birthday')).getTime())/1000/60/60/24/365

  • I then have an "assign" type prompt in survey sheet (say, "member1_age") that has calculates.calculateAge() for a value in the "Calculation" column of the survey sheet.

  • Now if I have 4 family members, instead of having 4 formula to enter (with something like (data('member1_birthday')) & (data('member2_birthday')) & etc), it would be nice if I could just pass to my calculation the variable on which I would like to it base its calculation. So it would look like this:

Calculates sheet:

calculation_name: calculateAge
calculation: (now().getTime()-new Date(data(variable_passed_as_argument)).getTime())/1000/60/60/24/365

And in the survey sheet, in the Calculation column, something like:

calculates.calculateAge(member1_age) for member1
calculates.calculateAge(memeber2_age) for member2,

etc.

Would there be a way to make this work? If possible, how to I make my calculation understand I don't want him to use the argument?


(clarice larson) #2

We don't currently support this in the calculates worksheet of Survey.

However, you are welcome to explore using a session variable for your
purposes. I've attached an Excel file with an example. I've used the
session variable to store the birthday for member 1, perform the
calculation for the age of member 1 with the session variable, change
the session variable to the birthday for member 2, and then use the
same calculation for the age of member 2. Session variables are not
stored in the database. Also, their values are not retained after the
Survey form is closed. If this doesn't meet your needs, the four
different calculations will work.

Clarice

birthday.xlsx (11.3 KB)

··· On Mon, Jul 7, 2014 at 11:00 AM, wrote: > Hi all, > > I am testing out Survey and I was wondering if the following would be possible to pass an argument to a formula entered in the "Caculates" sheet. Let's consider the following use: > > - I am using the Calculate sheet, say to calculate an age from a birthday entered by the user. I would use the data entered by the user in a formula to calculate the age, like so: > > Calcuates Sheet: > > calculation_name: calculateAge > calculation: (now().getTime()-new Date(data('birthday')).getTime())/1000/60/60/24/365 > > - I then have an "assign" type prompt in survey sheet (say, "member1_age") that has calculates.calculateAge() for a value in the "Calculation" column of the survey sheet. > > - Now if I have 4 family members, instead of having 4 formula to enter (with something like (data('member1_birthday')) & (data('member2_birthday')) & etc), it would be nice if I could just pass to my calculation the variable on which I would like to it base its calculation. So it would look like this: > > Calculates sheet: > > calculation_name: calculateAge > calculation: (now().getTime()-new Date(data(variable_passed_as_argument)).getTime())/1000/60/60/24/365 > > And in the survey sheet, in the Calculation column, something like: > > calculates.calculateAge(member1_age) for member1 > calculates.calculateAge(memeber2_age) for member2, > > etc. > > Would there be a way to make this work? If possible, how to I make my calculation understand I don't want him to use the argument? > > -- > You received this message because you are subscribed to the Google Groups "ODK Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an email to opendatakit-developers+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/d/optout.

#3

Hi Clarice,

This is very interesting. So I understand that you assign, for each member, the value of interest (birthday) to the session variable. Then you calculate the age, assign that calculated value to the member's age. You can repeat that X times for X different members if the same calculation is needed.

I do have some more questions based on this:

  • In calculates sheet, what does new Data(.......) exactly means? I have tried to just use a reference to data("display_birthday") but that doesn't seem to work (I get a loading screen that goes on forever apparently). Therefore new Data(....) must be a function, but is it a Java function or is it defined in Survey itself?

  • My Java isn't really up to par, sorry if the next question seems naive. But since calculations can include any valid Java expression, wouldn't there be a way, using Java, to call a calculation and treat it just like a function and pass it one or more arguments to perform the calculation with? Or it there some built-in limitations that prevent this?

··· On Monday, July 7, 2014 2:00:07 PM UTC-4, francisv...@gmail.com wrote: > Hi all, > > I am testing out Survey and I was wondering if the following would be possible to pass an argument to a formula entered in the "Caculates" sheet. Let's consider the following use: > > - I am using the Calculate sheet, say to calculate an age from a birthday entered by the user. I would use the data entered by the user in a formula to calculate the age, like so: > > Calcuates Sheet: > > calculation_name: calculateAge > calculation: (now().getTime()-new Date(data('birthday')).getTime())/1000/60/60/24/365 > > - I then have an "assign" type prompt in survey sheet (say, "member1_age") that has calculates.calculateAge() for a value in the "Calculation" column of the survey sheet. > > - Now if I have 4 family members, instead of having 4 formula to enter (with something like (data('member1_birthday')) & (data('member2_birthday')) & etc), it would be nice if I could just pass to my calculation the variable on which I would like to it base its calculation. So it would look like this: > > Calculates sheet: > > calculation_name: calculateAge > calculation: (now().getTime()-new Date(data(variable_passed_as_argument)).getTime())/1000/60/60/24/365 > > And in the survey sheet, in the Calculation column, something like: > > calculates.calculateAge(member1_age) for member1 > calculates.calculateAge(memeber2_age) for member2, > > etc. > > Would there be a way to make this work? If possible, how to I make my calculation understand I don't want him to use the argument?

(clarice larson) #4

data() is a JavaScript function that we provide. You are correct in that
it will not work with the display_birthday session variable since session
variables are not stored in the database. The data() function returns the
value currently stored in the database for the given name. Documentation
regarding the formula functions that we provide can be found at
http://opendatakit.org/use/2_0_tools/odk-application-designer-2-0-alpha-1/odk-xlsxconverter-2-0-beta-2/#formulaFunctions.

We currently don't support passing in arguments in the way that you
describe. The justification for doing this was to prevent users form
damaging the framework code. In theory, we could support this, but it is
not something that we will do in the near term.

Clarice

··· On Tue, Jul 8, 2014 at 8:03 AM, wrote:

Hi Clarice,

This is very interesting. So I understand that you assign, for each
member, the value of interest (birthday) to the session variable. Then you
calculate the age, assign that calculated value to the member's age. You
can repeat that X times for X different members if the same calculation is
needed.

I do have some more questions based on this:

  • In calculates sheet, what does new Data(.......) exactly means? I have
    tried to just use a reference to data("display_birthday") but that doesn't
    seem to work (I get a loading screen that goes on forever apparently).
    Therefore new Data(....) must be a function, but is it a Java function or
    is it defined in Survey itself?

  • My Java isn't really up to par, sorry if the next question seems naive.
    But since calculations can include any valid Java expression, wouldn't
    there be a way, using Java, to call a calculation and treat it just like a
    function and pass it one or more arguments to perform the calculation with?
    Or it there some built-in limitations that prevent this?

On Monday, July 7, 2014 2:00:07 PM UTC-4, francisv...@gmail.com wrote:

Hi all,

I am testing out Survey and I was wondering if the following would be
possible to pass an argument to a formula entered in the "Caculates" sheet.
Let's consider the following use:

  • I am using the Calculate sheet, say to calculate an age from a
    birthday entered by the user. I would use the data entered by the user in a
    formula to calculate the age, like so:

Calcuates Sheet:

calculation_name: calculateAge
calculation: (now().getTime()-new
Date(data('birthday')).getTime())/1000/60/60/24/365

  • I then have an "assign" type prompt in survey sheet (say,
    "member1_age") that has calculates.calculateAge() for a value in the
    "Calculation" column of the survey sheet.

  • Now if I have 4 family members, instead of having 4 formula to enter
    (with something like (data('member1_birthday')) &
    (data('member2_birthday')) & etc), it would be nice if I could just pass to
    my calculation the variable on which I would like to it base its
    calculation. So it would look like this:

Calculates sheet:

calculation_name: calculateAge
calculation: (now().getTime()-new
Date(data(variable_passed_as_argument)).getTime())/1000/60/60/24/365

And in the survey sheet, in the Calculation column, something like:

calculates.calculateAge(member1_age) for member1
calculates.calculateAge(memeber2_age) for member2,

etc.

Would there be a way to make this work? If possible, how to I make my
calculation understand I don't want him to use the argument?

--
You received this message because you are subscribed to the Google Groups
"ODK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to opendatakit-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(clarice larson) #5

Correction for my response. The data("display_birthday") even though
it is a session variable should work. There is a different code path
for this type of variable. Date is a JavaScript function not one that
we built in. I'm unclear as to what you entered to get the loading
screen behavior you describe. Feel free to clarify or send an example
xlsx file.

Clarice

··· On Tue, Jul 8, 2014 at 10:41 AM, clarice larson wrote: > data() is a JavaScript function that we provide. You are correct in that it > will not work with the display_birthday session variable since session > variables are not stored in the database. The data() function returns the > value currently stored in the database for the given name. Documentation > regarding the formula functions that we provide can be found at > http://opendatakit.org/use/2_0_tools/odk-application-designer-2-0-alpha-1/odk-xlsxconverter-2-0-beta-2/#formulaFunctions. > > We currently don't support passing in arguments in the way that you > describe. The justification for doing this was to prevent users form > damaging the framework code. In theory, we could support this, but it is > not something that we will do in the near term. > > Clarice > > > On Tue, Jul 8, 2014 at 8:03 AM, wrote: >> >> Hi Clarice, >> >> This is very interesting. So I understand that you assign, for each >> member, the value of interest (birthday) to the session variable. Then you >> calculate the age, assign that calculated value to the member's age. You can >> repeat that X times for X different members if the same calculation is >> needed. >> >> I do have some more questions based on this: >> >> - In calculates sheet, what does new Data(.......) exactly means? I have >> tried to just use a reference to data("display_birthday") but that doesn't >> seem to work (I get a loading screen that goes on forever apparently). >> Therefore new Data(....) must be a function, but is it a Java function or is >> it defined in Survey itself? >> >> - My Java isn't really up to par, sorry if the next question seems naive. >> But since calculations can include any valid Java expression, wouldn't there >> be a way, using Java, to call a calculation and treat it just like a >> function and pass it one or more arguments to perform the calculation with? >> Or it there some built-in limitations that prevent this? >> >> On Monday, July 7, 2014 2:00:07 PM UTC-4, francisv...@gmail.com wrote: >> > Hi all, >> > >> > I am testing out Survey and I was wondering if the following would be >> > possible to pass an argument to a formula entered in the "Caculates" sheet. >> > Let's consider the following use: >> > >> > - I am using the Calculate sheet, say to calculate an age from a >> > birthday entered by the user. I would use the data entered by the user in a >> > formula to calculate the age, like so: >> > >> > Calcuates Sheet: >> > >> > calculation_name: calculateAge >> > calculation: (now().getTime()-new >> > Date(data('birthday')).getTime())/1000/60/60/24/365 >> > >> > - I then have an "assign" type prompt in survey sheet (say, >> > "member1_age") that has calculates.calculateAge() for a value in the >> > "Calculation" column of the survey sheet. >> > >> > - Now if I have 4 family members, instead of having 4 formula to enter >> > (with something like (data('member1_birthday')) & (data('member2_birthday')) >> > & etc), it would be nice if I could just pass to my calculation the variable >> > on which I would like to it base its calculation. So it would look like >> > this: >> > >> > Calculates sheet: >> > >> > calculation_name: calculateAge >> > calculation: (now().getTime()-new >> > Date(data(variable_passed_as_argument)).getTime())/1000/60/60/24/365 >> > >> > And in the survey sheet, in the Calculation column, something like: >> > >> > calculates.calculateAge(member1_age) for member1 >> > calculates.calculateAge(memeber2_age) for member2, >> > >> > etc. >> > >> > Would there be a way to make this work? If possible, how to I make my >> > calculation understand I don't want him to use the argument? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "ODK Developers" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to opendatakit-developers+unsubscribe@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. > >

(Yaw Anokwa) #6

A post was merged into an existing topic: Help with calculation column