Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / SQL

Excel user defined functions unlimited

Rate me:
Please Sign up or sign in to vote.
4.81/5 (15 votes)
30 Nov 200619 min read 222.8K   5.7K   77  
Describes how to return tables of values as the result of user defined functions (UDF) in Excel.
//---------------------------------------------------------------------------
//  Excel Multivalue Formula Add-In
//  Copyright (C) <2005> <Herbert Danler>
//  Contact: danler@users.sourceforge.net
//  Project Home Page: http://excelmvf.sourceforge.net/
//
//  This program is free software; you can redistribute it and/or modify it under
//  the terms of the GNU General Public License as published by the Free Software
//  Foundation; either version 2 of the License, or (at your option) any
//  later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
//  or FITNESS FOR A PARTICULAR PURPOSE.
//  See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along with
//  this program; if not, write to the Free Software Foundation, Inc.,
//  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//---------------------------------------------------------------------------

/***********************************************************************
 * Module:  TRequestController.cpp
 * Author:  Herbert Danler
 * Modified: Donnerstag, 28. Oktober 2004 16:47:26
 * Purpose: Implementation of the class TRequestController
 ***********************************************************************/

#include "stdafx.h"
#include "TRequestController.h"

int TRequestController::cachingperiod=10;

TFuncParams TRequestController::funcParams;

////////////////////////////////////////////////////////////////////////
// Name:       TRequestController::getOrderItems(Variant pstartdate, Variant penddate, std::string psortcriterias)
// Purpose:    Implementation of TRequestController::getOrderItems()
// Parameters:
// - pstartdate
// - penddate
// - psortcriterias
// Return:     const  TOrderItemDataset*
////////////////////////////////////////////////////////////////////////

TOrderItemDataset* TRequestController::getOrderItems(VARIANT pstartdate, VARIANT penddate, std::string psortcriterias)
{
   //look in the container if data is already locally stored
   TOrderItemDataset temp(pstartdate, penddate,psortcriterias);
   pair<set<TOrderItemDataset>::iterator,bool> pos;
   time_t currtime;
   pos.first = orderItemDatasetContainer.find(temp);
   if (pos.first==orderItemDatasetContainer.end()) {
        temp.retrieve();
        pos = orderItemDatasetContainer.insert(temp);
   }
   else{
        //check if data is out of date
        if ((*pos.first).getTimestamp()<(time(&currtime)-cachingperiod)){
                orderItemDatasetContainer.erase(pos.first);
                temp.retrieve();
                pos.first = orderItemDatasetContainer.insert(temp).first;
        }
   }
   return &(*pos.first);
}

////////////////////////////////////////////////////////////////////////
// Name:       TRequestController::getCustomers()
// Purpose:    Implementation of TRequestController::getCustomers()
// Return:     const  TCustomerDataset*
////////////////////////////////////////////////////////////////////////

TCustomerDataset* TRequestController::getCustomers(std::string psortcriterias)
{
   //look in the container if data is already locally stored
   TCustomerDataset temp(psortcriterias);
   pair<set<TCustomerDataset>::iterator,bool> pos;
   time_t currtime;
   pos.first = customerDatasetContainer.find(temp);
   if (pos.first==customerDatasetContainer.end()) {
        temp.retrieve();
        pos = customerDatasetContainer.insert(temp);
   }
   else{
        //check if data is out of date
        if ((*pos.first).getTimestamp()<(time(&currtime)-cachingperiod)){
                customerDatasetContainer.erase(pos.first);
                temp.retrieve();
                pos.first = customerDatasetContainer.insert(temp).first;
        }
   }
   return &(*pos.first);
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions