Thanks for the script for rain totals. Now I figure I can make that do the same for donations. Am I on the right track? I keep getting syntax errors or other errors trying to get the syntax right. Am I way off track? Attached is my screen shot. I can’t seem to get the {} deletions right.
– I deleted the “beginning of the year” section.
– Replaced the variables “today” & “previous_total” ID values with the ones appropriate for my form. Now it looks like this.
function Season_to_date() {
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
// xx = line deleted
xx }
xx // Is this the beginning of the year?
xx var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
xx if (date.getMonth() == 9 && date.getDate() == 1){
xx return 0;
}
var today = record.getFieldValue('xxxxx'); //replaced val with my value from form
var previous_total = previousRecord.getFieldValue('fld-xxxxx'); //replaced val with my value from form
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
-
This topic was modified 3 years ago by
Brendan. Reason: Added back ticks around code -- Brendan
Attachments:
You must be
logged in to view attached files.
The experts seem to be busy, so I’ll chime in here.
Your solution will require adding the results in one record with results from the following record to get totals at the bottom of your spreadsheet. I know that can’t be done without lots of javascript programming which is much harder that tracking with a spreadsheet. If I’m wrong, I’m sure somebody will correct me. Look at my postings for examples. They have helped me, but that was a simple solution compared to yours I think. If not you can look at the script they provided for me to add up daily rain totals to get a yearly summation.
A while ago I believe Daniel wrote a script for me to grab a number from a previous record and add it to a number in the current record to provide a running total. I have several forms that use this technique (in another DB program now defunct) for various purposes including medication days between treatments and rain totals. In that program no computer language programming was necessary.
I could not get the script to work and finally 2.5 weeks ago I submitted my Form with data and the script included (“Previous Field calc”)for rain totals in the Script subdivision of the forum, but apparently nobody is interested in looking at it.
I would really like to get this working – I’m used to depending on those forms.
Hope somebody will look at my problem.
Thanks.
Try to define the variables outside of your script:
var value_1;
var value_2;
function Script() {
...
}
Script();
-
This reply was modified 3 years, 1 month ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I have a journal that has items that need a date listed for when It should recur next.
I don’t want a value in that field unless the checkbox “Recur” is checked.
Does this take script or can it be done with an if statement.
I just wanted to know before I made a new Form to play with because I know I’ll lose all my data if I play with this one.
Make sure that you set the Script folder on the Preferences window to your Desktop folder before you run the script. It’s probably not able to read the file due to security restrictions in macOS.
Hi Brendan, thanks for that example script. It works, if i get the JSON data from clipboard.
Using your original script to get the JSON data from file:
...
let url = 'file:///Users/jur/Desktop/CitiesAndTimeZones.json';
let cities = Utils.getJsonFromUrl(url);
console.log(cities);
for (let index = 0; index < cities.length; index++) {
...
results to:
undefined
Import Time Zones: TypeError: undefined is not an object (evaluating 'cities.length'), line:(null)
One cool feature in TF is that picklist entries can come from a form. So I have a form called ‘Brands’ that contains name and URL fields. I select this form to be the source of my picklist. Then in my main form, I use that picklist to select the brand. Further, I use a field script ‘URL Script’ to set the value of the URL field. The field script is triggered whenever a different brand is selected and then the URL field is updated accordingly.
When the script is triggered, it fetches all entries of the ‘Brands’ form and loops through them. Once a match is found, the URL field is set according to that matching record entry and the script aborted:
function Url_Script() {
var brand_id = 'fld-feb500eed99747169d8d90db3a620243';
var url_id = 'fld-0a342472ca0b44b1a52c3758c5be92e4';
const brands_name_id = 'fld-8d373c0912154c69a7f579e696cd598c';
const brands_url_id = 'fld-0a342472ca0b44b1a52c3758c5be92e4';
let brand = record.getFieldValue(brand_id);
// get the brands form. Note that the name of the form must match!
let brandsForm = document.getFormNamed("Brands");
for (let rec of brandsForm.getRecords()){
if (rec.getFieldValue(brands_name_id) == brand){
let url = rec.getFieldValue(brands_url_id);
console.log("found match: " + brand) + " with url " + url);
record.setFieldValue(url_id, url);
document.saveAllChanges();
return url;
break;
}
}
}
Url_Script();
Hope this is what you were looking for. I have attached my example document.
Attachments:
You must be
logged in to view attached files.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Thanks Brendan. I did have a go at scripting via the scripts tab but I have very limited JavaScript programming knowledge. It is something to learn. I will also play around with Link to Form. Maybe a more detailed look into the help sections.
Hi Bil,
Something like this could be done with a Script field that monitored the value from your field that contains the names and then sets a Website Address field to a website when you select it. Your script would probably have to have the list of corresponding website addresses in it though.
I’m not sure how well versed you are in JavaScript programming though.
You could also use a Link to Form field that contains a list of names and corresponding website addresses. Then when you assign the child record to your parent record, you would see the name and website address. The website address on the child form would be a Website Address field. At least you would want it to be.
Thanks,
Brendan
Hi Chris,
Well, what you probably have is an array of dictionaries in your JSON.
So your data variable is probably just that. So you need to iterate over the array and pick out the values, create a new record for each entry in the JSON file and set the field value on it.
Here’s an example script that does that to import a file of time zones.
function Import_Time_Zones() {
var time_zone_name_id = 'fld-1b8bca6459724faabec0d3f7a1b3e4e4';
var country_id = 'fld-69acd4190b974d678a6e662308d580d7';
var name_id = 'fld-4dc0850299c64960b480a3593bbdfad0';
let url = 'file:///Users/brendan/Desktop/CitiesAndTimeZones.json';
let cities = Utils.getJsonFromUrl(url);
for (let index = 0; index < cities.length; index++) {
let city = cities[index];
let name = city.name;
let country = city.country;
let time_zone_name = city.timeZoneName;
let newRecord = form.addNewRecord();
newRecord.setFieldValue(name_id, name);
newRecord.setFieldValue(country_id, country);
newRecord.setFieldValue(time_zone_name_id, time_zone_name);
}
form.saveAllChanges();
}
Import_Time_Zones();
I’ve attached the JSON file if you want to play around with it.
One very helpful technique when dealing with JSON files, is use the JSON.stringify() function to see what kind of data you get from your JSON file.
Thanks, that’s useful. But with my little experience with java script it isn’t possible for me to get a reliable result. Maybe someone else has solved that in the past and could give some hints.
Especially if one have a JSON file with multiple “datasets”, looping through the “datasets” and adding a new entry in TF for every “dataset” is difficult to understand for me :-( … but i’m working on it…
@Brendan (if you read this): Wouldn’t it be easier to introduce an import option for JSON files? JSON is also only a table in some way… or am I thinking wrong?
What a coincidence, I’m working on getting JSON data into TF right now as well. My data is on the clipboard. So I import it from there. Next I translate it into a javascript data object. Then loop over the different entries.
For you, since not all records have the same fields, you need to check for valid content like I do with customer.name.
let customersJSON = Utils.copyTextFromClipboard();
let customers = JSON.parse(customersJSON);
for (let customer of customers){
if (customer.name){
console.log("Processing customer name: " + customer.name);
} else {
// missing customer name branch
}
}
Hope this helps you get started.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Hi everyone,
I would like to import data from a JSON file. The fields should be filled accordingly. Here is an example of a JSON file with one record. Retrieving the entire file is no problem with
var data = Utils.getJsonFromUrl(url);
… but I want to fetch them one by one and populate the fields in the form’s record.
I think it is easy, but i’m still not handy with java script.
Easyest way would be from the import dialog, but that doesn’t work.
Thanks
Chris
The due date is different for each record.
2 fields are used here
1 -Original date of event.
2 – days until next thing is due.
It doesn’t look like you can use a field in the option “D”.
DATEADD(Date;Y;M;W;D;H;M;S)
Looks like D has to be a set number, not a var from another field. That is why I thought a script would be needed.