Remove extra whitespace

Viewing 3 reply threads
  • Author
    Posts
  • January 31, 2022 at 11:26 PM #46548

    Prashant M
    Participant

    When importing an excel file into TF, note field has extra spaces and sometimes line breaks , how may I get ride of them inside TF

    PS : I already solved the issue outside TF by using some simple regex, but would love to know how to tackle this inside TF

    February 1, 2022 at 2:24 AM #46551

    Brendan
    Keymaster

    You would probably need to write a Form Script that looped through the records then fetched the values for the fields you want to trim and then use the JavaScript trim() function.

    February 1, 2022 at 6:04 AM #46556

    Prashant M
    Participant

    oh my , I read JS and immediately it’s out of my depth ! regex and bbedit to rescue

    February 1, 2022 at 9:42 AM #46557

    Daniel Leu
    Participant

    It is not that complicated! This is a form script that loops through all records. It reads that value of your note field, modifies it, and then writes it back. Additionally, it checks that the note field content is defined, otherwise further processing is skipped.

    The script currently replaces all line-breaks with a whitespace and removes all leading and trailing whitespaces. As you see, you can use your regex in JavaScript as well :)

    The only change you need to make is define the note_id constant according to your form script. There are several ways to get the id. One way is to click on the field name in the script editor, then select and copy the ID string that is displayed underneath all the field names.

    function Remove_Spaces() {
    
       const note_id = 'fld-xxx' // set note_id according to your form
    
       for (let rec of form.getRecords()){
    	
          // get note field
          let note = rec.getFieldValue(note_id)
          console.log("Orignial note: " + note)
    
          // Check that note is defined
          if (note) {
             // modify note (replace line breaks with
             // whitespaces and remove whitespaces from
             // both ends of string
             let modifiedNote = note.replace(/\n/g, " ").trim()
    			
             console.log("Modified note: " + modifiedNote)
    	
             // save note field
             rec.setFieldValue(note_id, modifiedNote)
          }
       }
    	
       document.saveAllChanges()
    }
    
    Remove_Spaces()
    • This reply was modified 2 years, 9 months ago by Daniel Leu. Reason: replaced tabs with ' ' for better readability
    • This reply was modified 2 years, 9 months ago by Daniel Leu.
    • This reply was modified 2 years, 9 months ago by Daniel Leu.
Viewing 3 reply threads

You must be logged in to reply to this topic.