How do we add a date picker to a cell in Numbers?
Hi,
I would like so that when I double click on a particular cell, a calendar date picker comes up so I can pick a date to assign to the cell.
Does anyone know how to do this?
Hi,
I would like so that when I double click on a particular cell, a calendar date picker comes up so I can pick a date to assign to the cell.
Does anyone know how to do this?
It can be done with a script but it is not perfect. If you want to try it out, open the Script Editor app and paste the script below into a new window. Select a cell or range of cells in Numbers. Go back to Script Editor and hit the "Play" button. The selected cells should get the date or date&time, whichever you chose.
The imperfect part is that the picker does not highlight the chosen date when you are picking. It will give you the date you click on but there is no indication of that date in the picker. The picker itself is part of "AppKit", not something a user has written, so we have no way to correct that problem that I know of.
If you like it, you can use Automator to turn it into a "Quick Action" for Numbers which will put it in the Numbers/Services menu and you can assign a keyboard shortcut to it. One day I should probably write down the instructions to do that and save them (versus writing them, posting them in a thread, but not saving them for an easy copy/paste next time). If you need assistance with those steps, let us know.
-- Author: Shane Stanley
-- Edited by Badunit for use within the Numbers app
-- This version is edited to work around Apple's newly introduced selection range bug
-- Nov 22, 2021
use scripting additions
use framework "AppKit"
property thedate : missing value
property returncode : missing value
on run
its performSelectorOnMainThread:("showDatePicker:") withObject:{} waitUntilDone:true
tell application "Numbers" to tell the front document to tell active sheet to tell (first table whose class of selection range is range)
set therange to every cell of range (get name of selection range)
repeat with c in therange
set value of c to thedate
end repeat
end tell
end run
on showDatePicker:params
local params
set datePicker to current application's NSDatePicker's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 100))
-- set datepicker style: choices are
-- NSTextFieldAndStepperDatePickerStyle
-- NSClockAndCalendarDatePickerStyle
-- NSTextFieldDatePickerStyle
datePicker's setDatePickerStyle:(current application's NSClockAndCalendarDatePickerStyle)
-- set datepicker elements: choices include
-- NSHourMinuteDatePickerElementFlag
-- NSHourMinuteSecondDatePickerElementFlag
-- NSTimeZoneDatePickerElementFlag
-- NSYearMonthDatePickerElementFlag
-- NSEraDatePickerElementFlag
datePicker's setDatePickerElements:((current application's NSYearMonthDayDatePickerElementFlag) + (current application's NSHourMinuteSecondDatePickerElementFlag as integer))
-- set datepicker initial date
datePicker's setDateValue:(current application's NSDate's |date|())
-- set datepicker size
set theSize to datePicker's fittingSize()
datePicker's setFrameSize:theSize
-- set the view
set theView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 200))
theView's setFrameSize:theSize
-- add the picker to the view
theView's setSubviews:{datePicker}
-- create an alert
set theAlert to current application's NSAlert's alloc()'s init()
-- set up alert
tell theAlert
its setMessageText:"Pick a Date"
its setInformativeText:""
its addButtonWithTitle:"Date & Time"
its addButtonWithTitle:"Date Only"
its addButtonWithTitle:"Cancel"
its setAccessoryView:theView
end tell
-- show alert in modal loop
set returncode to theAlert's runModal()
-- retrieve date. First button is date & time. Second is date with time set to 12:00AM. Third is cancel
if returncode = (current application's NSAlertFirstButtonReturn) then
set thedate to datePicker's dateValue() as date
else if returncode = (current application's NSAlertSecondButtonReturn) then
set thedate to datePicker's dateValue() as date
set time of thedate to 0
else
error number -128
end if
end showDatePicker:
It can be done with a script but it is not perfect. If you want to try it out, open the Script Editor app and paste the script below into a new window. Select a cell or range of cells in Numbers. Go back to Script Editor and hit the "Play" button. The selected cells should get the date or date&time, whichever you chose.
The imperfect part is that the picker does not highlight the chosen date when you are picking. It will give you the date you click on but there is no indication of that date in the picker. The picker itself is part of "AppKit", not something a user has written, so we have no way to correct that problem that I know of.
If you like it, you can use Automator to turn it into a "Quick Action" for Numbers which will put it in the Numbers/Services menu and you can assign a keyboard shortcut to it. One day I should probably write down the instructions to do that and save them (versus writing them, posting them in a thread, but not saving them for an easy copy/paste next time). If you need assistance with those steps, let us know.
-- Author: Shane Stanley
-- Edited by Badunit for use within the Numbers app
-- This version is edited to work around Apple's newly introduced selection range bug
-- Nov 22, 2021
use scripting additions
use framework "AppKit"
property thedate : missing value
property returncode : missing value
on run
its performSelectorOnMainThread:("showDatePicker:") withObject:{} waitUntilDone:true
tell application "Numbers" to tell the front document to tell active sheet to tell (first table whose class of selection range is range)
set therange to every cell of range (get name of selection range)
repeat with c in therange
set value of c to thedate
end repeat
end tell
end run
on showDatePicker:params
local params
set datePicker to current application's NSDatePicker's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 100))
-- set datepicker style: choices are
-- NSTextFieldAndStepperDatePickerStyle
-- NSClockAndCalendarDatePickerStyle
-- NSTextFieldDatePickerStyle
datePicker's setDatePickerStyle:(current application's NSClockAndCalendarDatePickerStyle)
-- set datepicker elements: choices include
-- NSHourMinuteDatePickerElementFlag
-- NSHourMinuteSecondDatePickerElementFlag
-- NSTimeZoneDatePickerElementFlag
-- NSYearMonthDatePickerElementFlag
-- NSEraDatePickerElementFlag
datePicker's setDatePickerElements:((current application's NSYearMonthDayDatePickerElementFlag) + (current application's NSHourMinuteSecondDatePickerElementFlag as integer))
-- set datepicker initial date
datePicker's setDateValue:(current application's NSDate's |date|())
-- set datepicker size
set theSize to datePicker's fittingSize()
datePicker's setFrameSize:theSize
-- set the view
set theView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 200))
theView's setFrameSize:theSize
-- add the picker to the view
theView's setSubviews:{datePicker}
-- create an alert
set theAlert to current application's NSAlert's alloc()'s init()
-- set up alert
tell theAlert
its setMessageText:"Pick a Date"
its setInformativeText:""
its addButtonWithTitle:"Date & Time"
its addButtonWithTitle:"Date Only"
its addButtonWithTitle:"Cancel"
its setAccessoryView:theView
end tell
-- show alert in modal loop
set returncode to theAlert's runModal()
-- retrieve date. First button is date & time. Second is date with time set to 12:00AM. Third is cancel
if returncode = (current application's NSAlertFirstButtonReturn) then
set thedate to datePicker's dateValue() as date
else if returncode = (current application's NSAlertSecondButtonReturn) then
set thedate to datePicker's dateValue() as date
set time of thedate to 0
else
error number -128
end if
end showDatePicker:
Unless it has been added recently, this is not a supported feature in Numbers.
Use the Provide Numbers Feedback menu item n the Numbers menu to make a feature enhancement request directly to Apple.
Regards,
Barry
It is all there. The "code" box has scroll bars. Just to be sure I could select it all, I click-dragged from top to bottom to select all the text in the box, pasted it into Script Editor, and ran it no problem.
Hi Viking OSX,
Is the script presented in your post above complete? It appears that the right ends of several lines were truncated at a fixed right margin.
Regards,
Barry
Thanks for the clarification. Happy to know there's noting missing.
Regards,
Barry
How do we add a date picker to a cell in Numbers?