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,126 through 1,140 (of 2,989 total)
  • Author
    Search Results
  • #45847
    Steven Daniel
    Participant

    Wow – way more complicated and involved than I expected. So I presumably made an error somewhere – please see attached scripts – both show same error message “can’t find variable entries” on line 23. The only difference in the two scripts is I added the last bit of code you suggested to be able to add other synonyms down the road in one, without it in the other.

    I kind of, at a very basic level, get what you’re doing here, but really need to get my head around this scripting more. And really appreciate the help. I thought it would be something relatively simple like if Plant_ID (Atlas) = Plant_ID (Synonyms) enter Scientific_Name (from Synonyms) into the new Atlas Synonym field. Oh well – one step at a time. Thanks again!

    Can you see what went wrong and how to fix it?

    Attachments:
    You must be logged in to view attached files.
    #45846
    Sam Moffatt
    Participant

    Not quite copy paste but leveraging TF to do a lot of heavy lifting here.

    If everything is keyed off plant_ID, create a new “Link to Form” field in the atlas and link it to your synonym form and plant ID fields. That should automatically create links to the synonym records as either new synonyms are added or new plants are added.

    If you’re going to do it all programmatically, I’d swap the type of your synonym field from a “text” field to be a “script” field. Double click on the field name to open the script editor, delete the boilerplate code, select the “Scientific_Name” field on the left on the field list (it should show up once you create the “Link to Form” JOIN field) and then on the bottom left in the “Snippets” it should have “child records loop”. It’ll insert a new function called recordsLoop for you and it’ll have a line that says record.getFieldValue and if statement in the middle of it. After the line that was record.getFieldValue, put in a line like the following:

    var entries = [];
    

    My guess it should look something like this:

    	var atlas_synonyms = record.getFieldValue(atlas_synonyms_id);
    	var entries = [];
    

    We’re going to update the if statement to look something like this:

    		if (scientific_name) {
    			entries.push(scientific_name);
    		}
    

    Lastly there will be a line that says return;, update it to say return entries.join(' '); to get a space separated list of synonyms or return entries.join(', '); to have a comma separated list.

    If you want to keep the synonym field as a separate text field so you can add other values, then you’ll need to do this instead of the return:

    record.setFieldValue('fld-synonymfieldid', entries.join(' '));
    document.saveAllChanges();

    Usually I guard this with a check for if that field has a value already so that it doesn’t overwrite existing values automatically, putting this at the top of the function:

    function recordsLoop() {
    	if (record.getFieldValue('fld-synonymfieldid')) {
    		return;
    	}
    

    This prevents the script from overwriting the text field if there is a value in it already. Double clicking on the field name in the left on Tap Forms should populate the record.getFieldValue code for you (remove any var blah = bit).

    Hope this helps! Not quite copy/paste but hopefully useful enough instructions to get it sorted.

    #45844
    Sam Moffatt
    Participant

    Q1: Yes, this is a three field setup. One text field to import the date as is, a second “date” field to store the canonical representation and a third script field that watches the text field and mutates it to the date field.

    Q2: Swap this line around:

    	var parsedDate = new Date(pieces[2], pieces[0] - 1, pieces[1]);
    

    becomes:

    	var parsedDate = new Date(pieces[2], pieces[1] - 1, pieces[0]);
    

    The new Date() accepts a year, monthIndex (0 to 11, hence the -1) and then day, so we just need to flip the order to match.

    Q3: You can add a new field to land just the imported data and keep your existing date field. That shouldn’t mess up any layout, you’re just adding a text field to land data in during import. The field itself can be hidden most of the time, or if using custom layouts just not shown. Same deal with the script field or you could achieve something similar with a form script. I perhaps err on the side of caution that it’s usually more annoying to detangle data after you’ve imported and mangled it. You could do it inline with the import parse using the same code to convert a passed date string into a JS Date object:

    function parseDate(dateString) {
    	let pieces = dateString.split("/");
    	return new Date(pieces[2], pieces[1] - 1, pieces[0]);
    }
    

    The above assuming UK DD/MM/YYYY format already.

    #45840
    Steven Daniel
    Participant

    Hi – new to Tap Forms and no programming experience. But it seems like it should be simple to accomplish the following:

    I have a form that I added a new text field (synonym). I want to populate all the records in this field with the same value in the related field (species) when the field plant_ID (a number field) value in matches the same plant_ID field in a different form.

    I’m not sure I’m using the correct language. In the first screenshot here I have the form (Synonyms) with just two fields. In the next screenshot I have the new blank Synonym field, that I want to fill with the proper name when the plant_ID matches. So when the plant_ID = 66 it should fill Rhus toxicodendron in the new synonym field in the Atlas form.

    A simple script that I can cut and paste would be appreciated.

    One possible snag – sometimes there are multiple synonyms for the same species. Would it be possible to insert each synonym into the same field? Or how best to handle that.

    Thanks!

    Attachments:
    You must be logged in to view attached files.
    #45839
    Victor Warner
    Participant

    Sam,

    Thank you for the reply.

    **Query 1**.
    Does you code require the date data first to be imported as a text field, and then the script, in another field, with the script attached to the field to format the date?

    **Query 2**.
    How would the script you provided need to be adapted for a non-USA (UK) format?

    **Query 3**.
    More fundamentally, the database I have in Tap Forms contras a lot more Forms – and has many layouts based on and use existing dates – so I am reluctant to engage in a lot of restructuring to add more fields.

    Either to do some processing of the dates in the script you originally provided or outside of Tap Forms – e.g. using a Keyboard Maestro macro to do so.

    The backstory is that I am exporting several hundred records from FileMaker, each record (in Tap Form forms speak) consisting of several forms. I have to do this record by record – as the forms need to be linked together in Tap Forms – and have to be incorporate some manual data which is still recorded on paper.

    Using a script in importing will speed that part of the process – the way I have been doing this until know is to use the File / Import / Records in Tap Forms and automating the process through Keyboard Maestro – but for 6 or 7 forms for each record it take several minutes – and sometimes does not work (if the computer slows down the appearance of a window or keystrokes etc are delayed in appear, etc…).

    #45836
    Sam Moffatt
    Participant

    There are a few posts on Keyboard Maestro and AppleScript on the forum that you might be able to check into for inspiration. Maybe even Automator might help depending on precisely what your use case is.

    If you’re looking to do proper automation, I’d suggest setting up CouchDB and using the sync to that to automate from. I’ve got a set of PHP scripts that provide various mechanisms for grabbing data from CouchDB and putting it elsewhere.

    #45835
    Sam Moffatt
    Participant

    I’ve got a video on using Shortcuts to create a record from a scanned barcode which could be adapted to do something similar with an additional Utils.openUrl(newRecord.getUrl()) to open up the record after it’s been created (and obviously without the barcode scanning portion). If you set this as a shortcut on your home screen somewhere using the Shortcuts widget then you can launch into it and land more or less directly in the form. One caveat is that some times from cold load Tap Forms might load the wrong document or as it loads the right document sometimes doesn’t seem to execute the script. For my own use case I usually just swipe back and re-run the shortcut to get it all to work.

    #45833
    Prashant M
    Participant

    Is there a AppleScript or otherwise(since I couldn’t find in TapForms) to automate exports of records for a given form / database?

    I would be attempting Keyboard Maestro to achieve the same but will love if someone has already solved this problem.

    • This topic was modified 4 years, 1 month ago by Prashant M.
    #45830
    Sam Moffatt
    Participant

    Dates are always a pain. Javascript wants your dates to be in a particular non-ambiguous format when parsing them.

    Personally I’d actually suggest landing it in a normal text field so that you have the original value and then using a script field to parse the value of the field into a date field.

    For a parsing script for US style dates, it looks like this:

    function Date_Parser() {
    	var date_import = record.getFieldValue('fld-3e35e177e6174b139d3c4c8c2eea08d0');
    	var date_parsed = record.getFieldValue('fld-96b53c5f607e4e7286fad5448ceae477');
    
    	// if the date_parsed field is set, don't reset it.
    	if (date_parsed) {
    		return;
    	}
    	
    	var pieces = date_import.split("/");
    	var parsedDate = new Date(pieces[2], pieces[0] - 1, pieces[1]);
    	
    	record.setFieldValue('fld-96b53c5f607e4e7286fad5448ceae477', parsedDate);
    	document.saveAllChanges();
    	return parsedDate;
    }
    
    Date_Parser();
    
    #45828
    Victor Warner
    Participant

    Brendan

    Thank you for the response. Apologies in advance, but given my basic (almost non-existent) understanding of JavaScript, how do I incorporate into the script?

    // this imports the Papa Parse script
    form.runScriptNamed('PapaParse');
    
    // replace with your field ID's
    
    // Passport
    
    var passport_number_id = 'fld-9fe227057cdf44bfa62ad8a97cc6a62a';
    var nationality_id = 'fld-0039b290b2054881ac8f004c01903c6f';
    var country_id = 'fld-6e861fab76b9457bb625953cece54c96';
    var date_of_issue_id = 'fld-0154d8f9ce384e708502fdd775d7bfb1';
    var date_of_expiry_id = 'fld-df90736c929549cf8b863666077937fe';
    var issuing_authority_id = 'fld-d03c8c1e5fe64e4dada673cb4a6ed322';
    
    function Import_Passport() {
    	let filename = "file:///Users/victor/Desktop/Passport - test.csv";
    	
    
    	
    	let csvFile = Utils.getTextFromUrl(filename);
    	
    	if (!csvFile) {
    		console.log("No CSV file?");
    		return
    	}
    	
    	var output = Papa.parse(csvFile);
    
    	// abort if there are any errors and log to console.
    	if (output.errors.length > 0) {
    		console.log(errors.join("\n"));
    		return;
    	}
    
    	// read each line
    	for (let line of output.data) {
    		//var newRecord = document.getFormNamed("Passport").addNewRecord();
    		
    		//document.getFormNamed("Other Form").addNewRecord()
    		
    		var newRecord = document.getFormNamed("Passport").addNewRecord();
    		newRecord.setFieldValues({
    			[passport_number_id]: line[0],
    			[nationality_id]: line[1],
    			[country_id]: line[2],
    			[date_of_issue_id]: line[3],
    			[date_of_expiry_id]: line[4],
    			[issuing_authority_id]: line[5],
    		});
    		document.saveAllChanges();
    	}
    }
    
    Import_Passport()
    
    #45821
    Victor Warner
    Participant

    Sam Moffat provided the code to import a CSV file into a Form: https://www.tapforms.com/forums/forum/script-talk/.

    I have adapted the script but dates are not being imported. The dates are in the format: “01/01/2020” (day/month/year). The data is imported, but the dates are skipped.

    The database / forms are attached and the csv file.

    Is it necessary to add something to the Javascript to get the dates to import?

    Attachments:
    You must be logged in to view attached files.
    #45811
    Sam Moffatt
    Participant

    Oh good catch, let me update the video description as well. I used Google to grab the link and didn’t realise it was the older version.

    #45807
    Brendan
    Keymaster

    Hi Jean-Christophe,

    You would just set the all_day parameter to false and set the Date to have a specific time.

    https://www.w3schools.com/jsref/jsref_sethours.asp

    function Add_And_Update_Event() {
    
    	var event_info = {"calendar_name" : "Work",
    			"title" : "Test Event 1",
    			"location" : "Big Ben, London, UK",
    			"notes" : "This is a note",
    			"url" : "https://www.tapforms.com",
    			"all_day" : false};
    
    	var start_date = new Date();
    
            // Set the time to 15:30:00
            start_date.setHours(15, 30, 0);
    
    	// Create end Date instance 1 hour later
    	var end_date = new Date();
            end_date.setHours(16, 30, 0);
    
    	var identifier = Utils.addToCalendar(event_info, start_date, end_date);
    	console.log(identifier);
    	event_info["title"] = "Hello Event 2";
    	Utils.updateCalendarEvent(identifier, event_info, start_date, end_date);
    
    }
    
    Add_And_Update_Event();

    You can copy this code into a new Form Script to test it out.

    #45801

    Hello,
    I would like to add/update an event with hour (not all day) from two fields, one for the date, one for the hour.
    I’m new in scripting and I can’t find an example with hour included.
    Thanks for your help !

    Sam Moffatt
    Participant

    On Twitter there was an ask to have a field decrease in value based on the action in another form. I use scripting and model a transaction log style interface to not only keep balances but record the transactions as they happen for accountability (where did my yarn balls go?).

    In Part 1 I work through the initial structure and layout of the forms as well as creating our first script to set the balance for a yarn ball. Part 2 covers creating a “Library” script and the work needed to handle adding and removing entries whilst maintaining the transaction log. This then gets applied to the “products” form so that we can see how to use the bill of materials to reduce the balances whilst maintaining a log of what happened.

    Attached is an export of the Yarn Balls form template which should contain the forms, fields and scripts covered in the videos.

    If there is interest I can go through and do a third part with some quality of life improvements, adding some extra validation and potentially creating another form with produced items automatically linked together.

    Attachments:
    You must be logged in to view attached files.
Viewing 15 results - 1,126 through 1,140 (of 2,989 total)