def pyxll_stock_price(endpoint, api_token, symbol): """not supported in this version of Excel""" if aiohttp is None: return "aiohttp module could not be imported" if json is None: return "json module could not be imported" return "async functions are not supported in Excel %s" % xl_version()
def show_last_error(): selection = xl_app().Selection exc_type, exc_value, exc_traceback = get_last_error(selection) if exc_type is None: xlcAlert("No error found for the selected cell") return msg = "".join( traceback.format_exception(exc_type, exc_value, exc_traceback)) if xl_version() < 12: msg = msg[:254] xlcAlert(msg)
def yahoo_stock_price(symbol): """not supported in this version of Excel""" return "async functions are not supported in Excel %s" % xl_version()
performance. """ from pyxll import xl_func, xl_version, xlAsyncReturn # # this example uses urllib2 to perform an asynchronous http # request and return the data to Excel. # import urllib2 import threading # # Async functions are only supported from Excel 2010 # if xl_version() >= 14: @xl_func("string, async_handle: void") def yahoo_stock_price(symbol, handle): """returns the last price for a symbol from Yahoo Finance""" def thread_func(symbol, async_handle): result = None try: # get the price using an http request (f=l1 means get the last price) url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1" % symbol data = urllib2.urlopen(url).read() # the returned data is in csv format, but only has one row and one column result = float(data.strip()) except Exception, e:
except ImportError: _log.warning("json could not be imported. Async example will not work", exc_info=True) json = None try: import aiohttp except ImportError: _log.warning("aiohttp could not be imported. Async example will not work", exc_info=True) aiohttp = None # # Async functions are only supported from Excel 2010 # if xl_version() >= 14 and json is not None and aiohttp is not None: @xl_func async def pyxll_stock_price(endpoint, api_token, symbol): """Return the latest price for a symbol from iextrading.com""" url = "{endpoint}/stock/{symbol}/quote?token={api_token}".format( endpoint=endpoint, symbol=symbol, api_token=api_token) async with aiohttp.ClientSession() as session: async with session.get(url) as response: assert response.status == 200 data = await response.read() data = json.loads(data.decode("utf-8")) return data.get("latestPrice", "#NoLatestPrice") else:
performance. """ from pyxll import xl_func, xl_version, xlAsyncReturn # # this example uses urllib2 to perform an asynchronous http # request and return the data to Excel. # import urllib.request, urllib.error, urllib.parse import threading # # Async functions are only supported from Excel 2010 # if xl_version() >= 14: @xl_func("string, async_handle: void") def yahoo_stock_price(symbol, handle): """returns the last price for a symbol from Yahoo Finance""" def thread_func(symbol, async_handle): result = None try: # get the price using an http request (f=l1 means get the last price) url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1" % symbol data = urllib.request.urlopen(url).read() # the returned data is in csv format, but only has one row and one column result = float(data.strip()) except Exception as e:
xlcCalculateNow, xlcCalculateDocument, ) import pyxll import hashlib import win32com.client import json import requests import multiprocessing from multiprocessing.pool import ThreadPool # Excel 2010 or newer is needed since we require async_handle support. if xl_version() < 14: raise Exception("Gemini calls not supported in older Excel versions") # global hash which stores Gemini call results RESULTS = {} # FIXME: hard-coded pool size pool = ThreadPool(processes=20) def xl_app(): """returns a Dispatch object for the current Excel instance""" # get the Excel application object from PyXLL and wrap it xl_window = pyxll.get_active_object() xl_app = win32com.client.Dispatch(xl_window).Application # it's helpful to make sure the gen_py wrapper has been created