Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Find and replace script – to replace anything
Tagged: Find and Replace
- This topic has 11 replies, 3 voices, and was last updated 4 years, 8 months ago by Sam Moffatt.
-
AuthorPosts
-
April 5, 2020 at 1:14 AM #40227
Victor WarnerParticipantI found the find and replace script posted by Brendan at https://www.tapforms.com/forums/topic/find-and-replace-script/ very helpful, but would like some help in adapting it:
var field_id = 'fld-64b0b17a635f49b8b40867e45f8d24db'; function findAndReplace(find_text, replace_with) { var records = form.getRecords(); for (var index = 0, count = records.length; index < count; index++){ var rec = records[index]; var value = rec.getFieldValue(field_id); if (value == find_text) { value = replace_with; rec.setFieldValue(field_id, value); } } document.saveAllChanges(); return; } findAndReplace('Ford', 'Tesla');
specifically:
1. at present it searches for a specific item in a specific field. How is it possible to search for anything in that field and replace with specific text (so rather than ‘Ford’ in Brendan’s example, there might be Ford, Mercedes or BMW etc). I have tried adding a wildcard indicator (*), but it does not work.
2. A variation on 1. is where there is nothing in the specific field. Changing ‘Ford’ in Brendan’s example, to ” also does not work.
Is this possible with the script?
Any help would be gratefully received.
April 5, 2020 at 8:53 AM #40233
Daniel LeuParticipantEverything is possible with a script, well almost ;-)
Here is the version that doesn the replacement of strings in a field:
var field_id = 'fld-64b0b17a635f49b8b40867e45f8d24db'; function findAndReplace(find_text, replace_with) { for (let rec of form.getRecords()){ let value = rec.getFieldValue(field_id); if (value) { rec.setFieldValue(field_id, value.replace(find_text, replace_with)); } } form.saveAllChanges(); return; } findAndReplace("Ford", 'Tesla');
And this version replaces an empty field with the replacement text:
var field_id = 'fld-34fb289f2b124f26982df66430b31fbd'; function findAndReplace(find_text, replace_with) { for (let rec of form.getRecords()){ let value = rec.getFieldValue(field_id); if (find_text === ""){ // special case with empty string if (value == undefined){ rec.setFieldValue(field_id, replace_with); } } else { if (value){ rec.setFieldValue(field_id, value.replace(find_text, replace_with)); } } } form.saveAllChanges(); return; } findAndReplace("Ford", 'Tesla');
Enjoy!
April 5, 2020 at 11:47 AM #40236
Victor WarnerParticipantDaniel,
Thank you very much for the two codes.
For the first one, it only works *at all* when replacing the last line from
findAndReplace("Ford", 'Tesla');
to
findAndReplace("", 'Tesla');
But it does not replace the existing text but adds the replacement text to the beginning of the existing text.
Could you provide some additional help?
April 5, 2020 at 12:03 PM #40237
Daniel LeuParticipantThe first one is a regular search and replace. If you have an empty search string, which is not really a search, you need to use the second code block.
April 5, 2020 at 3:32 PM #40239
Victor WarnerParticipantThank you for the further explanation. But I am clearly missing something.
In the attached sample Form I have a field “Test” with 3 records, containing 1, 2, 3.
I would like to replace all the entries with 0, but neither set of code works (creating Form Scripts you provided). The first one (“F&R – anything from something”) adds a 0 to 1, 2, 3 (01, 02, 03) while the second one (“F&R – nothing with something) does nothing at all.
Am I doing something wrong? Any further help would be gratefully received.
Attachments:
You must be logged in to view attached files.April 5, 2020 at 4:34 PM #40241
Daniel LeuParticipantThank you for sharing your script, Victor.
The first script is NOT to be used with empty text. You need to use the second script for that. I deleted one test field and run the second script which properly updates the empty field with 0.
I created two scripts so others can profit as well. I don’t consider search for “” and replace with “something” to be a normal use case. That’s why I added the second script. For your use case, you only use the second script! It does a regular search and replace, and for cases where the field is empty, it puts the replacement text in there.
April 5, 2020 at 6:16 PM #40242
Victor WarnerParticipantOkay, but neither work were
1. the 3 Test fields contain, respectively, 1, 2, and 3; and
2 I wish to replace with them with 0.The second does, as you write, does add a 0 where the field is empty. That is one problem I am trying to solve. The other is to replace any text appearing in the field with a 0 (as a I mentioned already, the first script, simply adds the replacement 0 to the existing text and not replaces it). For the second problem, in conventional find and replace, the find part would done through a wildcard
My problem is that I do not know JavaScript so I do not know how to fix it to do it what I want.
April 5, 2020 at 7:48 PM #40243
Daniel LeuParticipantFor the second problem, in conventional find and replace, the find part would done through a wildcard
You can use regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
April 6, 2020 at 2:32 PM #40252
Victor WarnerParticipantThank you, but a brief look at the link does not provide how, what and where I add the relevant regular expression. The code provided in the linked page does not seem to relate at all (for someone who does not know JavaScript) to the examples you have provided.
Can you, Brendan, or someone else provide how it is possible to search for any text appearing in a field (as a wildcard, whether with regular expression or without) and replace it with something?
April 6, 2020 at 5:12 PM #40254
Daniel LeuParticipantSorry that my solution is not sufficient. Did you even try the most obvious solution like
findAndReplace(/[a-zA-Z0-9]/, 'replace');
?My link applies. And Google can find you other resources like this one: https://www.w3schools.com/jsref/jsref_obj_regexp.asp
April 27, 2020 at 5:10 AM #40376
Victor WarnerParticipantDaniel,
Apologies for my comments, they were not intended to disparage your helpful replies – more my frustration with trying to understand the pages you linked (mainly to do with JavasScript for websites) and its relevance to Tap Forms (and the sparse practical information currently available) for someone who does not understand it.
Anyway I have finally worked out where to put
findAndReplace(/[a-zA-Z0-9]/, ‘replace’)
That is right at the end of the code you provided and now provides what I need. Thank you for providing code.
April 27, 2020 at 9:15 AM #40378
Sam MoffattParticipantMost of the Javascript references that are basic Javascript should be relevant to Tap Forms. There are some functions that are not part of the Javascript ES6 standard that web browsers commonly implement that you might not see. Most of the time anything that references
window
,document
and the DOM will not be relevant with Tap Forms. Unfortunately Javascript’s use in web browsers isn’t entirely standardised which leads to confusing documentation on the web. -
AuthorPosts
You must be logged in to reply to this topic.