I have a number of microcontrollers which send data to my PC. I can control the format of the data stream. I would like to inport “paste special” to data direct to a calc sheet.
Is this possible?
if so how do I do it?
Write a Python/Java/JavaScript program which generates data arrays from the incoming stream and use the UNO API to dump the data array to sheet cells.
A data array is a nested array, one array of data per row. Spreadsheet data can be integers, doubles or strings.
Simple code appending a given data array (int, string, double) to a given sheet:
def test():
doc = XSCRIPTCONTEXT.getDocument()
sh = doc.Sheets.getByIndex(0)
tpl = ((1,'A',12.98),(2,'B',13.11),(3,'C',999.99))
appendDataArray(sh, tpl)
def appendDataArray(sheet, da):
'''Append data array below used range of given sheet'''
cr = getCurrentRegion(sheet.getCellByPosition(0,0))
cra = cr.getRangeAddress()
sc = cra.StartColumn
sr = cra.EndRow +1
ec = cra.StartColumn + len(da[0]) -1
er = cra.EndRow + len(da)
# print(sc,sr,ec,er)
rg = sheet.getCellRangeByPosition(sc, sr, ec, er)
rg.setDataArray(da)
def getCurrentRegion(oRange):
"""Get current region around given range."""
oCursor = oRange.getSpreadsheet().createCursorByRange(oRange)
oCursor.collapseToCurrentRegion()
return oCursor