The premise: I am working in libreoffice calc and need to send an instruction to another program that I know to be listening on a TCP port, via a macro. I am expecting a reply from the listening program and want to insert the reply data into the libreoffice spreadsheet.
Asked
Active
Viewed 2,231 times
0

Matt Ellen
- 11,268
- 4
- 68
- 90

Rolf of Saxony
- 21,661
- 5
- 39
- 60
-
1Is that a question? If you've got an answer you can __answer your own question__. – ForceBru Mar 06 '15 at 13:23
-
You've posted this [twice](http://stackoverflow.com/q/28896381/4354477) by the way. One of them can get closed as duplicate. – ForceBru Mar 06 '15 at 13:24
-
I use this site to see if anyone else has come up with an answer to a question that I am having trouble with. I assumed that posting a solution to such a question would be Okay. If I have broken some rule or other of this site, then I'll happily withdraw my post, if you can point me in the right direction. There are 2 similar posts, one is for libreoffice writer and the other is for libreoffice calc, they use slightly different techniques, which is one of the bug bears of the UNO api. Let me know if I should delete these posts, as I have 3 or 4 more to go. – Rolf of Saxony Mar 06 '15 at 16:10
-
You have not broken a rule. To help reduce traffic from those who answer questions I recommend putting "(self answer)" at the end. Then the question and answer are generally considered by the reviewers as a unit. Might not be worth doing at this point, but worth trying in the future. – Quaternion Mar 07 '15 at 19:50
1 Answers
2
Having been helped many times over by searching stackoverflow, I thought that I would post a solution to a problem which took much effort to resolve. The code splits the data into lines and inserts into the cell in which the cursor is presently positioned and for each line of subsequent data increments the row for the next insert.
Configobj is a package that reads parameters from a flat file. In this example, I am using that file to store the TCP port to be used. Both the listening program and this code are reading the port number from the same configuration file. It could have been hard coded.
Here is a python macro that works for me, I trust that it will point others in the right direction.
def fs2ClientdataCalc(*args):
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
try:
sheets = model.getSheets()
except AttributeError:
raise Exception("This script is for Calc Spreadsheets only")
#sheet = sheets.getByName('Sheet1')
sheet = model.CurrentController.getActiveSheet()
oSelection = model.getCurrentSelection()
oArea = oSelection.getRangeAddress()
first_row = oArea.StartRow
last_row = oArea.EndRow
first_col = oArea.StartColumn
last_col = oArea.EndColumn
#get the string from Footswitch2 via a TCP port
import os, socket, time
from configobj import ConfigObj
configuration_dir = os.environ["HOME"]
config_filename = configuration_dir + "/fs2.cfg"
if os.access(config_filename, os.R_OK):
pass
else:
return None
cfg = ConfigObj(config_filename)
#define values to use from the configuration file
tcp_port = int(cfg["control"]["TCP_PORT"])
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
try:
sock.connect(("localhost", tcp_port))
except:
return None
sock.settimeout(10)
try:
sock.send(bytes('client\n', 'UTF-8'))
except:
return None
try:
time.sleep(1.0)
s_list = sock.recv(4096).decode('UTF-8')
s_list = s_list.split("\n")
except:
return None
lines_in_response = len(s_list)
if lines_in_response is None:
return None
column =['A','B','C','D','E','F','G','H','I','J','K','L','M',\
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for x in range(0,lines_in_response):
insert_table = s_list[x].split("\n")
parts = len(insert_table)
for y in range(0,parts):
it = insert_table[y]
cell_name = column[first_col + y]+str(x +1 +first_row)
cell = sheet.getCellRangeByName(cell_name)
cell.String = it
sock.close()
return None

Rolf of Saxony
- 21,661
- 5
- 39
- 60
-
Very helpful example, Rolf. Can I ask, how did you learn that the socket library was available? `import socket` – Jannie Theunissen Feb 08 '17 at 10:42
-
@JannieT The mists of time! I was using unix sockets to send key strokes into the "fake tty" interface of vlc, based on which pedal had been pressed on a foot pedal but that was a long time ago, before I discovered `evdev` – Rolf of Saxony Feb 08 '17 at 10:54
-
hahaha. My question wasn't so clear, but just found this also helpful http://stackoverflow.com/questions/3952513/get-available-modules Keep posting the good stuff! – Jannie Theunissen Feb 08 '17 at 11:51
-
1@JannieT Indeed, I misunderstood the question. As `socket` is a standard library, it didn't occur to me. As in your provided link, in case anyone reads your question and the link disappears: On the python command line, type in `help()` and then `modules` which provides a list of all modules available. It is especially useful if you are using a big module like wx, where `modules wx` provides all of the sub modules available i.e. list the modules with the key word `wx` in them – Rolf of Saxony Feb 08 '17 at 16:56