Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,411 through 1,425 (of 2,952 total)
  • Author
    Search Results
  • #43826
    Sam Moffatt
    Participant

    There is a bunch of Javascript resources out there, mostly focused on the web unfortunately but the basics of the language are transferable so it’s not a complete waste, plus it helps understand how the web works as well so not a complete loss. Javascript is an awkward language at times with a bunch of beginner pitfalls but it’s one of the most popular languages out there when you realise every website is powered by it, it’s used as a scripting engine in a bunch of places like Tap Forms and then you have node.js for running it as a server language.

    In any case you’ve got your own use cases with Tap Forms which I feel is the best way to learn. Have a problem you care about that you want to solve and search for how to solve it. It’s a great way to learn how to program :)

    #43812
    Sam Moffatt
    Participant

    Why don’t you just do the following:

    prompter.addParameter('Kalender: ', 'calname', 'popup', arr)
             .show('Bitte Kalender auswählen:', callbackFunction);
    

    And sorting should just be arr.sort() instead of just arr:

    prompter.addParameter('Kalender: ', 'calname', 'popup', arr.sort())
             .show('Bitte Kalender auswählen:', callbackFunction);
    

    Since arr is already an array, you don’t need to extract it back out to be an array.

    One other trick is the JavaScript spread operator which will expand out an array for you automatically though in this case it’s not useful (since you’re expanding an array back into the same array):

    prompter.addParameter('Kalender: ', 'calname', 'popup', [...arr])
             .show('Bitte Kalender auswählen:', callbackFunction);
    

    You can also do a [...arr].sort() as well because the [] syntax is returning an array object you can operate on directly.

    It’s possible I miss understood something but I think this solves the problem for you.

    #43811
    Sam Moffatt
    Participant

    I think the best option I’d go for is T L Ford’s JavaScript 101 which is focused from a Tap Forms users approach and I have some videos on my YouTube channel as well.

    There are plenty of web focused Javascript resources (Mozilla Developer Network, W3Schools) and a few resources leveraging node.js (also W3Schools as an example), there aren’t that many resources that focus on pure Javascript that I’ve found. A lot of the resources include platform specific stuff that really only relates to the use in web browsers or to node.js rather than the ECMAScript standard.</p>

    #43809
    Ray Robillard
    Participant

    Thanks a lot, Sam ! I will try this tomorrow.

    Is there an online course on JavaScript you recommend ?

    #43808
    Chris Ju
    Participant

    Hi,

    i’m using the new JS API function Utils.getCalendarNames() (Big thanks to Brendan for this!) in the prompter dialog to list/choose and use for my scripts:

    ...
    arr = Utils.getCalendarNames();
    var calname;
    let prompter = Prompter.new();
    prompter.addParameter('Kalender: ', 'calname', 'popup', [arr[0], arr[1], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8], arr[9], arr[10], arr[11], arr[12], arr[13], arr[14], arr[15], arr[16], arr[17], arr[18], arr[19], arr[20], arr[21], arr[22], arr[23], arr[24], arr[25], arr[26], arr[27], arr[28], arr[29], arr[30]])
    .show('Bitte Kalender auswählen:', callbackFunction);
    ...

    1. In my case i have 30 items from the array in my list. If i have less than 30 calendars, the “missing” items in the prompter are empty. Is it possible to limit the list according to the length of the array so that only the calendars that actually exist are displayed?

    2. Is it possible to sort the calendar list alphabetically?

    Thanks!

    Chris

    Sam Moffatt
    Participant

    Well we think the script field execution in a table row is also bugged right now. I can get it to trigger when I edit a related field in the same row but it doesn’t recalc everything and when I run the recalc formulas button at the bottom of the record. So you end up with the situation where all of the other rows are wrong until you click into the record and hit enter to trigger the script field for that row.

    The alternative that works consistently is to create a script field in the form that handles recalculating. I haven’t been able to get it to trigger consistently either but when I click “recalc formulas” it at least updates consistently for me.

    Here’s the script:

    function Populate_Percentage() {
    	let total = record.getTotalOfLinkedFieldForField('fld-b9927271feeb4fe2b9c0cd32f36c7cd1', 'fld-3fead523555c48f9a77377fd340e59f6');
    	let table = record.getFieldValue('fld-b9927271feeb4fe2b9c0cd32f36c7cd1');
    	
    	for (let index in table) {
    		var per_serving_amount = table[index].getFieldValue('fld-3fead523555c48f9a77377fd340e59f6');
    		table[index].setFieldValue('fld-9048ea3263164a739908f8add07b5ed7', per_serving_amount / total);
    	}
    	document.saveAllChanges();
    }
    
    Populate_Percentage();
    

    Some definitions:

    • fld-b9927271feeb4fe2b9c0cd32f36c7cd1 is the ID of the table field.
    • fld-3fead523555c48f9a77377fd340e59f6 is the ID of the per_serving_amount field or for you this would be your quantity field.
    • fld-9048ea3263164a739908f8add07b5ed7 is the ID of the percentage field or your ‘% of recipe’ field.

    You should be able to replace the IDs with the ones from your form and it should more or less work.

    • Decomposing the script, the let total line gets the total from the table field of the field in the table field.
    • The let table line gets all of the records in the table field.
    • The for line is the start of a loop that goes through all of the records.
    • The var per_serving_amount gets the value of the per serving field from the table row number index.
    • The setFieldValue is setting our percentage field for that row (the table[index]).
    • The document.saveAllChanges() is required to tell Tap Forms you made a change and you want to keep it.
    • Finally the Populate_Percentage() line triggers everything.

    This should work if you create a new script field in your form, ensure your percentage field is a number field and then you update the ID’s to match. It does mean to update you will need to click on the “Recalculate formulas” button at the bottom of your record. You can hide the script field so that it doesn’t show up in your default layout as well.

    scott johnson
    Participant

    Oof – that may be exceeding my capabilities. I really appreciate the insight – let me play with it a bit today and see if a non-computer guy can figgure out scripting 8-)

    #43794
    Sam Moffatt
    Participant

    The undefined type means that its not an object which is why when you do value.search you get an error because value is undefined and has no value. Since you’re iterating through all of your records, you need to check if the value you get back is undefined and skip because if the field isn’t set on a record, then you will get an undefined value back from the scripting API.

    Try something like this that ignores empty fields and also logs any records missing tags for further review:

    function Feel_Good() {
    
        var nombre = 0;
        var movie_form = document.getFormNamed(‘Movies’);
    
        var movie_form_tags_field = movie_form.getFieldNamed(‘Tags’);
        var movie_form_tags_id = movie_form_tags_field.getId();
    
        var movie_form_records = movie_form.getRecords();
    
        for (var index = 0, count = movie_form_records.length; index < count; index++) {
            var value = movie_form_records[index].getFieldValue(movie_form_tags_id);
    
            if (value) {
                var pos = value.search(“Feel good”);
    
                if (pos > -1) {
                    nombre++;
                }
            } else {
                console.log("Record missing tags: " + record.getUrl());
            }
    
        }
    
        return nombre;
    }
    
    Sam Moffatt
    Participant

    Script fields in tables seem bugged but I did this in a test and it worked in the script editor:

    function Percentage() {
    	var per_serving_amount = record.getFieldValue('fld-3fead523555c48f9a77377fd340e59f6');
    	var total = record.parentRecord.getTotalOfLinkedFieldForField("fld-b9927271feeb4fe2b9c0cd32f36c7cd1", 'fld-3fead523555c48f9a77377fd340e59f6');
    	return (per_serving_amount / total * 100);
    }
    
    Percentage();
    

    Where per_serving_amount or fld-3fead523555c48f9a77377fd340e59f6 is a field inside my table and where fld-b9927271feeb4fe2b9c0cd32f36c7cd1 is the field ID of the table in the main form. It gives me the right value in the script editor but it feels like script fields are not being properly evaluated in table fields.

    Alternatively if it was possible to project parent fields into the table calculations, I created a calculation field in the parent record with the total of the field int he table however I couldn’t readily get to it via a calculation in the table.

    Brendan
    Keymaster

    You would need to use a Script field for that and ask the form for the total of the field from the Link to Form field.

    You would need to loop through all of the records of the Table field and total up the value of that column. This would be done whenever the value in your Quantity field changes.

    #43781
    Ray Robillard
    Participant

    Hi !

    I’ve a movie database in which there’s a Tag pick list, made of checkboxes. I want to find how many different records have a certain item in this pick list checked.

    So thinking the pick list returned something I wasn’t sure of, I tried to get the type of the object, but all I am getting is undefined.

    var value = movie_form_records[index].getFieldValue(movie_form_tags_id);
    console.log(typeof value);
    (console output is “undefined”)

    So when I try this :
    var pos = value.search(“Feel good”);

    It never returns any item, despite the fact that some of the records have this tag. I know this because using the search filter, I can find several movies for which this tag is checked.

    My loop goes through all the records and make the following test :
    if (pos >-1) {
    nombre++;
    }

    But when I am running the code, I am getting the following output in the console :

    2021-03-10, 8:17:57 AM / Movies / Feel good
    Feel good: TypeError: undefined is not an object (evaluating ‘value.search’), line:(null)
    Feel good: TypeError: undefined is not an object (evaluating ‘value.search’), line:(null)

    The whole code is :

    function Feel_Good() {

    var nombre = 0;
    var movie_form = document.getFormNamed(‘Movies’);

    var movie_form_tags_field = movie_form.getFieldNamed(‘Tags’);
    var movie_form_tags_id = movie_form_tags_field.getId();

    var movie_form_records = movie_form.getRecords();

    for (var index = 0, count = movie_form_records.length; index < count; index++) {

    var value = movie_form_records[index].getFieldValue(movie_form_tags_id);

    var pos = value.search(“Feel good”);

    if (pos >-1) {
    nombre++;
    }

    }

    return nombre;
    }

    So.. what am I doing wrong ? I’m a total newbie at JavaScript. So maybe I missed something somewhere.

    Thanks,

    Ray

    scott johnson
    Participant

    So what I’m looking to do is re-build an old formulation database i built years ago in Access – where i’d have a large ingredient table with hundreds of ingredients (sugar, fats, etc – and each one of those had all their nutritional attributes stored in that same record).

    There was a second table then, which had a number of drop down fields that linked back to the ingredient table – allowing me to select each individual ingredient for use in the formulation. I’ve been able to get this far in TapForms on my own.

    Where I’m stuck is here: When i pull – say – sugar – from the drop down list that’s linked back to the ingredient table – what i’d also like it to do is pull and display the information for, say, carbohydrates – that is associated with the sugar i just choose. Why? Because I will then use that in a series of calculated fields to determine the quantity of carbohydrates in the precise formulation i’m working on, and get real time updates. This is what’s got me stuck. I’m not a scripter – so that’s out of my depth. Anything obvious I’m missing to help me do this?

    Thanks folks
    SJ

    #43733

    In reply to: Create Running Totals

    Sam Moffatt
    Participant

    If you’re after running totals, a while back I did a post on a bank account style running total. There are some caveats which I’ve posted about elsewhere around making sure your ordering is consistent otherwise the numbers will be wrong. Excel has an implicit row ID in it where as Tap Forms does not, so if you don’t have a field you can sort on consistently then you will need to add your own row number (perhaps by using an autoincrementing number field).

    #43728
    Daniel Leu
    Participant

    Hi Gerhard,

    You can use the progress feature for that, but this seems to be an overkill for such a simple task. Following script does the same thing:

    function setAddress() {
    	const adresse_lang_id = 'fld-xxx';
    	const maps24_id = 'fld-xxx';
    
    	for (r of form.getRecords()){
    		let x = r.getFieldValue(adresse_lang_id);
    		r.setFieldValue(maps24_id, x);	
    	}
    	document.saveAllChanges();
    }
    
    setAddress();

    But I’m wondering if using a form script for this tasks is what you really want. Whenever you run the script, all your maps24 field are updated.
    It might be better to use a field script for that.

    function setAddress() {
    	const adresse_lang_id = 'fld-xxx';
    	const maps24_id = 'fld-xxx';
    
    	let x = record.getFieldValue(adresse_lang_id);
    	record.setFieldValue(maps24_id, x);	
    	document.saveAllChanges();
    }
    
    setAddress();

    Now the target field is only updated when the adresse_lang field is changed.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #43721
    Gerhard Hoffmann
    Participant

    Hi Eddy,

    I’m using TabForms since 4 months and the script is a real game changer. I’m not an expert in script programming, but my son helped me a lot.
    I had a similar problem and I want to fill in the address fields to the field location, but not only to one address, it should do the work to all addresses. See, we use a for-next-loop.

    // start

    var adresse_lang_id = ‘fld-5840ffe2e4b04f01a150329d15498877’;
    var maps24_id = ‘fld-ce41104cdbc2405eb2ad0f9be8baa35a’;
    var progress = Progress.new();

    function ProcessRecords(records) {
    for (index in records) {

    if (progress.cancelled) {
    console.log(‘Cancelled operation’);
    break;
    }

    var aRec = records[index];
    var x = aRec.getFieldValue(adresse_lang_id);
    aRec.setFieldValue(maps24_id, x);

    progress.updateProgress(index);
    }
    }

    var records = form.getRecords();
    progress.totalCount = records.length;
    progress.currentCount = 1;
    console.log(‘Begin’);

    // show the progress sheet
    progress.show(‘Processing Records…’);

    ProcessRecords(records);

    // dismiss the progress sheet when done.
    progress.dismissProgress();

    // save your changes
    form.saveAllChanges();

    console.log(‘End’);

    // end

    Regards,

    Gerhard

Viewing 15 results - 1,411 through 1,425 (of 2,952 total)