Format date field in CSV file for import via script

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Format date field in CSV file for import via script

Viewing 6 reply threads
  • Author
    Posts
  • October 25, 2023 at 5:05 PM #50041

    Victor Warner
    Participant

    Some time ago, I got help with how to import the data from a CSV file with a script (see the post at https://www.tapforms.com/forums/topic/using-javascript-to-import-csv-files/#post-45422).

    One of the fields in the CSV contains a date in the form “01/01/1963”, and the aim is to import it into a date field in the form.

    Although the script runs fine and imports the data in the CSV, the date field is not imported.

    Because of the way the script was written, I do not know how to change the format of the date field so that the date field is correctly formatted for import.

    Help would be very gratefully received.

     

    October 25, 2023 at 10:06 PM #50042

    Daniel Leu
    Participant

    Can you share a sample csv file and your form template?

    October 26, 2023 at 1:13 AM #50043

    Victor Warner
    Participant

    Daniel,

    Thank you. Files attached.

    Victor

    Attachments:
    You must be logged in to view attached files.
    October 27, 2023 at 1:00 PM #50054

    Brendan
    Keymaster

    Looks like the parser code expects date fields to be in ISO format:

    var ISO_DATE = /^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))$/;

    It’s using a regular expression to parse the date. So they must be in this format:

    yyyy-MM-dd (e.g. 2023-10-27)

    Although I’m not sure what the parser will do if it doesn’t get the rest of the time value.

    October 27, 2023 at 1:01 PM #50055

    Brendan
    Keymaster

    Also, that’s a huge amount of code just to parse a CSV file. Way more code than even I wrote for parsing CSV files.

    Just out of curiosity, have you tried the built-in Import Records function with your CSV file?

    October 31, 2023 at 3:03 AM #50065

    Victor Warner
    Participant

    Thank you, Brendan,

    I did not realise the complexity involved.

    Why use a script rather than the Import Records function? I am transferring a series of records from FileMaker to TapForms (each with 4 to 7 forms). But I have to transfer them one record at a time.  The Import Records function involves a series of key presses or uses of the mouse (x 4 to 7 forms). Although automating these with Keyboard Maestro is possible, it still takes some time (as KM just automates what is done manually).

    Using a script essentially automates the process – and is far quicker.

    The date issue is resolved by searching and replacing the date in the CSV file with a regular expression before running the script.

    October 31, 2023 at 11:26 AM #50066

    Brendan
    Keymaster

    ah good to know you found a solution to the date import issue.

Viewing 6 reply threads

You must be logged in to reply to this topic.