Search Results for 'script'
Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Search › Search Results for 'script'
-
AuthorSearch Results
-
October 9, 2021 at 1:10 AM #45439
In reply to: Using Javascript to import csv files
Sam Moffatt
ParticipantThe easiest way would be to copy the function and replace the mappings. So instead of
Import_Entries()andImport_Whom()and you just change the file name, remap the field mapping and instead ofform.addNewRecord()you dodocument.getFormNamed("Other Form").addNewRecord(). That means a bunch of duplicated code but it will work.A more elegant solution would probably try to enable the header support and then attempt to map those across automatically. First change is to enable headers by updating the
Papa.parseline to add a new header option. Then theoutput.datawill change to be keyed by the first row field names. This means assuming the header matches your field name, otherwise you’re hard coding. So assuming the CSV header matches field names, you can then iterate over them, grab the field from TF and then update it’s value.Here’s the full script:
function Import_Entries(formName, filename) { let filename = "file:///Users/victor/Desktop/" + filename; let csvFile = Utils.getTextFromUrl(filename); if (!csvFile) { console.log("No CSV file?"); return } var targetForm = document.getFormNamed(formName); if (!targetForm) { throw new Error("Invalid form name: " + formName); } var output = Papa.parse(csvFile, { header: true }); // abort if there are any errors and log to console. if (output.errors.length > 0) { console.log(errors.join("\n")); return; } console.log(JSON.stringify(output)); // read each line for (let line of output.data) { var newRecord = form.addNewRecord(); for (let fieldName in line) { console.log(fieldName); if (!line[fieldName]) { continue; } var field = targetForm.getFieldNamed(fieldName); if (!field) { console.log("Invalid field name: " + fieldName); continue; } newRecord.setFieldValue(field.getId(), line[fieldName]); } document.saveAllChanges(); } } // Import entries to form "Who" from filename "Whom.csv" Import_Entries("Who", "Whom.csv");This is a use case I’d possibly put in my Script Manager form (and I should add Papa Parse to the repo) or some other sort of scripting form that has scripts that are agnostic of their parent form (sort of like a document script).
October 8, 2021 at 9:44 PM #45437In reply to: Using Javascript to import csv files
Victor Warner
ParticipantSam,
Thank you.
One question: Is it possible to import more than one .csv and put the contents of each .csv into a different form with the same script?
October 8, 2021 at 4:46 PM #45435In reply to: Using Javascript to import csv files
Sam Moffatt
ParticipantThere should be an
Import_Entries()line at the bottom of the script to actually do the import. Without that line, it won’t run the function to import the data.October 8, 2021 at 4:36 PM #45430In reply to: Script for find and modify records
Fernando DS
ParticipantI sent the script and the error. The script does nothing in the database.
Attachments:
You must be logged in to view attached files.October 8, 2021 at 3:14 PM #45428In reply to: Script for find and modify records
Fernando DS
ParticipantNo, these are two scripts. I have put it in the same page to compare. The second one works, it’s the one done last days. The first one is a copy of the second, changing the fieldid and the words to be changed. The first does not work.
October 8, 2021 at 3:09 PM #45427In reply to: Script for find and modify records
Daniel Leu
ParticipantIf you delete the second function (script), does the first one work?
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksOctober 8, 2021 at 11:28 AM #45426In reply to: Script for find and modify records
Fernando DS
ParticipantThe second script works. The first don’t. But why?.
October 8, 2021 at 11:20 AM #45424In reply to: Script for find and modify records
Fernando DS
ParticipantI send both scripts. They are the same, just change the fieldid and the words to be changed. Where is the mistake?
Attachments:
You must be logged in to view attached files.October 8, 2021 at 9:58 AM #45422In reply to: Using Javascript to import csv files
Victor Warner
ParticipantSam,
Thank you very much for the script.
On the first run it worked – it imported the .csv file.
On subsequent runs – nothing was imported – and no error was generated.
The script as I used:
// this imports the Papa Parse script form.runScriptNamed('PapaParse'); // replace with your field ID's var first_name_id = 'fld-068c60aaec5047ea93f46988251ae938'; var last_name_id = 'fld-4155c0ec104e4448af17f2776880d205'; var email_id = 'fld-6a9e9e828e294e2ca694e751c7f3faf9'; var date_id = 'fld-9c739c301f614a7c8b21eb427059430e'; function Import_Entries() { let filename = "file:///Users/victor/Desktop/Whom.csv"; let csvFile = Utils.getTextFromUrl(filename); if (!csvFile) { console.log("No CSV file?"); return } var output = Papa.parse(csvFile); // abort if there are any errors and log to console. if (output.errors.length > 0) { console.log(errors.join("\n")); return; } // read each line for (let line of output.data) { var newRecord = form.addNewRecord(); newRecord.setFieldValues({ [first_name_id]: line[0], [last_name_id]: line[1], [email_id]: line[2], [date_id]: line[3], }); document.saveAllChanges(); } }The .csv file contains one line:
Simon,Adams,Noone@AtHome.com,01/02/1888
I tried changing the data but that made no difference.
I attach the database in case there is something in that which shows what the issue is.
Attachments:
You must be logged in to view attached files.October 8, 2021 at 9:13 AM #45421In reply to: Script for find and modify records
Fernando DS
ParticipantI will look for that video. In the meantime i have been doing some tryings with the script. I want search in any other field. I though it be easy. But not at all. I have changed the fileid, and the words to be changed. The rest of the script exact the same. But I have errors. How is possible?.
October 7, 2021 at 5:46 PM #45419In reply to: Newbie Questions
Sam Moffatt
ParticipantThe first could possibly be achievable using a number of script fields to calculate the values you’re after and then you could display them. It’d be a weird use case because it’d be a form full of script fields that calculate values in other forms/searches but conceivably that could work. It’d be a non-trivial amount of work to setup.
October 7, 2021 at 5:41 PM #45418In reply to: Using Javascript to import csv files
Sam Moffatt
ParticipantSo the link in the original post has an example of
Utils.getTextFromUrl, the same sort of instructions apply.For a CSV parser, I just tried Papa Parse and it worked fine. I went to their GitHub repo and copied their uncompressed Javascript code into a new form script called “PapaParse”. I did the following quick test and it worked properly:
form.runScriptNamed('PapaParse'); function Csv_Parse_Test() { // Replace with your own code var hello_world = '"Hello, World!",Test,123'; var output = Papa.parse(hello_world); console.log(JSON.stringify(output, null, '\t')); } Csv_Parse_Test();Outputted the following:
7/10/21, 5:22:19 pm / Script Examples / CSV Parse Test { "data": [ [ "Hello, World!", "Test", "123" ] ], "errors": [], "meta": { "delimiter": ",", "linebreak": "\n", "aborted": false, "truncated": false, "cursor": 24 } }This seems to be correct interpretation of the input. So let’s put that together with the earlier post:
// this imports the Papa Parse script form.runScriptNamed('PapaParse'); // replace with your field ID's var title_id = 'fld-4e8e68e2979643be8417c3469015abff'; var description_id = 'fld-429934cd6ae646b1ac1cf5ad659cb926'; var url_id = 'fld-b46c858779094c9d906f2ce5e5c4a028'; var upload_date_id = 'fld-fc1f8537415c4d6cabb8c0784b64f2a6'; var thumbnail_url_id = 'fld-12b4e040711b4afea624c7c049fdd7ce'; function Import_Entries() { let filename = "file:///Users/yourusernamehere/Documents/input.csv"; let csvFile = Utils.getTextFromUrl(filename); if (!csvFile) { console.log("No CSV file?"); return } var output = Papa.parse(csvFile); // abort if there are any errors and log to console. if (output.errors.length > 0) { console.log(errors.join("\n")); return; } // read each line for (let line of output.data) { var newRecord = form.addNewRecord(); newRecord.setFieldValues({ [title_id]: line[0], [description_id]: line[1], [url_id]: line[2], [upload_date_id]: line[3], [thumbnail_url_id]: line[4] }); document.saveAllChanges(); } } Import_Entries();First thing we do is pull in the Papa Parse script. Then we have the field ID’s for our fields that we’re going to use. We could put this anywhere, I like it being in global scope since they’re immutable anyway.
We enter the function and define the path to the file. If you don’t know what this is, drag the file from Finder into the script editor and it’ll put the full path in for you. Make sure this folder is the same one you’ve given access to in the Script Folder Access in the preferences for this document. The next line imports that file for you.
We then check to see if we got anything from the CSV file, if we didn’t we log an error and return flow control. We then hand the contents of the file to
Papa.parseand if it has any errors we log them and return as well.We then read each line of the data to process it, create a new record, map the field order in the CSV to our Tap Forms field ID’s and then save the changes.
You might get a performance boost moving the
document.saveAllChanges()line but there are quirks around creating new records that cause me to leave the save in the loop not outside.I’ve not tested it but something like that should work.
October 7, 2021 at 1:01 PM #45415In reply to: Script for find and modify records
Sam Moffatt
ParticipantYou probably just need to delete line one entirely (the
function Buscar_Y_Modificarone) . That looks like the old placeholder which you don’t need since you’ve got a function (Replace_Metal). Assuming that’s the full script then nothing else jumps out at me as wrong.If that works, you could also change
Replace_MetalwithBuscar_Y_Modificaron thefunctionline and also at the bottom of the script (e.g. changeReplace_Metal();withBuscar_Y_Modificar();).October 7, 2021 at 11:47 AM #45412In reply to: Script for find and modify records
Fernando DS
ParticipantThe script photo is going on now.
Attachments:
You must be logged in to view attached files.October 7, 2021 at 11:43 AM #45410In reply to: Script for find and modify records
Fernando DS
ParticipantHi Daniel, thank you for your support. I have done it and I sent the script as it is now. But there is an error, that I also sent.
Attachments:
You must be logged in to view attached files. -
AuthorSearch Results