I am interested in writing an iterative look up using python macros. Essentially the sheet on the spread sheet will have in Column A, a list of values (xxx, yyy, zzz, etc, …) There will be an occasional blank row which will be skipped, but I will not have up front knowledge of the precise length of the list. My plan is to us a for loop to cycle through the data and fetch the outside information and place it in column B adjacent to the item in col A.
A B
xxx 14
yyy 16
zzz 03 and so forth.
code snippet which will allow me to get the data is as follows:
import sys
import uno
import time
import urllib.request
from datetime import date
oDoc = XSCRIPTCONTEXT.getDocument()
def update():
oSheet = oDoc.CurrentController.ActiveSheet
oCell1 = oSheet.getCellRangeByName('A1')
url_stub = 'http://download.inventory.intranet/qty_avail?spartid='
part = 'ABSR'
url = url_stub + part + '&ac=qtyonhand'
r = urllib.request.urlopen(url)
oCell1.Value = r.read()
return()
This works. It calls the url with http://download.inventory.intranet/qty_avail?spartid=ABSR&ptyonhand, but obviously it only works once. It returns a number between 0 and n and stores (in this case a number in cell A1 on the current sheet.
What I would like to do is to do an iterative process across rows: (pseudo-code)
def update():
oSheet = oDoc.CurrentController.ActiveSheet
oCell1 = oSheet.getCellRangeByName('A1')
oCell2 = oSheet.getCellRangeByName(1,0)
url_stub = 'http://download.inventory.intranet/qty_avail?spartid='
for part in oSheet.oCell1. [ some method which ends when there is no more data in rows 1 and below ]
url = url_stub + part + '&ac=qtyonhand'
r = urllib.request.urlopen(url)
oCell2 = oSheet.getCellRangeByValue(1,row number)
oCell2.Value = r.read()
return()
If someone can point me in the right direction for the best method for detecting no more data below the final row, I’d be grateful. The rest of it, I think I can figure out. As I have struggled with python macros in LibreOffice, and am now pounding the old StarOffice documents, I’m going to try to put what I’ve learned into a consolidated document once I get through this project. All the stuff I’ve seen is mainly based on BASIC, but I think if I can figure out the python methods, it might be a better way to go. Thanks for any help.