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,186 through 1,200 (of 2,989 total)
  • Author
    Search Results
  • #45387
    Fernando DS
    Participant

    Hi Brendan,

    The script is as follows:

    function Buscar_Y_Modificar() {

    function Replace_Metal() {
    var fieldId = ‘fld-fieldid’;
    for (var record of form.getRecords()) {
    var fieldValue = record.getFieldValue(fieldId);
    if (fieldValue.includes(“rock duro”)) {
    record.setFieldValue(fieldId, fieldValue.replace(“rock duro”,
    “heavy metal”));
    }
    }
    form.saveAllChanges();
    }

    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or
    find it in the field editor underneath the description field.

    Sorry, my english is very poor. I don’t understand exactly what you mean with curly and straight quotes.

    #45386
    Brendan
    Keymaster

    I would think that you could import a CSV file because Tap Forms allows you to setup a Script Folder that has security permissions to read files from. Then you could use the var csv_data = Utils.getTextFromUrl(url); function to read in the CSV file and then just loop through it parsing out the data and writing it to the database.

    #45385
    Brendan
    Keymaster

    Can you upload a screenshot of your script as you have it?

    Your fieldId is wrong. Also, make sure you’re not using curly quotes for any of the values. They must be straight quotes. The example that Sam posted uses straight quotes, but your code you posted is using curly quotes.

    #45377
    Daniel Leu
    Participant

    There is currently no API available to import CSV files. One way to go around that is to use the clipboard to get data into TF (Utils.copyTextFromClipboard()) or to use external script calls with parameters (see paragraph ‘Calling a script from a URL’ in https://www.tapforms.com/help-mac/5.3/en/topic/javascript-api).

    Cheers, Daniel

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

    #45373
    Victor Warner
    Participant

    It appears from another post that it possible to import csv files to a form using Javascript.

    I go lost in the detail (and not being proficient in Javascript) and could not work out how to do so for myself.

    Using a simple database (see attached and csv files) what are the basic format/commands in Javascript to do so?

    Also whether it is possible to import into more than one form at a time.

    In the attached there are two forms (Who and Passport) whether it is possible to import into both of them from the same script.

    INnthe real world version I have data in Filemaker and I am exporting the data record by record and importing through File/Import/Records in Tap Forms. I have automated the steps with Keyboard Maestro – but it is time-consuming and sometimes fails (where Tap Forms or some other processes hangs for a while).

    Any help would be gratefully received.

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

    This is how the script is redacted:

    function Replace_Metal() {
    var fieldId = ‘fld-fieldid’;
    for (var record of form.getRecords()) {
    var fieldValue = record.getFieldValue(fieldId);
    if (fieldValue.includes(“rock duro”)) {
    record.setFieldValue(fieldId, fieldValue.replace(“rock duro”,
    “heavy metal”));
    }
    }
    form.saveAllChanges();
    }

    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or
    find it in the field editor underneath the description field.

    #45366
    Sam Moffatt
    Participant

    For something like that, a new form script like the following should do it:

    function Replace_Metal() {
      var fieldId = 'fld-fieldid';
      for (var record of form.getRecords()) {
        var fieldValue = record.getFieldValue(fieldId);
        if (fieldValue.includes("rock duro")) {
          record.setFieldValue(fieldId, fieldValue.replace("rock duro", "heavy metal"));
        }
      }
      form.saveAllChanges();
    }
    
    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or find it in the field editor underneath the description field.

    #45360
    Fernando DS
    Participant

    Hi, I would like to have and script to find and modify my records, but i don’t know nothing about programming and it’s very dificult to me to do the script. I use and iPhone and iPad, I have not got a Mac. Could anybody help me? Thank you very much.

    Attachments:
    You must be logged in to view attached files.
    #45331
    Brendan
    Keymaster

    Hi Tom,

    The act of choosing a folder from the user interface is what gives Tap Forms permission to access files within that folder.

    Tap Forms can have a different Script Folder for each document. Could it be that you were working with multiple documents and it just seemed like you had set it before but because it was a different document it hadn’t been set yet?

    #45328
    Tom Kerswill
    Participant

    Hi

    Quite often when writing a new script and pointing to a new file, I get undefined when doing getJsonFromUrl() or getTextFromUrl()

    I’m pretty certain this is due to permissions errors. Is there any way of catching an error in Tapforms script and seeing what the error is from a call like this? At the moment it’s impossible to distinguish between permissions errors or something else.

    What I’ve found in the past is that I’ve had to go into Tapforms preferences, and choose a comletely different folder for script folder access, and then set my chosen one again. But this always seems a bit flakey; sometimes when I go back into preferences the folder apparently hasn’t changed.

    I’m also not certain it works if you set a higher-up folder. E.g. ideally I’d like to set my “Documents” folder, but this doesn’t seem to always allow access to files in subfolders.

    Is there perhaps a different way to set permissions? Or perhaps Tapforms could ask for permissions when something tries to access a folder for the first time, using the Security preferences etc.

    Thanks in advance,

    Tom

    #45324
    T.L. Ford
    Participant

    I’m glad the scripting tutorials are useful. Eventually, I really need to get around to 103. :)

    #45318
    Stephen
    Participant

    Hi,

    I’ve created a field script to colour a record green when the task is complete. All looks great in the desktop version, clearly brightly filled rows in the linked table display of records.
    In iOS the view is slightly underwhelming & easily missed that tasks are complete, just a subtle coloured line at the top of the record once you tap to enter the linked form, no clear indication whilst viewing the table list of records.

    Is there anyway of making this colour change more obvious to mobile users at a glance?

    Thanks in advance for any suggestions, Stephen.

    #45315

    In reply to: Help with script

    Sam Moffatt
    Participant

    This one is a fun one, TF doesn’t track users but devices. It has a modified by device field but that can change if someone modifies it (which makes sense). You can do a ‘created by device’ or ‘record author’ by doing the following.

    First step is to add a new field of type ‘Modified by Device’. You’ll need it’s field ID from below the description field. The second step is to create a new text field to store the ‘created by device’ or maybe ‘record author’. The reason to use text field is so you can override it later, especially as your old records will be likely wrong. The third field is a script field that looks something like this:

    function Set_Record_Author() {
    
    	var record_author = record.getFieldValue('fld-471ac88b92364a228d9fec90d9a5347b');
    	
    	if (!record_author) {
    		var deviceToAuthor = {
    			"pancake": "Sam",
    		};
    
    		var device = record.getFieldValue('fld-0bbeb9cfa1024c11a624ed63cc8200e0');
    		
    		record.setFieldValue('fld-471ac88b92364a228d9fec90d9a5347b', deviceToAuthor[device] ? deviceToAuthor[device] : device);
    		document.saveAllChanges();
    	}
    }
    
    Set_Record_Author();
    
    • fld-471ac88b92364a228d9fec90d9a5347b is the ID of the Modified By Device field
    • fld-0bbeb9cfa1024c11a624ed63cc8200e0 is the ID of the Record Author field

    This creates a one time copy of the modified by field to our field though if you delete the contents of the text box it will reset. It also maps the device name (in my case my laptop is called pancake because it was thinner than my older laptop) to a human name as well but falls back to device name if there isn’t a match.

    function Aggregate_By_Date() {
    	// set our date format, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
    	// remove "day" from it and you'll roll up by month for example.
    	var dateFormat = { "day": "2-digit", "month": "2-digit", "year": "2-digit"};
    	
    	// this is where we're going to store the rollups per day.
    	var rollups = {};
    	
    	// iterate to all of the records in the form
    	for (var rec of form.getRecords()) {
    		var marketplace = rec.getFieldValue('fld-c163aba17ae64c4d93b5a53819a139dc');
    		var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd');
    		var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07'));
    
    		// Skip entries that don't have a price or date set.
    		if (!price || !purchase_date) {
    			continue;
    		}
    		
    		// format the date for use in the rollup.
    		var formattedDate = purchase_date.toLocaleDateString("en-AU", dateFormat);
    
    		// Rollup to this date, add to the existing value or set it if not set.
    		if (!rollups[formattedDate]) {
    			rollups[formattedDate] = { [marketplace]: price };
    		} if (!rollups[formattedDate][marketplace]) {
    			rollups[formattedDate][marketplace] = price;
    		} else {
    			rollups[formattedDate][marketplace] += price;
    		}
    	}
    	
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		for (var marketplace in rollups[month]) {
    			console.log(month + ", " + marketplace + ": $" + rollups[month][marketplace]);
    		}
    	}
    }
    
    Aggregate_By_Date();
    

    First change is to get the extra field, I used marketplace since I was testing against my products document but replace it with the field ID we just created.

    Second change is to how we do the rollup to add multiple dimensions (in this case, marketplace). For each dimension we need to check if it exists and create it if it doesn’t. If both date and marketplace are set, the else handles adding values together.

    The last change is to the output loop to iterate over each day and each “marketplace”.

    You’re using the script that doesn’t hit the rollup form but if you want to use it, you’ll need to add a new field to it and then where there is the addNewRecord line add an extra setFieldValue after it with the new field ID:

    function Aggregate_By_Date() {	
    	// this is where we're going to store the rollups per day.
    	var rollups = {};
    	//var marketplaces = new Set();
    	
    	// iterate to all of the records in the form
    	for (var rec of form.getRecords()) {
    		var marketplace = rec.getFieldValue('fld-c163aba17ae64c4d93b5a53819a139dc');
    		var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd');
    		var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07'));
    
    		// Skip entries that don't have a price or date set.
    		if (!price || !purchase_date) {
    			continue;
    		}
    		
    		// format the date for use in the rollup.
    		var formattedDate = purchase_date.getFullYear() + "-" + purchase_date.getMonth() + "-" + purchase_date.getDay();
    		
    		//marketplaces.add(marketplace);
    
    		
    		// Rollup to this date, add to the existing value or set it if not set.
    		if (!rollups[formattedDate]) {
    			rollups[formattedDate] = { [marketplace]: price };
    		} if (!rollups[formattedDate][marketplace]) {
    			rollups[formattedDate][marketplace] = price;
    		} else {
    			rollups[formattedDate][marketplace] += price;
    		}
    	}
    	
    	// Get the rollups form.
    	var rollupsForm = document.getFormNamed("Rollups");
    
    	// Delete previous roll up records.
    	rollupsForm.getRecords().forEach(rollupRec => rollupsForm.deleteRecord(rollupRec));
    	
    	var lines = [];
    	
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		for (var marketplace in rollups[month]) {
    			var line = month + ","  + marketplace + "," + rollups[month][marketplace];
    			console.log(line);
    			lines.push(line);
    
    			var rollupRec = rollupsForm.addNewRecord();
    			rollupRec.setFieldValue("fld-cd1d454672c84bce8103a4267507ca03", month);
    			rollupRec.setFieldValue("fld-4c9f208bb5ed406489a54a76d4b6cd18", marketplace);
    			rollupRec.setFieldValue("fld-9eeeff7120db401b830ccec4e06f2bc3", rollups[month][marketplace]);
    		}
    	}
    	
    	document.saveAllChanges();	
    
    	Utils.copyTextToClipboard(lines.join("\n"));
    }
    
    Aggregate_By_Date();
    
    #45313

    In reply to: Help with script

    Guillermo q
    Participant

    Sorry to bother you again guys.

    With this code you gave me:

    function Aggregate_By_Date() {
    	// set our date format, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
    	// remove "day" from it and you'll roll up by month for example.
    	var dateFormat = { "day": "2-digit", "month": "2-digit", "year": "2-digit"};
    	
    	// this is where we're going to store the rollups per day.
    	var rollups = {};
    	
    	// iterate to all of the records in the form
    	for (var rec of form.getRecords()) {
    		var purchase_date = rec.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd');
    		var price = parseFloat(rec.getFieldValue('fld-08129d71ab0f4fa4a2749456281fca07'));
    
    		// Skip entries that don't have a price or date set.
    		if (!price || !purchase_date) {
    			continue;
    		}
    		
    		// format the date for use in the rollup.
    		var formattedDate = purchase_date.toLocaleDateString("en-AU", dateFormat);
    
    		// Rollup to this date, add to the existing value or set it if not set.
    		rollups[formattedDate] ? rollups[formattedDate] += price : rollups[formattedDate] = price;
    	}
    	
    	// log to console the aggregated values.
    	for (var month in rollups) {
    		console.log(month + ": $" + rollups[month]);
    	}
    }
    
    Aggregate_By_Date();

    I have a field depending on who added this field (my wife or me). I would like this report by day, but with XX or XY depending on who added the field and the price.

    Thank you very much, you helped me a lot.

    #45312
    Kimberley Hoffman
    Participant

    Thanks, Brendan. I do follow Sam on YouTube. However, I realise I need to learn the basic vocabulary in order to string sentences together. Thank you for linking to JavaScripting 101.

    On the bright side of life, despite my struggle with the scripting, I did manage to turn out a work-around without one. And I was super happy that I could filter information easily and quickly for this data bank. That won me back time that I otherwise would have lost had I not set up the forms. Thanks.

Viewing 15 results - 1,186 through 1,200 (of 2,989 total)