Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,126 through 1,140 (of 2,951 total)
  • Author
    Search Results
  • #45439
    Sam Moffatt
    Participant

    The easiest way would be to copy the function and replace the mappings. So instead of Import_Entries() and Import_Whom() and you just change the file name, remap the field mapping and instead of form.addNewRecord() you do document.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.parse line to add a new header option. Then the output.data will 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).

    #45437
    Victor Warner
    Participant

    Sam,

    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?

    #45435
    Sam Moffatt
    Participant

    There 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.

    #45430
    Fernando DS
    Participant

    I sent the script and the error. The script does nothing in the database.

    Attachments:
    You must be logged in to view attached files.
    #45428
    Fernando DS
    Participant

    No, 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.

    #45427
    Daniel Leu
    Participant

    If you delete the second function (script), does the first one work?

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #45426
    Fernando DS
    Participant

    The second script works. The first don’t. But why?.

    #45424
    Fernando DS
    Participant

    I 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.
    #45422
    Victor Warner
    Participant

    Sam,

    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.
    #45421
    Fernando DS
    Participant

    I 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?.

    #45419

    In reply to: Newbie Questions

    Sam Moffatt
    Participant

    The 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.

    #45418
    Sam Moffatt
    Participant

    So 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.parse and 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.

    #45415
    Sam Moffatt
    Participant

    You probably just need to delete line one entirely (the function Buscar_Y_Modificar one) . 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_Metal with Buscar_Y_Modificar on the function line and also at the bottom of the script (e.g. change Replace_Metal(); with Buscar_Y_Modificar();).

    #45412
    Fernando DS
    Participant

    The script photo is going on now.

    Attachments:
    You must be logged in to view attached files.
    #45410
    Fernando DS
    Participant

    Hi 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.
Viewing 15 results - 1,126 through 1,140 (of 2,951 total)