Calculation similar to VLOOKUP (MS Excel)

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Using Tap Forms Calculation similar to VLOOKUP (MS Excel)

Viewing 12 reply threads
  • Author
    Posts
  • May 31, 2019 at 2:22 AM #34969

    Chris Ju
    Participant

    Hello Brendan,

    is it possible to add a calculation similar to Excels VLOOKUP?

    Thanks!
    Chris

    May 31, 2019 at 9:38 PM #34974

    Brendan
    Keymaster

    You could do something like that using a Script field instead of a Calculation field. You’d just have to fetch the values from the field from another form using the value from a field in your current form. I’m sure there’s more nuances to the VLOOKUP function. I’ve just never used it before.

    June 1, 2019 at 10:12 AM #34985

    Sam Moffatt
    Participant

    You could possibly do a Link to Form with a JOIN type to get matches that way and then you could iterate over the values of the Link to Form field. Not quite a 1:1 match to the functionality but you could tie this together with a script field to get the exact value you want out of the children.

    June 2, 2019 at 1:25 PM #35011

    Chris Ju
    Participant

    Thanks for your reply! The Link to form solution would not really help. Nevertheless i’ll give it a try.

    A solution with a VLOOKUP calculation would be better to get data from a table inside a form.

    After i tried different things, i’ll come back to this thread.

    Thanks!
    C.

    June 2, 2019 at 10:14 PM #35016

    Sam Moffatt
    Participant

    Out of interest, do you mind sharing a little more about your use case, what structures you’ve got already and what you’re trying to solve? Might help in providing lateral solutions to achieve your goal :)

    June 2, 2019 at 10:58 PM #35018

    Chris Ju
    Participant

    Thanks again!

    That’s a very complex thing i’m trying to solve. In a silence minute, i’ll write it here!

    C.

    June 12, 2019 at 6:17 AM #35062

    Alan Woollard
    Participant

    I think I am looking for the same thing – Hope your “silence moment” bears fruit.

    June 12, 2019 at 6:28 AM #35063

    Chris Ju
    Participant

    Quiet moments are unfortunately a shy species! ;-)

    June 13, 2019 at 8:45 PM #35080

    Sam Moffatt
    Participant

    @mythical if you can share your use case that might help as well :)

    June 13, 2019 at 11:40 PM #35081

    Chris Ju
    Participant

    Thanks for your interest and help!

    There are several scenarios, but all are based on the same problem (which can be easily implemented with Excel’s VLOOKUP!):

    1. I have a form (form1), eg clients.
    2. In this form I have a table (tab1) or a child form (form2) with eg address data (street, postcode, city, …).
    3. tab1 / form2 has several records linked to form1 (eg several addresses, such as business, private, branch, …)
    4. Now I want to set a main address from the linked records (tab1 / form1) for a client or switch as needed. The reason for this is that form1 itself is linked to a form eg “files” (form0) and the main address is to be used in form0.

    ….

    So far, I have entered the main address in form1 itself. But that is awkward!

    The next scenario would be to be able to select in form0 from the records from form1 (from tab1/form2) by checkbox.

    Thanks!

    June 14, 2019 at 11:07 PM #35089

    Sam Moffatt
    Participant

    This will work, it requires using script field instead of calc field but it’s not that bad. You need to tell it the value you want to lookup with, the “join” field (table or link to form), the search field to match the lookup value on and then the field to return.

    Create a new form script called “vlookup” with the following contents:

    function vlookup(lookup, join_field, search_field, return_field) {
    	var entries = record.getFieldValue(join_field);
    
    	for (var index = 0, count = entries.length; index < count; index++){
         	var target = entries[index].getFieldValue(search_field);
    		if (target && target == lookup) {
    			return entries[index].getFieldValue(return_field);
    		}
    	}
    	return "";
    }

    Then create a script field to map the values across:

    form.runScriptNamed('vlookup');
    var addresses_id = 'fld-34e22de8a7cf438fb4a83146108f0511';
    var address_name_id = 'fld-f05929829d674141aaed98efe11e29f1';
    var street_id = 'fld-04ec2a23e3554770b3e1f1d771157dd6';
    var primary_address = record.getFieldValue('fld-9b2865aa57b74b70bd4421b27081d65b');
    
    vlookup(primary_address, addresses_id, address_name_id, street_id);
    

    In the script editor, select the fields from the linked form and use the “ID” button instead of double clicking them to get the var syntax. You’ll want to change the last field to match your fields across.

    I’ve attached a sample archive which should demonstrate what I’m talking about. It also has another form script using a prompter to handle the address change but for some reason the script fields aren’t updating afterwards, you have to manually press refresh on the record.

    Attachments:
    You must be logged in to view attached files.
    June 15, 2019 at 3:47 AM #35094

    Chris Ju
    Participant

    That is excellent! Thanks alot! Did not think that it is so “easy”!

    Maybe I have other much more complex programming tasks. Are you interested in helping me? Of course I would pay for it!

    June 15, 2019 at 10:39 AM #35096

    Sam Moffatt
    Participant

    It’s actually mostly templated from the snippets, the child records loop snippet looks mostly like the vlookup function and the prompter snippet was in the other form script POC.

    I don’t have dedicated time to commit to extra work, I don’t mind helping ad hoc on the forums though. Something like this is also generically useful and interesting enough.

Viewing 12 reply threads

You must be logged in to reply to this topic.