Search Results for 'script'
Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Search › Search Results for 'script'
-
AuthorSearch Results
-
September 10, 2021 at 2:08 AM #45237
In reply to: TIME & DATE FIELD NOT WORKING
pinkcadillac
ParticipantWow Sam, that was the missing bit and it works perfectly and is far quicker to click “Now” than to type out the current date and exact time.
Many, many thanks.
I’ve no idea about scripts but this fixed it for me.
I’ll retain both my old Date field (using it’s calendar accessory) for historic orders and use the new T&D with it’s calendar from now on.
My Sorting is sorted too!
Great – thank you everyone!
PS is there a link to a file/doc/record which has all the possible TF fields and can it be added to my TF?
September 10, 2021 at 1:36 AM #45236In reply to: TIME & DATE FIELD NOT WORKING
Sam Moffatt
ParticipantEnable “show accessory controls” on the layout panel for your layout to see the calendar icon.
You could probably use a quick form script to copy the date across to your new field if you want to use it exclusively.
September 8, 2021 at 1:27 PM #45207In reply to: Help with script
Daniel Leu
ParticipantIn Javascript, comparison is
==not=. So you should useif (group==1) { record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 20); }-
This reply was modified 4 years, 2 months ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 8, 2021 at 12:26 PM #45204In reply to: Help with script
Guillermo q
ParticipantThank you for your awesome work and help!! This is amazing, it worked!
I have another question. I have a field (price) that has a fixed value depending on the input of another field (group).
group 1= 20
group 2= 40
group 3 = 54I tried with my absolute ignorance of programming creating a field with a script.
function pricetest() { var group = record.getFieldValue('fld-9565e24e59c6412ca388144d04e0bd07'); if (group=1) { record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 20); } if (group=2) { record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 40); } if (group=3) { record.setFieldValue('fld-e59ea447a29b4c518bd307507e243f92', 54); } } Pricetest();Any help? Thank you so much.
-
This reply was modified 4 years, 2 months ago by
Guillermo q.
September 7, 2021 at 6:18 PM #45198In reply to: Help with script
Sam Moffatt
ParticipantIf you’re on iOS/iPadOS, I’d just copy the values out and paste into Numbers (Apple’s spreadsheeting tool) or Excel (free for phones, I think a charge exists on iPads). If you change the console output to use commas, it should paste fine.
A small change could also directly put this on your pasteboard:
var lines = []; // log to console the aggregated values. for (var month in rollups) { var line = month + "," + rollups[month] console.log(line); lines.push(line); } Utils.copyTextToClipboard(lines.join("\n"));The Mac version of Tap Forms has some charting functionality, I’m not entirely sure if it supports aggregation or not but that would be the first place I’d look. Supporting aggregation seems like a reasonable feature ask if it doesn’t exist but that can only be answered by the Keymaster.
If you’re on the Mac, then an alternative could be to just write it to a new form and then chart it. To do this is going to be a bit more of a rewrite so that we can sort things easier
function Aggregate_By_Date() { // this is where we're going to store the rollups per day. var rollups = {}; // iterate to all of the records in the form for (var rec of form.getRecords()) { var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd'); var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07')); // Skip entries that don't have a price or date set. if (!price || !purchase_date) { continue; } // format the date for use in the rollup. var formattedDate = purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay(); // Rollup to this date, add to the existing value or set it if not set. rollups[formattedDate] ? rollups[formattedDate] += price : rollups[formattedDate] = price; } // Get the rollups form. var rollupsForm = document.getFormNamed("Rollups"); // Delete previous roll up records. rollupsForm.getRecords().forEach(rollupRec => rollupsForm.deleteRecord(rollupRec)); var lines = []; // log to console the aggregated values. for (var month in rollups) { var line = month + "," + rollups[month] console.log(line); lines.push(line); var rollupRec = rollupsForm.addNewRecord(); rollupRec.setFieldValue("fld-cd1d454672c84bce8103a4267507ca03", month); rollupRec.setFieldValue("fld-9eeeff7120db401b830ccec4e06f2bc3", rollups[month]); } document.saveAllChanges(); Utils.copyTextToClipboard(lines.join("\n")); } Aggregate_By_Date();Change to match the form name and field IDs but that should populate a new form with the records in the right format. Then you can use the chart feature on the Mac to visualise it.
Major changes in the full script, the formatted date is done by getting each value out:
// format the date for use in the rollup. var formattedDate = purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay();This produces a format that is easier to sort on but not zero padded, doing so would require grabbing the
sprintfimplementation via the Script Manager so I’ve left it out for simplicity sake.Next big change is our loop, I’ve incorporated the above pasteboard change but the core is as follows:
// Get the rollups form. var rollupsForm = document.getFormNamed("Rollups"); // Delete previous roll up records. rollupsForm.getRecords().forEach(rollupRec => rollupsForm.deleteRecord(rollupRec)); var lines = []; // log to console the aggregated values. for (var month in rollups) { var line = month + "," + rollups[month] console.log(line); lines.push(line); var rollupRec = rollupsForm.addNewRecord(); rollupRec.setFieldValue("fld-cd1d454672c84bce8103a4267507ca03", month); rollupRec.setFieldValue("fld-9eeeff7120db401b830ccec4e06f2bc3", rollups[month]); } document.saveAllChanges();We’re getting a form named “Rollups”, we’re going to clean it out each script run and then as we go through the rollups, we’ll add a new record and set a text field and a number field. We need to tell Tap Forms to save the changes as well which is the last line of that snippet. As with previously, replace the field ID’s with your own.
When doing this roll up, if in your main form you add a new script field (I called mine “Purchase Date Simplified”) and put this content in it:
function Purchase_Date_Simplified() { var purchase_date = record.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd'); if (!purchase_date) { return; } // format the date for use in the rollup. return purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay(); } Purchase_Date_Simplified();Then in your rollup form you can create a new “Link to Form” field, link it to the original form, then you set it to the JOIN type and you can match the text field to the script field. Then you can easily go from a roll up record to see which items were in the rollup.
September 7, 2021 at 12:37 PM #45196In reply to: Looping Through Records
Mark Robbins
ParticipantThanks, Daniel.
Your examples helped me get through the record looping, and things appear to be working as desired now. I ended up incorporating the original Fetch_Bgg_Information instead of calling it with form.RunScriptNamed(). When I get time for additional testing, I might pull it back out.September 7, 2021 at 11:45 AM #45195In reply to: Looping Through Records
Daniel Leu
ParticipantI treat
form.runScriptNamed()like anincludein other languages to define constants and functions. So at the beginning of my script, I’d haveform.runScriptNamed(‘Fetch_Bgg_Information’);but without any local function calls.Then in the loop over records, you would call whatever function you have and provide the current record id as an argument. Maybe something like this:
for (thisRecord in records) { // go to the record and run the function; console.log(“BGGid: ” + records[thisRecord].getFieldValue(field_bggid)); console.log(“Index: ” + thisRecord); Fetch_Bgg_Information(records[thisRecord].getId()); }And then in you
Fetch_Bgg_Informationscript you would have the functionfunction Fetch_Bgg_Information(myRecordId){ let myRecord = form.getRecordWithId(myRecordId); ..... }Or you could just define
let myRecord = records[thisRecord]in the loop and then access myRecord in your functionFetch_Bgg_Information()without using an argument. I prefer to use function call arguments, specially when the function is defined in a different script.Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 7, 2021 at 11:20 AM #45192In reply to: Looping Through Records
Brendan
KeymasterHi Mark,
What does
Fetch_Bgg_Information()do?Can you upload your form template?
The
form.selectRecord(record)function is generally just used as sort of the last thing in a script to have Tap Forms select a specific record in the user interface. It’s like if you added a new record in the script and wanted Tap Forms to select that new record after. At least that’s what I had in mind when I wrote that function.September 7, 2021 at 10:15 AM #45191Topic: Looping Through Records
in forum Script TalkMark Robbins
ParticipantI have a need to loop through each record in a table and run a script for that record. I wrote the script below, but it does not seem to work. I’m new to Tap Forms, and a bit new to javascript, so my understanding may be at fault. I know the called script works well when I run it from the Form window for a record, I just can’t get it to run against each record.
Thanks.
function Process_All_Records() {
var records = form.getRecords();
var field_bggid = “fld-ce86e98498894969839b4d574c754511”
var value_bggid = 0for (thisRecord in records)
{
// go to the record and run the function;
form.selectRecord(records[thisRecord]);
console.log(“BGGid: ” + records[thisRecord].getFieldValue(field_bggid));
console.log(“Index: ” + thisRecord);
form.runScriptNamed(‘Fetch_Bgg_Information’);
}
}Process_All_Records();
September 7, 2021 at 2:24 AM #45190In reply to: Help with script
Guillermo q
ParticipantThank you very much, the script works like charm!
Is there any possibilities to get a basic graph in that script??
Thank you very much.
September 6, 2021 at 9:34 PM #45189In reply to: Help with script
Sam Moffatt
ParticipantOn the Mac, it’s at the top of the record list next to the form name with a little menu sort of icon. On most views, it’s pretty close but the MCLV has it all the way to the right because of the extra stuff.
On iOS I don’t see a group summaries option, just the section headings. At that point I guess you’ll need to run the form script and check out the console log for the output.
September 6, 2021 at 4:00 PM #45188In reply to: Help with script
Guillermo q
ParticipantThank you for a so elaborated answer.
Sorry for this basic question: where is the show group summaries option?Otherwise I will try the script. Thank you.
September 6, 2021 at 2:14 PM #45187In reply to: Help with script
Sam Moffatt
ParticipantYou can do that by sorting by that date field, enabling “show group summaries” (this will also enable section headings) and then in your form settings, set the calculation to be total and the price field. Then you’ll get a summary at the bottom of each section. If you use the multicolumn list view, it’ll give you the option to display a calculations row and control per field aggregations as well.
In terms of a script, you’ll need to loop over all of the records in the form. Something simple like this could do it:
function Aggregate_By_Date() { // set our date format, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString // remove "day" from it and you'll roll up by month for example. var dateFormat = { "day": "2-digit", "month": "2-digit", "year": "2-digit"}; // this is where we're going to store the rollups per day. var rollups = {}; // iterate to all of the records in the form for (var rec of form.getRecords()) { var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd'); var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07')); // Skip entries that don't have a price or date set. if (!price || !purchase_date) { continue; } // format the date for use in the rollup. var formattedDate = purchase_date.toLocaleDateString("en-AU", dateFormat); // Rollup to this date, add to the existing value or set it if not set. rollups[formattedDate] ? rollups[formattedDate] += price : rollups[formattedDate] = price; } // log to console the aggregated values. for (var month in rollups) { console.log(month + ": $" + rollups[month]); } } Aggregate_By_Date();If you want to do more with the date formatting you’d have to pull out the fields from the date object. This will also be sorted in the order you retrieve them from the form which I think will be the default form ordering, so you’ll want to make sure that your first sort field is set to the date field to get the script output in the format you need.
September 6, 2021 at 3:05 AM #45184Topic: Help with script
in forum Script TalkGuillermo q
ParticipantHello to all! I love this app, abd I hace discovered the scripts and this is amazing.
I would like to know if there is a way to do the following
– I have a form where every item has a date and a price. I have several items in every date.
– I would like an script that sums everything in every date and gives me a list of dates with total prices(example: 23-5-21 > 250; 22>5>21 > 100Thank you very much
September 6, 2021 at 1:04 AM #45182In reply to: Copying and Pasting fields using Script
Sam Moffatt
ParticipantSo you need to get the field values via
record.getFieldValue, join it into a comma separated string and then userecord.setFieldValueanddocument.saveAllChangesto set and save the new value.Here is a script that takes a street, city and state fields to then join them into comma separated single line and then sets a location field and also a website field. You will need to change the ID’s to match your own ID’s. How do you find the ID of the field? It’s underneath the description field of the form editor or you can use the ID button in the script editor to also get the field ID or copy it from below the field list when selecting that field.
function Location_Copy() { var fields = ['fld-b5b69ac18a584b9998908bc82f42d7a5', // Street 'fld-5943325d2ee6480b89a1d044b8695e6c', // City 'fld-105b4e8304524a0bb7710c2b507f0590', // State ]; var location_id = 'fld-4d374af0f2644f798ea1e6c3c434290a'; // Location type field var google_maps_link_id = 'fld-db45b343c30f4410aa78c690ad92b2ff'; // Website type field var pieces = []; for (var field of fields) { var item = record.getFieldValue(field); item ? pieces.push(item) : null; } var joined = pieces.join(', '); console.log(joined); record.setFieldValue(location_id, joined); record.setFieldValue(google_maps_link_id, <code>https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}</code>); document.saveAllChanges(); } Location_Copy();Ok to take this apart a little. This first bit sets a variable called
fieldsthat has the list of fields in the order you want them to show up in the address. For my example I did street, city and state following your ask but you could add another line for country with the field ID of your country field.function Location_Copy() { var fields = ['fld-b5b69ac18a584b9998908bc82f42d7a5', // Street 'fld-5943325d2ee6480b89a1d044b8695e6c', // City 'fld-105b4e8304524a0bb7710c2b507f0590', // State ];These next two lines are setting the field ID’s of our target fields, the location field and a website field. Again, you can select your version of this field and click “ID” to get an output that looks like this. The
//is a comment that I added manually.var location_id = 'fld-4d374af0f2644f798ea1e6c3c434290a'; // Location type field var google_maps_link_id = 'fld-db45b343c30f4410aa78c690ad92b2ff'; // Website type fieldThis next part creates a list of
piecesand then uses the list offieldsto be able to get the field values of each of the fields (e.g. street, city, state) and then check if they’re set to a value (thats theitem ?line) and if they are set, push them into the array of items (pieces.push) or do nothing (thenull). This means that if a field is empty, it won’t be added and you won’t get a blank comma for example. Also means that you can add more fields by just adding the ID’s in the right order and everything will still work:var pieces = []; for (var field of fields) { var item = record.getFieldValue(field); item ? pieces.push(item) : null; }This is where we
jointhepiecestogether to create our comma separated list. I also log this for debugging because it’s useful to see what is going on.var joined = pieces.join(', '); console.log(joined);We then set the field value (
record.setFieldValue) of the location field and the website field with a Google Maps link. The location field can just be set with the simple joined value however the website link needs some extra data to hook it up to Google Maps. We need to save the changes which is whatdocument.saveAllChanges()does for us.record.setFieldValue(location_id, joined); record.setFieldValue(google_maps_link_id, <code>https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}</code>); document.saveAllChanges(); }Lastly we need to tell Tap Forms to call the function we just created:
Location_Copy(); -
This reply was modified 4 years, 2 months ago by
-
AuthorSearch Results