Hello
I tried to write a summary of the messages posted before.
Here it is :
What we must know to work with dates in Numbers.
(1) When we type a date value without a time component, the application complete it with the time value 0 which means 00:00:00.
(2) When we paste a date-text without a time component, the application complete it with the time value 0 which means 00:00:00.
(3) If we use GUIScripting to mimic the actions described above, we get the same results.
(4) If we use AppleScript to set the value of a cell to a date-text value the cell’s content will be a date.
If we set its format to display the time component we will see that this one matches the one available in the string (no time in the string will become 00:00:00).
Here in France, setting the value to the date-text 12/12/1943 will insert in fact 11/12/1943 00:00:00.
Here in France, setting the value to the date-text 8/8/2014 will insert in fact 8/8/2014 00:00:00.
(5) If we use AppleScript to extract the value from the cell filled with a date-text value (case 4), we will get a date value equal to the date plus the (time to GMT) value linked to the source date in the location where we do the job.
Here in France, the cell which was filled with “12/12/1943” will give 12/12/1943 01:00:00 (winter time).
Here in France, the cell which was filled with “08/08/2014” will give 08/08/2014 02:00:00 (summer time time).
(6)If we use AppleScript to set the value of a cell to a date value the cell’s content will be the date minus the (time to GMT) linked to the source date in the location where we do the job.
Here in France, setting the value to the date 12/12/1943 will insert in fact 11/12/1943 23:00:00 because the offset is one hour (winter time)
Here in France, setting the value to the date 8/8/2014 will insert in fact 7/8/2014 22:00:00 because the offset is two hours (summer time)
(7) If we use AppleScript to extract the value from the cell filled with a date value (case 6), we will get a date value equal to the original date which means, the stored date plus the (time to GMT) value linked to the date in the location where we do the job.
Here in France, the cell which was filled with date “12/12/1943” (it display 11/12/1943 23:00:00) will give 12/12/1943 00:00:00.
Here in France, the cell which was filled with date “08/08/2014” (it display 07/08/2014 22:00:00) will give 08/08/2014 00:00:00.
(8) If we need to be able to extract dates from a table using AppleScript, the safe way is to store the dates as text objects.
To do that, set value of the cell to the date value then set the format of the range (name of the cell) to text.
Of course, the table will be unable to make calculations upon these dates stored as text.
To be able to do that we will have to use an auxiliary column in which date-text values will be converted into date-time objects.
This may be achieved with this kind of code :
# Extract the localized names of two functions used below
set DATEVALUE_loc to localized string "DATEVALUE" from table "TSCalculationEngine" in bundle path to application "Numbers"
# in French --> DATEVAL
set TIMEVALUE_loc to localized string "TIMEVALUE" from table "TSCalculationEngine" in bundle path to application "Numbers"
# in French --> TEMPSVAL
set theDates to {"8/8/2014", "12/12/1943"}
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
repeat with i from 1 to 2
set maybe to (item i of theDates)
(*
# Inserts a date-text value in the cell. It will be deciphered automatically as a date
set value of cell 1 of row (i + 1) to maybe
# Inserts a formula which will convert the date in a true text object
set value of cell 2 of row (i + 1) to "=" & quote & quote & "&" & (name of cell 1 of row (i + 1))
*)
# Inserts a date-text value in the cell. It will be deciphered automatically as a date
set value of cell 3 of row (i + 1) to maybe
# Apply the format text to the date
set format of range (name of cell 3 of row (i + 1)) to text
# Insert a formula which convert the true date-text into a true date
set value of cell 4 of row (i + 1) to "=" & DATEVALUE_loc & "(" & name of cell 3 of row (i + 1) & ")+" & TIMEVALUE_loc & "(" & name of cell 3 of row (i + 1) & ")"
end repeat
end tell
I disabled the alternate scheme in which we leave the cells filled by the script in automatic date format and insert a formula converting the date into a true text object.
If we want to extract the date with a script, we will have to do that from the cells containing the formula converting date to text.
I know, it’s boring, but we can’t change the application behavior so the best thing to do is to learn how we may speak to it to achieve our goals.
Yvan KOENIG (VALLAURIS, France) jeudi 6 novembre 2014 10:55:18