Sorry for the late Answer was messing around with the scripts. Yes it is working 👍
Your best bet for now would be to export your records to an Excel spreadsheet and then import it into Excel (or Numbers) and do some summarization there. At the moment I don’t have a function that would do that. Tap Forms will compute totals for each category, but it will also display the details.
But of course you could write a script for it to just dump out the summary data in the console. But it would be easier for you to use Excel. Especially if you’re not familiar with scripting.
Thanks,
Brendan
Hello,
I have a database with 5185 records in 58 categories.
It’s a collection of comic book puzzles, and the different categories correspond to the comic book titles, as shown in the screenshot.
In the database, there are two scripts that highlight puzzles in the collection and those that are incomplete (scripts created by a form user at least 6 years ago).
I would like to automatically retrieve the number of puzzles in the collection and the number of missing puzzles for each category into a table.
I don’t know if this is possible with a script, and I don’t know how to do it either, as I’m not familiar with scripting.
Thank you for your information, and perhaps it’s not feasible. The screenshots show my database, the relevant fields, and a table as it could be created.
Attachments:
You must be
logged in to view attached files.
I got the script to work, thanks. BUT, not all dayofyear values are the same for multiple records of the same day. What does that, to be off a day?
Thanks for sticking with me on this.
Attachments:
You must be
logged in to view attached files.
OMG, how did I miss using the script window instead of the calc. I’m sorry. Just haven’t had to edit a script in a while I guess.
I changed the field type to SCRIPT.
I bring up the SCRIPT window and it has an example starting script. I can delete it, but I can’t past into the winddow the script you supplied.
If I try to type it, I’ll have many mistakes to weed out and that will be a big waste of both of our time.
How can I paste something into that SCRIPT window. I remember not being able to add the fld- number by copy and paste, but I never complained about that until now.
You can use field.sortedTableFields to get field details of a table.
Following function copies the values from the source record to a new row in the table of the current record. I’ve verified for text and number fields.
/**
* copyRecordToTable(srcRecord, tableId)
*
* Copies values from srcRecord into a new row appended to the table field
* identified by tableId, for all fields whose names match between the
* source record's form and the table.
*
* @param {TFRecord} srcRecord - The source record to copy values from.
* @param {string} tableId - The field ID of a Table-type field on the record.
* @returns {TFRecord} - The newly created table row record.
*/
function copyRecordToTable(srcRecord, tableId) {
// Resolve the table field from the record's own form using the field ID.
const table = form.getFieldWithId(tableId);
// Get the fields defined on the table, keyed by name for fast lookup.
const tableFields = table.sortedTableFields;
const tableFieldsByName = {};
for (var i = 0; i < tableFields.length; i++) {
var tf = tableFields;
tableFieldsByName[tf.name] = tf;
}
// Add a new row to the table.
const newRow = record.addNewRecordToField(tableId);
// Walk every field on the source record's form and copy matching values.
const sourceFields = srcRecord.form.fetchFields();
for (var j = 0; j < sourceFields.length; j++) {
var sf = sourceFields[j];
var match = tableFieldsByName[sf.name];
if (match) {
var value = srcRecord.getFieldValue(sf.getId());
newRow.setFieldValue(match.getId(), value);
}
}
return newRow;
}
To verfiy, I use this sample script which just adds a random record to the table.
function selectSourceRecord(formName) {
// return a random record from the form formName
const records = document.getFormNamed(formName).fetchRecords();
const index = Math.floor(Math.random() * records.length);
return records[index];
}
function Copy_Record_To_Product_Table() {
// source form name
const sourceFormName = "Products";
// select a random record from the source form
const sourceRecord = selectSourceRecord(sourceFormName);
// table field name
const tableFieldName = "Products";
// get the table field
const tableId = form.getFieldNamed(tableFieldName).getId();
// copy the record to the table
copyRecordToTable(sourceRecord, tableId);
document.saveAllChangesAndRefresh();
}
Copy_Record_To_Product_Table();
Let me know if this works for you!
-
This reply was modified 1 month, 4 weeks ago by
Daniel Leu.
-
This reply was modified 1 month, 4 weeks ago by
Daniel Leu.
-
This reply was modified 1 month, 4 weeks ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Hello everyone,
I’m currently experimenting with the “Table Fields”.
When creating one, there is an option to select a form. If I choose an existing form, I can use the button **”Copy fields into table…”** to insert the fields title from the previously selected form into the table. This works perfectly. However, I’m a bit curious about the description text displayed there:
> “Tap Forms will copy values from the selected records based on matching field titles.”
Is this functionality only available through the form selection field, or is there a way to achieve the same thing via scripting?
When I tried to do this with a script, I could only get it working by hard-coding the fieldsIDs beforehand. In other words, if I add a new field to the table, I have to manually add the corresponding field ID to the script.
Is there any way to read the table column headers via javascript, so that the script could iterate through them and (since the names are identical) retrieve the corresponding values from the original form automatically by getFieldNamed(‘Name’)?
Thanks for any ideas, and have a great weekend!
Steve
That’s a script for a Script Field. It’s not a formula for the Calculation Field.
You need to also call return result; at the end of the script.
But you also need to pass in your field’s date.
Here’s a more full example:
function Day_Number() {
const dayOfYear = myDate => {
const year = myDate.getFullYear();
const firstJan = new Date(year, 0, 1);
const differenceInMillieSeconds = myDate - firstJan;
return (differenceInMillieSeconds / (1000 * 60 * 60 * 24));
};
var date_id = 'fld-22b8461cd7e14fbc988ecd1b7b22a013';
let my_field_date = record.getFieldValue(date_id);
const result = dayOfYear(my_field_date);
return result.toFixed(0);
}
Day_Number();
You’ll need to replace the date_id (‘fld-22b8461cd7e14fbc988ecd1b7b22a013’) with your Date field’s Field ID.
Re 10 – scripts triggering on field change: this was user error and Brendan provided the solution via email.
I was using getId rather then explicitly identifying the field in the script itself:
mine that didn’t work correctly:
var currval=record.getFieldValue(form.getFieldNamed(“Currency”).getId());
Brendan’s solution that works great:
var currval=record.getFieldValue(‘fld-6cc122549b004a09b0e21e8b4f76b313’);
Here’s a JavaScript function which will give you the day of the year:
const dayOfYear = date => {
const myDate = new Date(date);
const year = myDate.getFullYear();
const firstJan = new Date(year, 0, 1);
const differenceInMillieSeconds = myDate - firstJan;
return (differenceInMillieSeconds / (1000 * 60 * 60 * 24) + 1);
};
const result = dayOfYear("2019-2-01");
For the time of day, you can call myDate.getHours(), myDate.getMinutes(), myDate.getSeconds()
I don’t remember what form I was using when I was given that instruction (I keep that now) in my notes (questions TapForms). It could have been a script.
I don’t care how I do it, you can see what i’m trying to do which is given the input from “Date & Time” in the first col., I want a day of year + a decimal for the time of day (hours & minutes).
This is so I can graph Date & Time against what happens on that event which is from a number from 1-6.
Thanks for looking.
Re 9 and updating latest date, in creating an example from scratch I realised that “recalculate formulas and scripts for all records” from the active form in single col view (rather than multi-col view) does work. I had completely missed that button on single-col view for some reason.
I had an exact copy of the ipad file I brought back, tried it with that and it worked fine so can scratch that one from the list.
Where did you get the info for the DAYOFYEAR() function? It is not a function I have in the formula editor.
Have you tried JavaScript to generate that?
Thanks,
Brendan
In another application, I used a checkmark field along with a field script to do some action. So you could have the same, once you set a checkmark field, the field script is triggered and that would clear all other checkmark fields.
Now with your main script, you can locate the two records in their respective forms that have the checkmark field set and populate your third form.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Re 9), I see that the ipad element makes no difference. I edited the subform direct on ipad as I couldn’t create a new subform record from within the parent form.
On macos, I just tried adding a record direct into the form that is used as a subform and that didn’t update when I viewed the parent form either.
It didn’t update when I tried multi-column > “recalculate formulas and scripts for all records”.
Some tests did update using the “recalculate formulas” in the parent form – but one time it didn’t.
Sadly that method isn’t practical as it requires my knowing which records are out of sync.
–
Re the forum post edit issue, I see that is just the result of the short editing time window.