Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 168 >> Next

{
if((r = T % 62) < 10)
r += ' 0 '; else if(r < 36) r += 'A' - 10; else
r += 'a' - 36; buffer[ch_count++] = r;
}
buffer[ch_count] = 0;
// Make a copy of the string and return it
char *new_name = (char *)malloc(ch_count + 1); strcpy(new_name, buffer);
return new_name; // caller must free the memory
}
9.8.2 Obtaining the internal name of the calling cell
The steps for this are:
1. Get a reference to the calling cell using xlfCaller.
2. Convert the reference to a full address specifier complete with workbook and sheet name in R1C1 form using xlfReftext.
3. Get the name, if it exists, from the R1C1 reference using xlfGetDef.
The following two pieces of code list two member functions of the xlName class that, together, perform these steps.
bool xlName::SetToCallersName(void)
{
Clear();
// Get a reference to the calling cell cpp_xloper Caller;
int xl4 = Excel4(xlfCaller, &Caller, 0); Caller.SetExceltoFree();
if(xl4) // if xlfCaller failed return false;
return SetToRef(&Caller, true); // true: look for internal name
}
312
Excel Add-in Development in C/C++
bool xlName::SetToRef(xloper *p_ref_oper, bool internal)
{
Clear();
if((p_ref_oper->xltype & (xltypeSRef | xltypeRef)) == 0) return false;
//---------------------------------------------------------------
// Convert to text of form [Book1.xls]Sheet1!R1C1
//---------------------------------------------------------------
cpp_xloper RefTextR1C1;
int xl4 = Excel4(xlfReftext, &RefTextR1C1, 1, p_ref_oper); RefTextR1C1.SetExceltoFree();
if(xl4 || RefTextR1C1.IsType(xltypeErr)) return false;
//---------------------------------------------------------------
// Get the name, if it exists, otherwise fail.
//
// First look for an internal name (the default if the 2nd // argument to xlfGetDef is omitted).
//---------------------------------------------------------------
if(internal)
{
xl4 = Excel4(xlfGetDef, &m_RangeName, 1, &RefTextR1C1); m_RangeName.SetExceltoFree();
if(xl4 || !m_RangeName.IsType(xltypeStr)) return m_Defined = m_RefValid = false;
m_Worksheet = false;
// If name exists and is internal, add to the list.
// add_name_record() has no effect if already there. add_name_record(NULL, *this);
}
else
{
// Extract the sheet name and specify this explicitly cpp_xloper SheetName;
xl4 = Excel4(xlSheetNm, &SheetName, 1, p_ref_oper); SheetName.SetExceltoFree();
if(xl4 || !SheetName.IsType(xltypeStr)) return m_Defined = m_RefValid = false;
// Truncate RefTextR1C1 at the R1C1 part
char *p = (char *)RefTextR1C1; // need to free this
RefTextR1C1 = strchr(p, '!') +1;
free(p);
// Truncate SheetName at the sheet name p = (char *)SheetName;
SheetName = strchr(p, ']') +1; free(p);
xl4 = Excel4(xlfGetDef, &m_RangeName, 2, &RefTextR1C1, &SheetName); m_RangeName.SetExceltoFree();
if(xl4 || !m_RangeName.IsType(xltypeStr)) return m_Defined = m_RefValid = false;
Miscellaneous Topics 313
m_Worksheet = true;
}
return m_Defined = m_RefValid = true;
}
9.8.3 Naming the calling cell
Where internal names are being used, the task is simply one of obtaining a reference to the calling cell and using the function xlfSetName to define a name whose definition is that reference. However, repeated calls to a naive function that did this would lead to more and more names existing. The first thing to consider is whether the caller already has a name associated with it (see section 9.8.2 above).
Sometimes the reason for naming a cell will be to associate it with a particular function, not just a given cell. Therefore, it may be necessary to look at whether the calling function is the function for which the cell was originally named. If not, the appropriate cleaning up or undoing of the old association should occur where necessary. If the name already exists, and is associated with the calling function, then no action need be taken to rename the cell.
The following code lists the member function of xlName that names the calling cell, if not already named. Note that if the name is specified and a name already exists, it deletes the old name before creating the new one.
bool xlName::NameCaller(char *name)
{
//-------------------------------------------------------------------
// Check if given internal name already exists for this caller
//-------------------------------------------------------------------
if(SetToCallersName() && !m_Worksheet)
{
// If no name specified, then the existing name is what's required if(!name || !*name) return true;
// Check if name is the same as the specified one if(m_RangeName == name) return true;
// If not, delete the old name, create a new one.
Delete();
}
//-------------------------------------------------------------------
// If no name provided, create a unique name
//-------------------------------------------------------------------
if(!name || !*name)
{
name = make_unique_name(); m_RangeName = name; free(name);
}
else
{
m_RangeName = name;
}
314
Excel Add-in Development in C/C++
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 168 >> Next