Executing python code from a gnumeric sheet



I wonder whether this is worth anything? For
small bits of code. Code (properly formatted)
in the first column. Stops when it sees an END.

import Gnumeric

def exec_sheet_obj(codeSheetObj):
    # Initialise
    codeStr = ""
    rowNum = 0
    # Get the code
    while (True):
        # Get the current line of possible code
        currStr = codeSheetObj.cell_fetch(0, rowNum).get_value()
        # Append if legal, stop if we see END (do not include it)
        if (currStr == "END"):
            break
        elif (currStr):  # No empty strings (and comments)
            if (currStr.strip() != "" and currStr[0] != "#"):
                codeStr += currStr + "\n"
        rowNum += 1
    # Execute code in its own environment.
    if (codeStr != ""):
        codeObj = compile(codeStr, "<string>", "exec")
        exec(codeObj, {}, {})
    return

def exec_sheet_name(sheetName):
    codeSheetObj = None
    # No way to access sheets so we are stuck with the first.
    workBook = Gnumeric.workbooks()[0]
    # Get sheet object
    for sheet in workBook.sheets():
        if sheet.get_name_unquoted() == sheetName:
            codeSheetObj = sheet
    if (codeSheetObj):
        exec_sheet_obj(codeSheetObj)
    return

Apologies if this is not the correct list, or this
is too stupid, insecure, whatever to bother with. 

You have to run it from the inbuilt python console,
as a plugin it is a mess.

cheers
phansi




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]