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 - 631 through 645 (of 2,989 total)
  • Author
    Search Results
  • #48986

    In reply to: days between dates

    Glen Forister
    Participant

    Of course. How did I miss that.
    It does work.
    I have some work to do to understand the scripts you have given me, so thanks for that.

    Also, thanks for the calc info “In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.”

    I will need that in another Form I just used the above script on and it worked. I have to know the weeks and months, not just days.

    #48985

    In reply to: days between dates

    Brendan
    Keymaster

    You need to put in your own field ID instead of fld-xxx as in Daniel’s sample script. He doesn’t know what your field ID is, so we often just put in stand-ins for a field ID. It’s something you have to substitute yourself when you see any code here. Field IDs are unique to your own forms.

    #48976
    Sam Moffatt
    Participant

    Those of you who have been looking at my YouTube might have seen the video talking about Managed Fields and I’m finally ready to show it to the world. This is brand new, probably has a few bugs and definitely isn’t as battle tested as some of the other code I’ve written as I had a more immediate need to leverage it. It’s already up to 1.0.1 because I found a bug with the log feature (I forgot to include the record ID! Doh!) and also wanted to include my old setIfEmpty feature as a part of managed fields as well making it easy to only update fields whose values aren’t already set in addition to a couple of other options.

    Those curious might also be interested in the VOD of Twitch stream I did which has some more development included though has some distractions as well. You can see in real time just how slow building some of this code up is though I must apologise the audio quality ended up quite poor.

    A big part of Managed Fields are two accompanying forms in addition to the Managed Field script library. Check out the attachment to this post (you’ll need an account to download) and import it to your record alongside the original Script Manager. I’ve also started to add some Markdown documentation and you can see an example with the addToTableIfMissing. I’m slowly adding documentation for various things as I come back to using them and forget how they work so hopefully also sharing. At some point I hope to leverage the Markdown feature in Tap Forms to include these inside the database so the documentation is always available…even if you’re working on your Tap Forms document on the plane!

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

    In reply to: days between dates

    Daniel Leu
    Participant

    I got several errors when I tried to run your code. This touched-up version works for me:

    var date_id = 'fld-xxx';
    
    function Days_Between_Dates(){
    	console.log("Days_Between_Dates() called ...");
    
    	var days = -1;
    	var records = form.getRecords(); // calls function getRecords()
    	var currentRecordIndex = records.indexOf(record);
    
    	if (currentRecordIndex > 0) {
    		var previousRecord = records[currentRecordIndex-1];
    	} else {
    		return -1;
    	}
    
    	// get the two dates
    	const today = record.getFieldValue(date_id);
    	const previous_day = previousRecord.getFieldValue(date_id);
    	
    	// calculate the difference between the two date objects
    	days = Math.round((today - previous_day) / (1000 * 60 * 60 * 24));
    
    	console.log("today: " + today)
    	console.log("previous day: " + previous_day)
    	console.log("difference: " + days)
    	
    	return days;
    }
    
    Days_Between_Dates()

    In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.

    I changed the return value to -1 for the first record.

    When reporting a syntax error, it’s helpful to have the entire message.

    When posting code, please encapsulate it in back-ticks. This way it is easier to copy&paste it and it looks prettier. Thanks!

    Cheers & happy hacking!

    • This reply was modified 2 years, 11 months ago by Daniel Leu.

    Cheers, Daniel

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

    #48968

    In reply to: days between dates

    Brendan
    Keymaster

    I see what you did. You copied Daniel’s code and put it inside your script after you had already defined the function.

    What he meant by this part:

    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
       ...
    }

    Was take your existing script and just below the function declaration, put in the console.log() call. Not duplicate the function definition again.

    Then after your function definition, actually call the Days_Between_Dates() function which your original script did not show you were doing.

    #48965

    In reply to: days between dates

    Brendan
    Keymaster

    I’m a bit confused with your script. Did you define Days_Between_Dates() twice?

    Or was that a typo in your post?

    I also edited your post to put the back-ticks in so your code was easier to read.

    • This reply was modified 2 years, 11 months ago by Brendan.
    #48959

    In reply to: days between dates

    Daniel Leu
    Participant

    Do you have a Days_Between_Dates(); at the end of the script after the closing bracket of the code you showed? This causes your function to be called.

    If you run this script on the first record, it will not do anything since it is the first record.

    Sometimes I add something like this to my functions:

    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
       ...
    }
    
    Days_Between_Dates();

    This way I can easier trace what’s going on with my scripts.

    Cheers, Daniel

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

    #48954

    In reply to: days between dates

    Glen Forister
    Participant

    Ok, missed that.
    But I got all the syntax cleared and this script does nothing, not even printing to console the variable values. So I’ve proved I haven’t learned anything yet.
    Any ideas?
    Thanks.

    
    function Days_Between_Dates(){
    	var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
    
    	
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        
        
        console.log("today: ")
    	console.log("previousday: ")
    	console.log("total: ")
        
        return total;
    }
    
    
    • This reply was modified 2 years, 11 months ago by Glen Forister.
    #48953

    In reply to: Restore a form?

    Glen Forister
    Participant

    The record isn’t important enough to warrant work. But the next problem might be.

    BUT, the fact that all those backups I have are useless unless I want to replace all my Forms and lose all the data in any or all 67 Forms since the last backup is appalling. Also, I guess I’ll never compact them again since I’ll never know when something might happen and I need that deleted information. I had compacted it because I had been doing some experimenting with Scripts and other features and after the session I was deleting records and Forms.

    The only way to have a backup is to archive all 67 Forms individually? That is ridiculous.

    Also, where is the option to engage a window “Are you sure you want to delete this record” with a yes or no/cancel option is almost unheard of in programs.

    Disappointed… In the past with my other database program there were many times I had to resort to a backup file and replace the original with that file. That program backed up each “Form” into a separate file and was a real backup. Sure, it took longer to sync and backup, but it worked and your system doesn’t.

    #48940
    Daniel Leu
    Participant

    As I mentioned before, the return type of the script needs to be set to number.

    • This reply was modified 2 years, 11 months ago by Daniel Leu.

    Cheers, Daniel

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

    #48939
    Glen Forister
    Participant

    I thought I was done. I was double checking that Script field ID, and wondered why it didn’t show up in the script editor and then forgot about that.

    I hit refresh and now I have the totals incrementing, but instead of adding the previous total to the current number, it is concatenating them.
    One of the things I love about programming is the surprises you don’t expect because of flawed logic…
    0 0
    50 500
    100 100500
    100 100100500
    100 100100100500
    20 20100100100500
    60 6020100100100500

    #48938
    Glen Forister
    Participant

    I didn’t know it was there. I’ve been using the Script Editor to get those inserted.
    Thanks.

    I appreciate the way you organized it. That is a lot easier to read.

    I’m having trouble reading and understanding the objects, etc. and have to study a bit more to understand it. When done I’ll attempt another use for the code finding out how many days between the date of the last treatment of the previous entry and the date of the current entry and record that in a Days between treatments.

    I have other work to get done first though. Even though I’m 79, I can’t get enough done with my insect collection and prepare for another season of collecting. Being retired is a lot of work.

    #48936
    Daniel Leu
    Participant

    Great that it works for you too!

    Yeah, to make it easier I consolidated all the field IDs at the top. To get the total_id, which is the ID of the script field, you need to close the script editor and look at the form editor. See attachment. You can get all the other IDs from the left side panel in the script editor.

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

    Cheers, Daniel

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

    #48935
    Glen Forister
    Participant

    While working with it I noticed the IDs were wrong also as you pointed out. Don’t know how that happened.

    I’ll have to compare this script with the previous one to see what you did, but with the changes you mentioned in the program and your last script, it worked.

    Thanks for the work. Hopefully I can transfer this to other forms and make it work there also.

    #48934
    Daniel Leu
    Participant

    You need to select ‘script’ and not ‘calculation’ for the field type, return type is ‘number’.

    The script had some errors too like incorrect field ids.

    
    function Season_to_date() {
    
    	var date_id = 'fld-e2d20da877cb4a6c8fd72153b86f1ab1';
    	var donation_id = 'fld-05c429d6a4484061b554aa584a45f8fc';
    	var total_id = 'fld-7c6b9bb288ab47e0b295af6eacc9cd26';
    	
    	var records = form.getRecords();
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousRecord = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
    
    	// Is this the beginning of the year?
    	var date = record.getFieldValue(date_id);
    	if (date.getMonth() == 0 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue(donation_id);
    	var previous_total = previousRecord.getFieldValue(total_id);
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();

    Cheers, Daniel

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

Viewing 15 results - 631 through 645 (of 2,989 total)