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 - 2,221 through 2,235 (of 2,950 total)
  • Author
    Search Results
  • #37843
    Peter Wolf
    Participant

    Doh! I am struggling to make sense of the examples although I do know Google Apps Script for Sheet. Can someone kindly help get me started with scripting my planned application?

    I have successfully imported into TF on iPhone a .csv file of names, membership numbers and some other details of ticket bookings for an event. Each ticket will have a printed QR or barcode of the person’s membership number. I wish to create a TF script which enables the QR or barcode on each ticket to be scanned by the iPhone on attendee registration, retrieving the relevant ticket and automatically completing a field in the form to record their attendance. That’s all . . .

    Thanks in hope and in advance!

    #37838

    In reply to: Time delay in scripts

    Sam Moffatt
    Participant

    The only way I can think of would be to create a busy loop and check to see if time has elapsed. Something like this should work:

    function delay(duration)
    {
    	let now = new Date();
    	let future = now.getTime() + duration;
    	while((new Date()).getTime() < future) { }
    }
    
    function Timer() {
    	console.log(new Date());
    	delay(5000);
    	console.log(new Date());
    }
    
    Timer();

    Sample Execution:

    Tue Nov 05 2019 17:58:36 GMT-0800 (PST)
    Tue Nov 05 2019 17:58:41 GMT-0800 (PST)

    There might be a better way but this will work in a pinch.

    You might want to push the logic for the delay into the method call itself. If this is running inside a big loop, it should work ok. If you have multiple field scripts that you’re executing then I’m not sure this will be as effective so give it a try first:

    var lastCalled = 0;
    
    function delay(duration)
    {
    	let now = new Date();
    	let future = now.getTime() + duration;
    	while((new Date()).getTime() < future) { }
    }
    
    function rateLimitedLogger(message, minTime = 5000)
    {
    	let now = (new Date()).getTime();
    	let nextExecution = lastCalled + minTime;
    	if (now < nextExecution)
    	{
    		console.log(<code>Waiting ${nextExecution - now}ms</code>);
    		delay(nextExecution - now);
    	}
    	lastCalled = (new Date()).getTime();
    	console.log(new Date() + ' ' + message);
    
    }
    
    function Timer() {
    	rateLimitedLogger('Message 1');
    	rateLimitedLogger('Message 2');
    	delay(3000);
    	rateLimitedLogger('Message 3');
    	delay(6000);
    	rateLimitedLogger('Message 4');
    }
    
    Timer();

    Sample output:

    Tue Nov 05 2019 18:05:40 GMT-0800 (PST) Message 1
    Waiting 4999ms
    Tue Nov 05 2019 18:05:45 GMT-0800 (PST) Message 2
    Waiting 2000ms
    Tue Nov 05 2019 18:05:50 GMT-0800 (PST) Message 3
    Tue Nov 05 2019 18:05:56 GMT-0800 (PST) Message 4
    

    Obviously you’d need to swap out the console logging for your own implementation. This should work fine though and you can see that in my case it works fine. The examples are:

    1. The first launch, should always run.
    2. An immediately following execution (should be max time though looks like we lost a millisecond)
    3. I put in a manual delay for 3000 ms to demonstrate it properly accounting for that as the offset of 2000ms.
    4. I put in a manual delay for 6000 ms to demonstrate it properly accounting for being able to run immediately.

    Depending on what you’re doing, you might want to set the last called variable from another record somewhere and update it as you’re going. This would be important for if you are changing fields that are being watched by field scripts however this sort of script will pause TF because of the nature of how it’s running.

    Are you using a form script for this that you manually execute or field scripts?

    #37835
    Nigel Hughes
    Participant

    Hi,
    I’m just starting the coding of scripts in Tap Forms but have come to a bit of a road block. My goal is to calculate the nutrients in my weekly food shop. So I’ve created my shopping list as a database – with he records being all the common shopping items and record fields properties of those items including their nutrients and the date they were last bought. However to create a summary of the total for each of the 100 odd nutrients for each week’s shopping list i have created another linked form with a JavaScript doing the totalling of each of the nutrients in all the shopping items In the first form (for a particular day). I am trying to use the function getTotalOfField to do this ona search of a days shopping (in the first database) but am running into errors in its use that I don’t understand how to get around. Following the example on the Tap Forms JavaScript API webpage I have found the search object and then used the getTotalOfField function on this. However I keep getting the error:
    TypeError: NutrientSearch_object.getMinOfField is not a function. (In ‘NutrientSearch_object.getMinOfField(nutrient_ID)’, ‘NutrientSearch_object.getMinOfField’ is undefined)
    I’ve attached screen shots of the code and resulting consult log in the hope that someone might be able to help me see what I’m doing wrong. Thanks in advance!

    Attachments:
    You must be logged in to view attached files.
    #37834
    Grant Anderson
    Participant

    Hi, I’m updating entries in my database and need to access an online barcode lookup system. It only allows lookups every 5 seconds so as not to overload the server. How can I add a time delay in a script? I tried using Javascript’s setTimeout which would call a function after a specified time but Tap Forms compiler doesn’t seem to understand that. Any ideas?

    #37832
    Daniel Leu
    Participant

    No idea what happened to my comment…. you can repost it for context. No problem with me.

    Good thing I still have my test document…. store_id and store_name_id are variables that need to be defined in your script. So this all belongs together as part of the script field options > script. This way the script know from where to fetch the necessary data.

    var store_id = 'fld-xxxx';
    var store_name_id = 'fld-xxxx';
    
    function joinChildField(childLinkId, fieldId){
    	let entries = record.getFieldValue(childLinkId);
    	var list = Array();
    	for (entry of entries){
    		list.push(entry.getFieldValue(fieldId));
    	}
    	return list.join(', ');
    }
    
    joinChildField(store_id, store_name_id);

    The function definition uses parameter names that make sense for different applications. So when you call this function with joinChildField(store_id, store_name_id);, store_id will become childLinkId and store_name_id becomes fieldId inside the function.

    Cheers, Daniel

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

    #37824
    Daniel Leu
    Participant

    I have to admit that I do all my development work on a desktop…. So I’m discovering the process along with you :)

    Let’s assume you have two forms in your document, Store and Wines. When you see them, there is an ‘Edit’ button on the top right. Click on it. Now you can edit the forms. Select Wines.
    I currently have three fields: ‘Wine Name’ (text), ‘Store’ (linked field to Store form, many-to-many type because I can buy the same wine at two stores and the stores usually have many wines, inverse relationship selected), and ‘Store TXT’ (field script). Just for completeness, in the ‘Store’ form, I have ‘Store Name’ (txt), ‘Wines’ (link), ‘Wines TXT’ (script).
    Click on ‘Store TXT’ and then ‘Field Options’ then ‘Script’. This is where your field script resides.

    You already noticed the Brenan’s and my field ids are different, so will be yours! To get the one you are looking for, touch in the text field somewhere to get the keyboard view. There is a small row with symbols. The one between fx and ABC is of interest. No idea how this symbol is called. Anyway, click on it. On the top, you can select between ‘getFieldValue()’ and ‘field_id’. The background behind these two options is ugly, looks like a bug where the background is not cleared. Here we need field_id. So select field_id.
    Since we are in the Wines form, we need the id of the ‘Store’ field (link to stores) and the name of the store.
    So click on ‘Store’. Now you are back in the editor with new text like
    var store_id = 'fld-....';
    Next we need the id of the store name. So again get the keyboard view and click on the icon between fx and ABC, click on ‘Store Name’ and you are back in the script editor with something like:
    var store_name_id = 'fld-....';
    These are the two items that are custom to your from and which are different to ours.

    Now finish the code as follows:

    function joinChildField(childLinkId, fieldId){
    	let entries = record.getFieldValue(childLinkId);
    	var list = Array();
    	for (entry of entries){
    		list.push(entry.getFieldValue(fieldId));
    	}
    	return list.join(', ');
    }
    
    joinChildField(store_id, store_name_id);

    The result will appear in the ‘Stores TXT’ field. Now you got to do for the ‘Store’ form and then you can search.

    Hope you were able to follow along….

    Update: Added my TF document for your reference

    Attachments:
    You must be logged in to view attached files.

    Cheers, Daniel

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

    #37818
    George Cx
    Participant

    Hey everyone, thanks for the awesome responses.

    First up, apologies to Brendan. I wasn’t trying to be unkind – TapForms is awesome – and is in pretty much every way far more flexible and cool than AT. Its amazing what you’ve created – and I only wanted to give an example to show what I was seeing, not to criticize – thank you for taking it in that spirit (and I’m rooting for your first billion! Lets have this conversation on your yacht!)

    Second up – I’m sorry to be such a dummy, but I’m not quite sure how to make these scripts work their magic on iOS.

    Firstly I went to one of the records, selected “run a script” from the top right menu, then pasted in Brendans code, saved it, then backed out, then went back in and ran the script. Much as monkeys throwing screwdrivers at a typewriter to write Shakespeare, it created the exact response I’d feared ie nothing, because I guess I’m not sure where the results are put, not sure how to figure out the specific variable names of my field ie var actors_id = ‘fld-984cf79ccafb45a381b0f95b0aa28e78’; var last_name_id = ‘fld-a907066570844290a27940d34de98b4f’; ie I’m assuming that long key is particular to the field of the record. (I note that Brendan and Daniel have different field references but I’m assuming thats because they ran this on their unique instances of their forms.)

    Then I backed out and edited the form itself, added a “script field” pasted in the text. Then pulled up the record and there was the script field. But this time even the typewriter was undented – the field sure said script, but clicking on it didn’t seem to invoke anything.

    If I could lean on everyone’s kindness and expertise one more time and assume you are teaching this code to a fifth grader (and not one of those really bright fifth graders) – is there a screencast or stepwise instructions that goes step by step through
    a) how to set up the code so those long keys actually related to fields my system will recognize
    b) where to paste said code
    c) how to invoke, and where to look for the results (and/or how to set up a special field to capture results*)

    *What I’m sensing will/should happen is that the code goes into the “linked” field – scrapes the returned value (say store name) then pastes “store name” as text ideally in some dedicated field so it shows on the multi-column view – then you can search/filter on that text. Anytime you update that record, you run the script again to make sure the latest scrape is in that text field – am I close?

    I could paste you a quick screen cast of my pitiable attempts, but just imagine a whole lot of nothing happening and you’ve got the gist.

    Thanks again for the pointers on this, I’m hoping my cluelessness might help others on the forum.

    Cheers y’all

    #37815
    Daniel Leu
    Participant

    Martin, the purpose of my script was just to show that you can get access to the field values using Javascript’s Date methods. I posted my response to your original question before I received your progress update :)

    Cheers, Daniel

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

    #37813
    Martin Inchley
    Participant

    Actually, Daniel, a couple of things about your script.
    1. I think you need to use getDate() rather than getDay(), in order to get the day of the month, and not the day of the week.
    2. In your final calculation, I think d and m are round the wrong way. Also the w3schools JS tutorial says to be alert for the need to add 1 to the month, because January = 0. But in my script, it works without it.

    var date_and_time_id = 'fld-a74f18a96d4c42489a40a367f54f45ee';
    var date_id = 'fld-3ef5526b645947c89317b8272dffd659';
    var time_id = 'fld-95075cb1f5604be59afbc73094591e7a';
    
    var date = record.getFieldValue('fld-3ef5526b645947c89317b8272dffd659');
    var time = record.getFieldValue('fld-95075cb1f5604be59afbc73094591e7a');
    
    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();
    var h = time.getHours();
    var mins = time.getMinutes();
    
    var compDate = new Date(y, m, d, h+1, mins)
    
    record.setFieldValue(date_and_time_id, compDate);
    form.saveAllChanges();
    
    console.log(compDate)

    However, I do currently have to add an hour to the hours, because of the problem I flagged in another thread, to which no one has yet come up with an answer.

    #37807
    Eddy
    Participant

    Dear Folks!

    I don’t know, if this is a bug of if I am to stupid…

    In a script I have to call some field values.

    For fields in the same form this is no problem.
    record.getFieldValue(…)

    But if I call a field from another form, I get a error message.
    The editor produces this phrase to call the value:
    “XXXX.getFieldValue(…)”
    “XXXX” is the name of the form, in which is the field, we need the value from.

    The error message is: “ReferenceError: Can’t find variable”

    Where is the mistake?

    Thanks in advance,
    Eddy

    P.S: My momentary workaround is to create a calculation field in my form, that calls the field value that I need and than to refer in the script to that calculation field. Working, but not really clean.

    #37800
    Martin Inchley
    Participant

    I should clarify. Using record.getFieldValue(fieldID) returns a date in the standard JavaScript output way. My problem is with extracting year, month, day (etc) values so that I can produce a new date object. I can’t see how to do this.

    #37798
    Martin Inchley
    Participant

    I want to combine the date from a Date field and the time from a Time field into a value that can be pasted into a Date/Time field.

    Using record.getFieldValue(fieldID) on either field type does not return a JavaScript Date Object, but a string with Date and Time info.

    Please does anyone know how to extract the year, the month number and the day from a Date field, and the hours and minutes from a Time field?

    #37789
    Martin Inchley
    Participant

    Thank you, Sam. So I found that this code jumps me to the specified record:

    var thisRec = form.getRecordWithId('rec-93d0352d75c64a638dced9515827ac95');
    form.selectRecord(thisRec)

    Great! But my need is to get TapForms to recognise that the current record is “selected” when I call a script from AppleScript. So I tried:

    var thisRecID = record.getId()
    var thisRec = form.getRecordWithId(thisRecID)
    form.selectRecord(thisRec)

    Running this script does nothing visible, of course, because it tells TF to go to the current record. My hope is that it would tell TF that the current record is indeed selected. But when I call this script from AppleScript, I get the same complaint in the console:

    “Record ID testing: TypeError: undefined is not an object (evaluating ‘record.getId’), column:23, line:1”

    I.e. it simply does not recognise “record” in line 1 as referring to the current record.

    Any more ideas?

    #37775
    Daniel Leu
    Participant

    I had a similar problem the other day and just wrote a small script that concatenates the content of a child field. The result is shown in the script field. Since this is only a helper field and I don’t want to clutter my form, I set the hidden flag on the script field.

    const wineryNameId = 'fld-3e98efea365847f3a2569700d679b4ed';
    var winesLinkId = 'fld-30dd9906b79649b6b54c01603928901d';
    
    function joinChildField(childLinkId, fieldId){
    	let entries = record.getFieldValue(childLinkId);
    	var list = Array();
    	for (entry of entries){
    		list.push(entry.getFieldValue(fieldId));
    	}
    	return list.join(', ');
    }
    
    joinChildField(winesLinkId, wineryNameId);

    In order for you to use this field script, you have to set the different field ids. wineryNameId is the field in the child form. winesLinkId is the linked field pointing to the child form.

    The operation of this script is very similar to what Sam posted. It is simpler as it doesn’t contain any logging features. The only thing you have to do is updating the two field ids.

    Obviously, it would be nice if search would go through child records, if requested. And then only show child entries that match.

    Cheers, Daniel

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

    #37772
    Brendan
    Keymaster

    Hi George,

    You’re right. Tap Forms does not search down multiple levels of relationships.

    But you can work around that by writing a Field Script which loops through the child records and builds a string that can be displayed and searched on the parent form.

    Use the Child Records Loop snippet to loop through your child records. Then pick out a value from the record to concatenate to a string. Return that value and then you will be able to search that string.

    function recordsLoop() {
    
    	var actors_id = 'fld-984cf79ccafb45a381b0f95b0aa28e78';
    	var last_name_id = 'fld-a907066570844290a27940d34de98b4f';
    	var actors = record.getFieldValue(actors_id);
    
    	var last_names = [];
    
    	for (var index = 0, count = actors.length; index < count; index++){
         	var last_name = actors[index].getFieldValue(last_name_id);
    		if (last_name) {
    			last_names.push(last_name);
    		}
    	}
    	return last_names.join();
    }
    
    recordsLoop();

    Probably not as convenient as what Air Table has. I just read an article in the BBC News that they’re worth a billion dollars now. So I suspect they have lots of engineers working on the app and can do some things that Tap Forms can’t do yet. I’m just 1 guy :)

    Thanks,

    Brendan

Viewing 15 results - 2,221 through 2,235 (of 2,950 total)