I did have a record that had no data, date or values. Deleted that. Also made sure my added records for Jan 1 had the value = 0 for donations (that field was empty).
Now the script runs without errors with the original lines, or those lines replaced with the above correction for undefined.
But, all my records have nothing in the Totals value. All blank.
I checked the script window and the value = number. Also the field Total is calculated and that value = decimal with zero decimal places.
Stopped the program, loaded it again and brought up the Form and refreshed it many times and no totals show up.
Is there a way to do this?
Thanks. Code below just so you know what I’m working with.
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 != undefined && date.getMonth() == 0 && date.getDate() == 1){
}
// 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();
I just ran it again to verify and I get using one statement:
2/16/23, 9:26:49 AM / Donations / Yearly
Yearly: TypeError: undefined is not an object (evaluating ‘date.getMonth’), line:(null)
The other statement gives me:
2/16/23, 9:29:25 AM / Donations / Yearly
Today: undefined
Previous: undefined
Total: NaN
I’m really confused. Don’t know what is going on.
Here is my file.
I would like to see my donations for any single year that I can see with a search view.
I have a script in the Total Field and a similar one (maybe different now) in the Script designer field.
Attachments:
You must be
logged in to view attached files.
Scripts can’t be run line by line due to how Apple’s JavascriptCore framework works.
The best you can do is to put console.log("got here") or console.log(someVariable) statements into your code.
But when you see undefined, it means exactly that. The value you’re trying to call a method on is empty or hasn’t been initialized with a value.
So you should add date != undefined into your if statement where you call getMonth().
if (date != undefined && date.getMonth() == 0 && date.getDate() == 1){
}
You may have a record that has no date value, so that’s what would cause that error.
The TF crashed. When it opened and I ran refresh again.
I get no errors:
2/15/23, 6:48:31 PM / Donations / Yearly
But, all my values in the Total col are blank.
Would help if the script could be run line by line. I assume it can, but haven’t found it.
Oh that’s weird. I didn’t know JavaScript had different scope rules than most every other programming language I know.
That didn’t help. I get error in line 16 and playing with that I’m not sure where I’m at.
Added a backtick char.
Here is the code I was using for my rain totals, but the rain season was from Oct 1 to Sept 31 of each year which required a loop I think to check for the end of the season. Since for this purpose, the year I’m grouping is for the tax year Jan 1 to Dec 31. So, didn’t think I needed the search for Oct 1 date. I thought if I took out that portion that this script would do the job of letting me see all years worth of donations and set up a view of only one specific year and see donations starting at 0 and adding up the amounts until Dec 31. Seemed simple.
This is the Rain script, so I assume I have to replace the id code like this (‘fld-0720a5baa6fc4980aee22798bd089714) with the id code for the field I want in my donation form.
So, do I have to start over or can I use this code with the correct changes. Sorry, thought this would be easy. Hopeful and thank you.
================
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();
There’s no need for scripting just to get totals. You can just set the Calculation Summary option on all the fields you want to get a total for to appear at the bottom of the records list.
I haven’t had time to write the form for you Yvette. Sorry about that. But I would suggest starting with just creating the form as you have it now in the spreadsheet. Just add the fields one by one setting the field types appropriately. Then set the Group Records By option to Category and the First Sort Field to whatever the next level of sorting it is that you want. Probably the Name field.
Then set the Calculation Summary setting for each of your Number fields. Or click the ∑ symbol at the bottom of the Multi-Column List View to turn on the Calculation Summary Row. Now you can click the popup buttons to have Tap Forms generate totals for you.
To see sub-totals for each category, just enable the Show Group Summaries option available from the little menu button that appears at the top-right of the multi-column list view.
Hi Kurtois,
That’s the video channel for Sam Moffatt. You’ll find him fairly active on the forums here. Especially the Script Talk forum.
For the best tutorial videos, you can find links to them on my Support page (https://www.tapforms.com/support) or select Video Tutorials from the Help menu in Tap Forms 5 for Mac.
Or here for Mac:
https://vimeo.com/channels/tapformsmac5
And here for iOS:
https://vimeo.com/channels/tapformsios
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 2 years, 11 months 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 2 years, 11 months 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