First only one selected cell must be retrieved. Therefore store the old
selection into a variable, then create a new cell cursor out of the current
selection and then restore the old selection. Now create a new "URL" object
and insert it as text content of the cell.
For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.
-- try to get a script context, will be .nil, if script was not invoked by OOo
x_ScriptContext = uno.getScriptContext()
if (x_ScriptContext <> .nil) then
do
-- invoked by OOo as a macro
-- get context
x_ComponentContext = x_ScriptContext~getComponentContext
-- get desktop (an XDesktop)
x_Desktop = x_ScriptContext~getDesktop
-- get current document
x_Document = x_ScriptContext~getDocument
end
else
do
-- called from outside of OOo, create a connection
-- connect to Open Office and get component context
x_ComponentContext = UNO.connect()
-- create a desktop service and its interface
service = "{%see com.sun.star.frame.Desktop}"
s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service)
x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop}
-- get the last active document
x_Document = x_Desktop~getCurrentComponent()
end
/*
first we need to get the currently selected cell in the currently
selected sheet. If more than one cell is selected we first have to
reduce the amount of selected cells to only one, otherwise an error
occurs.
*/
-- query a list of spreadsheets (used later)
x_SpreadsheetDocument = x_Document~{%see com.sun.star.sheet.XSpreadsheetDocument%XSpreadsheetDocument}
x_Spreadsheets = x_SpreadsheetDocument~getSheets()
x_SpreadsheetIA = x_Spreadsheets~{%see com.sun.star.container.XIndexAccess%XIndexAccess}
-- now get the current selection
x_Model = x_Document~{%see com.sun.star.frame.XModel%XModel}
currentselection = x_Model~getCurrentSelection()
-- now create a new selection object with only one cell selected
s_CurrentController = x_Model~getCurrentController()
x_MultiServiceFactory = x_Document~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
newselection = x_MultiServiceFactory~createInstance("{%see com.sun.star.sheet.SheetCellRanges}")
-- use the new selection on the current sheet
x_View = s_CurrentController~{%see com.sun.star.view.XSelectionSupplier%XSelectionSupplier}
x_View~select(newselection)
-- get the currently selected cell and its address
noselectioncell = x_Model~getCurrentSelection()
x_CellAddressable = noselectioncell~{%see com.sun.star.sheet.XCellAddressable%XCellAddressable}
st_CellAddress = x_CellAddressable~getCellAddress()
-- restore old selection
x_View~select(currentselection)
-- get position of current cell and query cell object
currentcell.sheet = st_CellAddress~bsf.getFieldValue("Sheet")
currentcell.column = st_CellAddress~bsf.getFieldValue("Column")
currentcell.row = st_CellAddress~bsf.getFieldValue("Row")
s_Spreadsheet = x_SpreadsheetIA~getByIndex(currentcell.sheet)
x_Spreadsheet = s_Spreadsheet~{%see com.sun.star.sheet.XSpreadsheet%XSpreadsheet}
cell = uno.getCell(x_Spreadsheet, currentcell.column, currentcell.row)
-- create text interface on cell
x_TextRange = cell~{%see com.sun.star.text.XTextRange%XTextRange}
x_Text = x_TextRange~getText()
-- read cell textcontent
urlstring = x_Text~getString()
-- create url field
s_urlfield = x_MultiServiceFactory~createInstance("{%see com.sun.star.text.TextField.URL}")
urlproperties = s_urlfield~{%see com.sun.star.beans.XPropertySet%XPropertySet}
urlproperties~setPropertyValue("Representation", urlstring)
urlproperties~setPropertyValue("URL", urlstring)
-- clear cell and write urlfield
x_TextContent = s_urlfield~{%see com.sun.star.text.XTextContent%XTextContent}
x_Text~setString("")
x_Text~insertTextContent(x_Text~createTextCursor(), x_TextContent, .false)
::requires UNO.CLS