Learning JavaScript

Tagged: ,

Viewing 39 reply threads
  • Author
    Posts
  • October 12, 2022 at 8:22 PM #48103

    Mike Guilbault
    Participant

    Can someone recommend any books, videos, etc. to learn JavaScript, particularly in relation to Tap Forms 5 rather than web use?

    October 13, 2022 at 9:46 AM #48104

    Daniel Leu
    Participant

    Have a look at “Scripting in Tap Forms” on https://www.tapforms.com/support/. Sam Moffatt and T.L. Ford provide additional resources on using scripting with TapForms.

    October 13, 2022 at 9:49 AM #48105

    Mike Guilbault
    Participant

    I’ve read/viewed those… got a little out of them but looking for something more complete.

    October 28, 2022 at 6:05 AM #48149

    T.L. Ford
    Participant

    What kind of things are you envisioning doing?

    October 28, 2022 at 6:21 AM #48150

    Mike Guilbault
    Participant

    Nothing too elaborate. But I’d like to learn the basics in some detail so I can understand what can be done.

    October 28, 2022 at 10:58 AM #48153

    Brendan
    Keymaster

    There’s also a good JavaScript reference here:

    https://en.wikipedia.org/wiki/JavaScript_syntax

    January 12, 2023 at 2:40 PM #48614

    Glen Forister
    Participant

    Disappointed…
    I didn’t know I would have to spend a week or more to learn Javascript to add or subtract two fields. Spent the greater part of a summer recently learning “R”, but now I can barely get started again. Use it or lose it. I don’t want to spend a week to do a simple calc to get a form to do something and then a month or year late have to go through all that again. This isn’t simple…

    January 12, 2023 at 8:55 PM #48630

    Brendan
    Keymaster

    Hi Glen,

    Instead of JavaScript, you could also just use a Calculation field.

    In your previous JavaScript question, the solution to get the difference between two dates in months is this formula:

    MONTHS(Start Date; End Date)

    and that’s it!

    It’s not required to learn to program in JavaScript just to do simple things like that. That’s what the Calculation field is for. There’s lots of functions that’ll do all sorts of things for you without programming. You just have to know simple mathematical expressions.

    Thanks,

    Brendan

    January 12, 2023 at 8:56 PM #48631

    Brendan
    Keymaster

    There’s a list of available Calculation field functions here:

    https://www.tapforms.com/help-mac/5.3/en/topic/calculation

    January 12, 2023 at 10:56 PM #48633

    Glen Forister
    Participant

    Ah, now I have another hidden doc.
    I have to study that, but I didn’t see anywhere quickly, how do you reference the previous reord? IE:

    MONTHS(recordDate. previous recordDate).
    The number of months since the last record was entered looking at the date entered for each record?

    January 13, 2023 at 6:10 PM #48636

    Brendan
    Keymaster

    There’s no function in the Calculation field to reference the previous record. Calculations work only on the current record.

    You would probably have to use JavaScript to do that. But then, what really is the previous record? It really depends on how you sort your records.

    January 13, 2023 at 7:33 PM #48638

    Glen Forister
    Participant

    Sorting is key to this operation, you are right.

    I have several health situations I log into a database like pill taken, cortisone cream use and inches of rain fallen each day with a running total are a few. They have to used in Date sorted view so the next entry is the last. This keeps me from taking medicine before a certain amount of time since the last treatment and I stretch it as long as I can and I can run a report of min, max, and avg of times between treatments.

    Do you know how that could be made to happen?
    Thanks.

    January 13, 2023 at 9:59 PM #48641

    Brendan
    Keymaster

    You can get the previous record with this code:

    function Get_Previous_Record() {
    
    	var records = form.getRecords();
    	var currentRecordIndex = records.indexOf(record);
    	var previousRecord;
    	
    	if (currentRecordIndex > 0) {
    		previousRecord = records[currentRecordIndex - 1];
    	}
    	
    	if (previousRecord != undefined) {
    		// fetch some value and do some calculation with it and the current record
    		// the currently selected record is always accessed with the built-in variable
    		// record
    	}
    }
    
    Get_Previous_Record();
    • This reply was modified 1 year, 3 months ago by Brendan.
    • This reply was modified 1 year, 3 months ago by Brendan.
    • This reply was modified 1 year, 3 months ago by Brendan. Reason: Fixed a syntax error
    January 14, 2023 at 9:58 AM #48644

    Glen Forister
    Participant

    Thanks for the script, but it doesn’t do anything, I suppose be cause I have to define where to get the previous record and then add today’s total to it. I could probably spend the rest of the day working on this and not figure it out I’m sure.

    I’ve attached my file and maybe if you fix it I can apply the example to other similar forms I have to import data for.
    Thanks.

    Attachments:
    You must be logged in to view attached files.
    January 14, 2023 at 2:31 PM #48646

    Brendan
    Keymaster

    Hi Glen,

    You put that script code into a Calculation field.

    You need to put it into a Script field, not a Calculation field.

    January 14, 2023 at 2:38 PM #48648

    Daniel Leu
    Participant

    Hi Glen,

    Thank you for sharing your form! The script Brendan posted is for a field of the type Script and not Calculation.

    Here is the updated script that works for me:

    function Season_to_date() {
    	
    	var records = form.getRecords();
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousRecord = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
    
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();
    January 14, 2023 at 3:09 PM #48650

    Brendan
    Keymaster

    Thanks for posting your script Daniel. I had a couple syntax errors in mine which I updated to fix.

    I wonder if Glen wants a cumulative total and not just a total from the previous and today’s totals? If that’s the case, then maybe a Form level script to loop through all the records and update a cumulative total field would be required?

    January 14, 2023 at 3:41 PM #48651

    Glen Forister
    Participant

    I’m recording daily rain fall (morning, noon and night)I want each record to show the total for that day (done) and add that to the previous season total to get the new season total.
    each record has the total for the day and the season total

    I added the Script to the list of Scripts, then noticed the change of field properties to Script instead of Calculated and put the script there, but nothing happened.

    Am I supposed to change the script to enter the field somewhere?

    January 14, 2023 at 3:41 PM #48652

    Glen Forister
    Participant

    I’m recording daily rain fall (morning, noon and night)I want each record to show the total for that day (done) and add that to the previous season total to get the new season total.
    each record has the total for the day and the season total

    I added the Script to the list of Scripts, then noticed the change of field properties to Script instead of Calculated and put the script there, but nothing happened.

    Am I supposed to change the script to enter the field somewhere?

    Why a loop?

    January 14, 2023 at 3:47 PM #48654

    Glen Forister
    Participant

    That loop script doesn’t do anything either.

    January 14, 2023 at 4:14 PM #48655

    Daniel Leu
    Participant

    Sorry, this is intended to be a field script for the Season Tot field.

    And you need to run Refresh Records List (control-option R).

    • This reply was modified 1 year, 3 months ago by Daniel Leu.
    January 14, 2023 at 7:23 PM #48657

    Glen Forister
    Participant

    That didn’t quite work. Looping to get all every time? Here is the first 3 lines of the relult. This is one thing I like about programming, always a surprise.
    Day tot. Sum
    0.03 0
    0.31 0.310
    0.23 0.230.310
    0.33 0.330.230.310

    Brandon’s script results
    0.03 0
    0.31 0.310
    0.23 0.230.310
    0.33 0.330.230.310

    January 14, 2023 at 10:38 PM #48659

    Daniel Leu
    Participant

    It works for me. I like the chart view. Archive attached.

    Attachments:
    You must be logged in to view attached files.
    January 15, 2023 at 10:42 AM #48662

    Glen Forister
    Participant

    Well, that worked after deleting form and reinstalling with your archive.
    Entered in the new data.
    Wanted to see on my iPhone and after selecting “Table” so the date would be to the left of the data, not under the date (hard to read), the iPhone display now is completely black and I can’t find any way to display the data. The form list shows it has 17 records, but the iPhone is a blank screen.

    BTW, did you produce that graph in TF, or export to another prg. My previous database could produce graphs of the data with a touch, but I don’t see it here. Still haven’t carefully read, or remember all I’ve read of what documents I could find or were given to me. I must be still missing a lot. I know it took a long time for me to learn all the tricks of the other DB, so I’m not surprised here.

    Attachments:
    You must be logged in to view attached files.
    January 15, 2023 at 2:00 PM #48664

    Daniel Leu
    Participant

    Sorry, don’t know about the iPhone. I created the graph from within TF on my Mac. See attachment.

    Attachments:
    You must be logged in to view attached files.
    January 15, 2023 at 2:23 PM #48666

    Glen Forister
    Participant

    I figured that out. thanks.
    The file I used was for learning and had only this season. I thought it would be good to start with a small file.
    I tried to import the whole 5 years into the file, but import created a new file Form with the same name instead of adding to the current form. So I tried to duplicate the format for the new form but failed. I think I have all the file properties correct, but the script just gives me “NaN” in the field (even after repeated refresh). You have a clue what I missed, or can I copy records from this form and add them to the working form?

    BTW, somehow the Date col disappeared which produced bogus charts obviously. I added a new Field (DATE) and moved it to the top and after configuring the field the original data appeared out of nowhere. What happened here? I couldn’t find that data until it suddenly appeared, and how did it disappear in the first place?

    January 15, 2023 at 2:50 PM #48667

    Brendan
    Keymaster

    When you use the Import Records function, you can choose which form to import the records into. If you’re importing from a CSV or XLSX file. The default is to create a new form to import into. I suspect you didn’t select the form from the popup button that you wanted to import into.

    I’m not sure what happened with the date field. I’d have to have been there to see what you did.

    January 15, 2023 at 3:29 PM #48668

    Glen Forister
    Participant

    I missed that import setting.
    Thanks.
    I imported all 5 years of data and now when I create a filter and just view this season, which is 38 records, the script sees all the records, not just this season.

    Do I need to create another field to delete the total at the beginning of the season to find the total for the season, or is there a more elegant way?

    January 15, 2023 at 3:31 PM #48669

    Mike Guilbault
    Participant

    Unsubscribe

    January 15, 2023 at 10:52 PM #48670

    Daniel Leu
    Participant

    This should to the trick to reset the season total at the beginning of the year:

    function Season_to_date() {
    	
    	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('fld-0720a5baa6fc4980aee22798bd089714');
    	if (date.getMonth() == 0 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();
    January 16, 2023 at 10:13 AM #48671

    Glen Forister
    Participant

    Thanks for the script. Wish I could understand it, but…

    Actually, the beginning of each rainy season is Oct 1 of the previous year extending to Oct 1 of this year.

    The records in my current view is only those records in that range, but sometimes I look at previous years with a different view search pattern.

    Will this do the job with that parameter?

    January 16, 2023 at 1:13 PM #48674

    Daniel Leu
    Participant

    The script checks for January 1st and if there is match, the return value is set to 0.

    Here is the update for checking for October 1st:

    	var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
    	if (date.getMonth() == 9 && date.getDate() == 1){ // check for October 1st
    		return 0;
    	}
    	
    
    January 16, 2023 at 2:25 PM #48675

    Glen Forister
    Participant

    That is great. I put it in the script right after the other “var”s near the top. Hope that is the right place.

    function Get_Previous_Record() {

    var records = form.getRecords();
    var currentRecordIndex = records.indexOf(record);
    // added var for Oct 1st.
    var previousRecord = nil;
    var date = record.getFieldValue(‘fld-0720a5baa6fc4980aee22798bd089714’);
    if (date.getMonth() == 9 && date.getDate() == 1){ // check for October 1st
    return 0;
    }
    // end added var for Oct 1

    if (currentRecordIndex > 0) {
    previousRecord = records[currentRecordIndex – 1];
    }

    if (previousRecords != nil) {
    // fetch some value and do some calculation with it and the current record
    // the currently selected record is always accessed with the built-in variable
    // record
    }
    }

    Get_Previous_Record();

    January 16, 2023 at 2:50 PM #48677

    Glen Forister
    Participant

    Guess I put it in the wrong place. That didn’t work. Luckily I’m working on a copy so I won’t screw up the original.

    January 16, 2023 at 3:55 PM #48679

    Daniel Leu
    Participant
    function Season_to_date() {
    	
    	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('fld-0720a5baa6fc4980aee22798bd089714');
    	if (date.getMonth() == 9 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();
    
    January 16, 2023 at 5:31 PM #48680

    Glen Forister
    Participant

    Sorry. That isn’t doing it. Look at my file and see what isn’t working.

    I tried putting it in the “Scripts” tab, and in the Field/Season Total tab in Scripts.

    Thanks for trying again.

    Attachments:
    You must be logged in to view attached files.
    January 16, 2023 at 9:08 PM #48682

    Daniel Leu
    Participant

    Hmmm… your dataset doesn’t have an October 1st in there. So it doesn’t show that it works. At lest to me it seems to be working.

    If it still doesn’t work for you, did you do a “Refresh Record List”? Otherwise, please explain what’s not working.

    BTW, this is a field script as mentioned in https://www.tapforms.com/forums/topic/learning-javascript-2/#post-48648

    January 16, 2023 at 10:19 PM #48683

    Glen Forister
    Participant

    My data shows (the first 3 lines) the first rain of the season.
    On 11-1-22 had 0.31″ rain.
    So, 11-01-22 total for that day should be 0.31, not 80.95.
    date 6am. noon. 6pm. 10pm. day. season
    11/01/22 0.00 0.00 0.00 0.31 0.31 80.95
    11/01/22 0.00 0.03 0.00 0.00 0.03 80.98
    11/07/22 0.28 0.00 0.33 0.00 0.33 81.31

    I can’t make it change with a refresh ctl+opt+R or from the menu “View/Refresh”
    The screen is only showing the Season records “2022-2023” from years going back to 11-21-20218.

    Are you showing 11-01-22 starting at 0.31 for season total? If so, something is off here.

    January 16, 2023 at 11:55 PM #48684

    Daniel Leu
    Participant

    I started with a new form and reimported your previous form. There I see your odd numbers. But once I closed TF and reopened the form, did the refresh, the correct data showed up.

    January 17, 2023 at 1:03 AM #48685

    Glen Forister
    Participant

    I guess it is a lost cause on my end, because no matter how many times I do all that, or delete the code and reinstall it and shut down and refresh I can’t get it to work.

    Too bad too, because that is another strike against Tap Forms. If you sent me via email the archive that works, maybe it will work for me??? I don’t know what is missing. I have several other files of data that I want to get this to work on also.

    I’ve had a crash course in TF for all day for the last 4 days transferring 70 files from HanDBase and configuring them so they do what they used to do. For the most part they do work, but there are a lot that require a form on the iPhone to make them work so I have to keep HanDBase running just for those. Maybe I can simplify them enough to get them to work but it is doubtful. I do all my database work on my iPhone, not on the computer so the form on the computer is useless almost.

    Anyway, thanks for trying (I believe you are volunteering your expert help here) and if you think you can solve the problem using code, and want to keep trying I’m good to go.
    Thanks.

    February 23, 2023 at 7:17 PM #49020

    footlooseboss
    Participant

    for future readers as much as OP:

    Can someone recommend any books, videos, etc. to learn JavaScript, particularly in relation to Tap Forms 5 rather than web use?

    I used Khan Academy to teach myself further maths; they have a course on JavaScript. I found it better than a real tutor (pause + rewind). Probably start with a few modules here.

    I learnt two ways:
    – downloading the source of a website, modifying it, breaking it, fixing it
    – setting a goal of writing a script to do something useful on a webpage automatically – in my case it was logging into a booking website every day, finding available time slots, and booking the first slot before anyone else

    I can suggest tools but to start experimenting just enable the developer menu in Safari, and open inspector/console.

    Tap Forms API reference:
    tapforms.com/help-mac/5.3/en/topic/javascript-api

    Tap Forms scripting videos (fairly in-depth, probably don’t start here)
    pasamio YouTube

    edit: format

Viewing 39 reply threads

You must be logged in to reply to this topic.