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,351 through 1,365 (of 3,102 total)
  • Author
    Search Results
  • #45006
    Tom Kerswill
    Participant

    Thanks so much for the help. I ended up writing something based on your great Script Manager, Sam, and then building out something to automatically add the fields (if they’re not already there).

    It works really well. Hooking into the existing CSV / Json import would be lovely; but doing it with a script isn’t too bad in the end.

    Here’s the snippet:

    		let targetRecord = 	getRecordFromFormWithKey(form.name,indexKeyId, rowId);
    		if (!targetRecord) {
    			console.log("Adding new record...");
    			targetRecord = form.addNewRecord();
    		}
    		console.log("Record: " + targetRecord.getId());
    		for (const [key, value] of Object.entries(row)) {  			
    let keyField = form.getFieldNamed(key);
    			if (!keyField) {				
    				console.log("Adding field: " + key + "Type: " + typeof(value));
    				if (typeof(value) == "number") {
    				fieldType = "number";
    				}
    				else {
    				fieldType = "text"
    				}
    				keyField = form.addNewFieldNamedWithType(key,fieldType);
    				form.saveAllChanges();
    			}
    			keyId = keyField.getId();
      			console.log("Key: " + key + " keyId: " + keyId + " Value: " + value);
      			targetRecord.setFieldValue(keyId, value);
    		}
    

    Tom

    #44967
    Brendan
    Keymaster

    And with setting the Script Folder on the Preferences screen, you can put your data in there and then use var data = Utils.getTextFromUrl(url); command to read in the file. Then parse it. The split() function could be used to split the lines into an array of lines. Then for each line, use split again to split by the column delimiter. But it really depends on how your data is structured. If you have delimiters within your content and quoted content, then that would be much more difficult to deal with. I should add a JavaScript API for parsing delimited content into two dimensional arrays of data, basically giving you access to the code I already use for parsing CSV files.

    #44938
    Daniel Leu
    Participant

    I don’t see an API for that. In one of my applications, I use the clipboard to import data into TF. It took a bit of time to get the data parsing right, but now it works well.

    If your data is already in CSV format, the parsing should be easy. I don’t think that such a script is that difficult since you already know where the data should go. You then call the script with tapformz://script/[document ID]/[form ID]/[form script name]?key1=value1&key2=value2...

    Cheers, Daniel

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

    #44934
    Tom Kerswill
    Participant

    Hi

    Is it possible to import data programatically? Ideally by using an existing import preset?

    I’d like to have something like Hazel or Keyboard Maestro trigger this when data in a file changes, so that I can reimport it into Tap Forms.

    I suspect I could probably do a form script that might do this; but ideally it would be able to trigger Tap Forms’ existing import system, as it can then do things like automatically setting links to other forms, etc., whereas I suspect this would be quite complex to do if building an import script from scratch.

    Thanks in advance,

    Tom

    #44915
    Sam Moffatt
    Participant

    You can use a script to get the children of one Link to Form field of JOIN type and then add them to another Link to Form field. In terms of script, a form script like this should do the trick:

    record.getFieldValue('fld-joinfieldid').forEach(rec => record.addRecordToField(rec, 'fld-1tomanyfieldid'));
    

    You could expand it to run on all of the records in your form but this will do just the currently selected record. I haven’t tested it but it should work.

    #44910
    Aaron Greenhouse
    Participant

    With regards to the import problem, I was importing into a new empty document.

    The problem I have with the Join Link type is that I created a new database for purchases and items after date X, and in that database I have used the 1 to many link type and set the links explicitly. I would like to import my old data from the spreadsheet (from before date X) into the new database. I cannot have both JOIN and 1-to-Many for the same field. I suppose I could have two fields, “Purchase” and “Legacy Purchase.” I could live with that. Is there a way, perhaps using a script, to copy the references from a “JOIN link” field to a 1-to-Many field?

    #44902
    Sam Moffatt
    Participant

    The other two numbers round correctly to the right value:

    0.5600000000000001
    0.560000000000000053290705182007513940334320068359375
    0.5500000000000000444089209850062616169452667236328125
    0.56999999999999995115018691649311222136020660400390625
    

    The script field is probably the answer if you really want to fix it, I don’t think there is a way within a calc field you can make it work when embedded in a string like that.

    #44882
    Sam Moffatt
    Participant

    What is the type of current year rate? I was trying to figure out how to repro this to figure out a viable solution but I didn’t see one. I create a number field and populated it with 0.56 and the text calculation field printed .56 as the rate as I’d expect. I tried doing it as a calculation field with 56/100 but that didn’t seem to trigger the weird behaviour either.

    What you’re seeing is floating point rounding errors. The 0.56 ends up being stored as 0.560000000000000053290705182007513940334320068359375 which is why you see 0.5600000000000001 as the 5 rounds up:

    0.5600000000000001
    0.560000000000000053290705182007513940334320068359375
    

    I suspect this is a reflection of the underlying JSON data model (Javascript floating point numbers are IEEE-754 64-bit double precision numbers) and unfortunately leaking out. For some more gory details I found a YouTube video on floating point representation and rounding error and an online converter that lets you put in 0.56 and see what the computer is doing.

    I have an sprintf implementation that I ported for printing formatted strings in Javascript that should handle it if you want, there was a not dissimilar thread on if to use a calc or script field that shows formatting floating point numbers. That may yet be the solution for you as well but it’s a bit heavier than a simple calc field.

    #44873

    In reply to: Link to dates in form

    Brendan
    Keymaster

    I wonder if a Table field would be ideal for this sort of thing?

    Do you mean that if you change a date on the Parent record, all the linked records would have their dates updated? So if the parent field had a Project Start Date and the Table field had a Task Start Date, if you change the Project Start Date to something later, the Task Start Date for each task would also change?

    If so, then a Script field would certainly be able to do this. Just loop through all the Table field records and update the Task Start Date depending on the Project Start Date.

    I don’t know if it would be all that difficult. And doesn’t use a Linked form, although a Table field is similar to a Link to Form field in the way it displays. It just doesn’t have a separate form for it. The sub-fields are all configured on the Table field itself.

    But maybe I’m misunderstanding the requirement.

    Thanks,

    Brendan

    #44870

    In reply to: Link to dates in form

    Sam Moffatt
    Participant

    I don’t think there is an easy way to do this, or even a hard way for that matter.

    The feature you’d need is self linking forms and whilst it sort of works, it only does so for M:M links when for this you’re building a tree like structure that would be 1:M. I got something sort of working with two link to form fields but it was janky and didn’t quite work the way I’d like and would still put the parent record in the child section. That would mean a second form to store the links and likely a bunch of scripting to replicate the TF record picker. Not impossible and with a lot of work, you could build the linking infrastructure you’d need to make it work and then once you’ve got that a moderately complex script field could push the update.

    #44868

    In reply to: what is wrong

    Sam Moffatt
    Participant

    As a script it’d be something like this:

    function Flag() {
        var classtype = record.getFieldValue("fld-fieldid1");
        var yps = record.getFieldValue("fld-fieldid2");
    
        return (classtype == "Std" && yps > 3.64) ? "" : "?";
    
    }
    
    return Flag();
    

    Note: class is a reserved word in Javascript so not a valid variable name.

    The fld-fieldid1 is the ID of the field, the script editor can help you generate a line for that (double click field on desktop or on mobile there is a field picker) with the variable name and field ID preset. You can also get the field ID from the form editor when you select the field to splice in the ID yourself.

    You don’t need the full function wrapper but I find it feels more natural to do the return statement. The return is using a ternary operator to do the comparison compactly.

    #44866

    In reply to: what is wrong

    Tim Flick
    Participant

    What this same calculated field look like in a script Thanks much

    #44864

    In reply to: Display GrappObject

    Sam Moffatt
    Participant

    Script field is Javascript so anything you can code into Javascript you can put as a condition. It’s a bit more verbose than the calculation fields but I feel a lot more flexible and expressive plus the development loop is a little easier as you can run it inside the editor and see the result (or get an error on the console from the JS interpreter).

    For each field, you need to do a record.getFieldValue to grab the value and then you can do what ever operation you need (obviously limited by Javascript).

    #44852

    In reply to: Display GrappObject

    Sam Moffatt
    Participant

    On the emoji front, I went looking for something that could automatically take the country and turn it into a flag, I found some sample code that used the country code to turn into the flag. I pulled that into a script and it seemed to work out ok:

    function getFlagEmoji(countryCode) {
      return countryCode.toUpperCase().replace(/./g, char => 
          String.fromCodePoint(127397 + char.charCodeAt())
      );
    }
    
    function Flag() {
    	var country = record.getFieldValue('fld-7769fdcb2a4f46a8a225db562a289359');
    	return getFlagEmoji(country);
    }
    
    Flag();
    

    Replace the field ID with your field ID, if you want to do some extra translations or mappings then you can do that in script but if you do some data wrangling this won’t be too bad. I grabbed the country code list from W3Schools and pasted it into a new form with the above script via the MCLV and it’s attached below as a Tap Forms Archive.

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

    In reply to: Display GrappObject

    Brendan
    Keymaster

    Hi Tim,

    You could maybe do something like that in a Script, but use Emoji’s for the flag instead of a graphic object. Although I guess depending on what you’re trying to do, just a Calculation field that looks at a value from a field and returns an Emoji flag character depending on the result.

    Something like:

    IFEQUAL(Country; "Canada"; "??"; "")

    So this says, if the value of the Country field is “Canada”, return the Canadian Flag emoji, otherwise, return nothing.

    Thanks,

    Brendan

Viewing 15 results - 1,351 through 1,365 (of 3,102 total)