The first example demonstrates how to copy numeric values from an OPER to a single array of double's. The bByRows flag is set to FALSE, so columns are grouped together into the output array. The bStrict flag is set to FALSE, so non-numeric values are treated as zeroes.
//{{XLP_SRC(OperToDoubleArrayExample1) // NOTE - the FunctionWizard will add and remove mapping code here. // DO NOT EDIT what you see in these blocks of generated code! IMPLEMENT_XLLFN2(OperToDoubleArrayExample1, "RP", "OperToDoubleAr" "rayExample1", "Arg1", "Example", "Demonstrates COper::ToDoub" "leArray() with a single array", "Argument 1\000", "\0", 1) extern "C" __declspec( dllexport ) LPXLOPER OperToDoubleArrayExample1(const COper* lpopArg1) { CXlOper xloResult; //}}XLP_SRC BOOL bOk = TRUE; USHORT usRows, usCols, i; double *pdArray = 0, dTotal = 0.0; bOk = lpopArg1->IsArray(); if ( bOk ) { // Allocate enough memory, in one contiguous block, // to hold the results. lpopArg1->GetDims(usRows, usCols); pdArray = new double[usRows * usCols]; // Copy the OPER's contents into an array of double's // Columns are kept together (bByRows=FALSE) and // non-numeric cell values are treated as zeroes // (bStrict=FALSE). lpopArg1->ToDoubleArray(pdArray, usRows, usCols, FALSE, FALSE); // Do something useful (!) with the array for ( i = 0; i < (usRows * usCols); i++ ) dTotal += pdArray[i]; // Clean up delete[] pdArray; } if ( bOk ) xloResult = dTotal; else xloResult = xlerrNA; return xloResult.Ret(); }
The second example demonstrates how to copy numeric values from an OPER to an array of pointers to double. The bByRows flag is set to FALSE, so columns are grouped together into the output array. The bStrict flag is set to TRUE, so non-numeric values are treated are not tolerated.
//{{XLP_SRC(OperToDoubleArrayExample2) // NOTE - the FunctionWizard will add and remove mapping code here. // DO NOT EDIT what you see in these blocks of generated code! IMPLEMENT_XLLFN2(OperToDoubleArrayExample2, "RP", "OperToDoubleAr" "rayExample2", "Arg1", "Example", "Demonstrates COper::ToDoub" "leArray() with an array of pointers", "Argument 1\000", "\0" , 1) extern "C" __declspec( dllexport ) LPXLOPER OperToDoubleArrayExample2(const COper* lpopArg1) { CXlOper xloResult; //}}XLP_SRC BOOL bOk = TRUE; USHORT usRows, usCols, i, j; double **ppdArray = 0, dTotal = 0.0; bOk = lpopArg1->IsArray(); if ( bOk ) { // Allocate enough memory to hold the results, // with each column in a block lpopArg1->GetDims(usRows, usCols); ppdArray = new double*[usCols]; for ( j = 0; j < usCols; j++ ) ppdArray[j] = new double[usRows]; // Copy the OPER's contents into the arrays of // double's. Columns are kept together // (bByRows=FALSE) and non-numeric cell values // are not tolerated (bStrict=TRUE). bOk = lpopArg1->ToDoubleArray(ppdArray, usRows, usCols, FALSE, TRUE); } if ( bOk ) { // Do something useful (!) with the array for ( i = 0; i < usRows; i++ ) for ( j = 0; j < usCols; j++ ) dTotal += ppdArray[j][i]; // Clean up for ( j = 0; j < usCols; j++ ) delete[] ppdArray[j]; delete[] ppdArray; } if ( bOk ) xloResult = dTotal; else xloResult = xlerrNA; return xloResult.Ret(); }