I’ve been a programmer for years but I can’t figure this out.
I simply want to paste a single character into the currently selected cell (not paste from clipboard) when I press a command button.
Thanks,
RIcky Wayne Hunt
I’ve been a programmer for years but I can’t figure this out.
I simply want to paste a single character into the currently selected cell (not paste from clipboard) when I press a command button.
Thanks,
RIcky Wayne Hunt
ThisComponent.getCurrentSelection()
can be a cell range, a selection of multiple ranges, any kind of rectangular object or a single cell. The following snippets apply to a single cell only:
objCell.setValue(1)
number
objCell.setFormula("1")
equivalent to the previous
objCell.setFormula("'1")
numeric string “1”
objCell.setString("1")
equivalent to the previous
In the UI, there is always one active cell, the cell whose formula is shown in the formula bar. The API has no built-in method to get that cell directly.
This is a function, I use to use since decades:
Function getActiveCell(Optional oView )
Dim as1(), lSheet&,lCol&,lRow$, sDum as String,bErr as Boolean
If isMissing(oView) then oView = ThisComponent.getCurrentController()
as1() = Split(oView.ViewData, ";")
lSheet = CLng(as1(1))
sDum = as1(lSheet +3)
as1() = Split(sDum, "/")
on error goto errSlash
lCol = CLng(as1(0))
lRow = CLng(as1(1))
on error goto 0
getActiveCell = oView.Model.getSheets.getByIndex(lSheet).getcellByPosition(lCol,lRow)
exit Function
errSlash:
if NOT(bErr) then
bErr = True
as1() = Split(sDum, "+")
resume
endif
End Function
Since each sheet of a Calc document can also have its own active cell, the following macro can also be suggested:
' lang:en
' For a Calc document or a Calc sheet, returns the active cell.
' On Error returns Nothing.
' The arg parameter (ThisComponent by default) can be an object representing a Calc document or a Calc sheet.
' If the document is in print preview mode, returns Nothing.
Function GetActiveCell(Optional Byval obj as Object) as Object
Dim oDoc as Object, nSheet as Long, s as String, arr, arr2
If IsMissing(obj) Then obj = ThisComponent
GetActiveCell = Nothing
On Error Goto ErrLabel
If HasUnoInterFaces(obj, "com.sun.star.sheet.XSpreadsheet") Then
oDoc = obj.DrawPage.Forms.Parent
nSheet = obj.RangeAddress.Sheet
Else
oDoc = obj
nSheet = -1
End If
arr = Split(oDoc.CurrentController.ViewData, ";")
If nSheet<0 Then nSheet=arr(1)
s = arr(3+nsheet)
arr2=Split(s, IIf(Instr(1, s, "+")>0, "+", "/"))
GetActiveCell = oDoc.Sheets(nSheet).getCellByPosition(CLng(arr2(0)), CLng(arr2(1)))
ErrLabel:
End Function