Format a column of dates

I import multiple .csv sheets of stock data, per day.


When I bring them into Numbers, I want to use AppleScript to format the column of dates not as text, but as dates. If I were doing this manually, which is very repetitious, I would highlight the dates column (it's column A) and in inspector format, I would choose cell, Data Format "Date/Time", and the choice for displaying dates is "01/01/19".


Is there an easy way to format the column in this way, using AppleScript??


Thanks.


MacBook Air (2018 or later)

Posted on Jan 2, 2019 7:52 PM

Reply
9 replies

Jan 4, 2019 11:24 AM in response to Sheppad

why not expand your applescript thinking to combine all the CSV files together and do one import, then format (manually or Applescript) the one column? i.e. Why stop at just the simplest step of formatting a column? If your going to automate it, automate the whole process from start to end.


to see how to use applescript to format a cell, you can visit a site like this one, which i think even apples website links to somewhere...

https://iworkautomation.com/numbers/cell-format.html


Jason


Jan 7, 2019 4:10 PM in response to Sheppad

It is easy to use a short script "fix" the column of dates associated with historical stock prices.


However, as posted above, it is necessary to know:


  • The date format is in the csv file. Is year is it mm/dd/yy, dd/mm/yy or something else? (It's hard to tell from your post, and a csv file does not have "Automatic.")
  • The date formats used in the settings for your region. (I was guessing Numbers doesn't recognize the dates as true date-time because the month and day are in a different order from what is used in your region. But then you seem to suggest Numbers is already giving them an Automatic format, which would mean it does recognize them as dates).


SG

Jan 7, 2019 7:52 PM in response to Sheppad

I took the data you posted, copy-pasted it into TextEdit, and saved as csv file. Then I opened that file with Numbers. I got this:




The values in the date column were left-aligned, and as can be seen lower-left Numbers guessed they were text.


But all I had to do was select the values in the column and give them a Data Format of Date & Time.




Numbers then recognized them as dates, which can be sorted as dates.



SG





Jan 7, 2019 3:05 PM in response to SGIII

When i look at the csv file, it shows as automatic. If I do not tweak the dates, when I perform the sort, it does this to dates:


01/02/18

01/02/19

01/03/18

01/03/19

01/04/18


I am wanting this:


01/02/18

01/03/18

01/04/18

01/02/19

01/03/19


Of course, I have a full year of dates per file, so there is much more to sort. I copy one csv for a stock such as Apple. Then after the csv is sorted, I pasted it into the Apple sheet, to update the data. I then retrieve another stock, such as Boeing. The csv for Boeing has to be sorted next. Then pasted into the spreadsheet in the Boeing tab.



Jan 7, 2019 7:35 PM in response to SGIII

The csv table looks like this:


Date Open High Low Close Volume

01/07/19 88.37 89.08 82.24 82.45 17428190

01/04/19 86.84 88.71 86.74 88.13 6275390

01/03/19 87.62 87.73 85.18 85.45 6099839

01/02/19 89.53 90.14 87.09 87.92 7099122

12/31/18 89.92 91.21 89.6 90.96 5436421

12/28/18 90.21 90.64 88.95 89.26 4508057

12/27/18 87.45 89.66 86.38 89.65 6015444


Of course, it is nice and lined up in the spreadsheet.


To answer your questions:


The date in the csv is 01/07/2019 format. mm/dd/yy


I do not know what format the date is in, when I import it. When I click on the column and go to format cells, Automatic is highlighted.


The date comes in as the 01/07/19 format, but it will not sort these dates as dates. I would almost suggest they come in as a fixed number or something.


Until I change the column to sort and Date & Time (format 01/07/19), it will not sort correctly.



Jan 10, 2019 4:18 PM in response to Sheppad

To run an AppleScript you need to select the cells and trigger the script, either via a 'run' button, a menu choice, or a keyboard shortcut.


To use the built-in Numbers formatting, you need to select the cells and make a menu choice in the format panel.


In this case I think you will find the built-in formatting, as described, to be just as easy.


One could write a script that selects the csv file, has Numbers open it, and then formats the column. But unless you are doing this operation many times a minute I don't think spending the time to write a script makes much sense.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Format a column of dates

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.