mercredi 15 juin 2016

xlw: how to use new typedef to get std::string from Excel cells (and possibly populate a std::map)

Introduction

I am referring to xlw, the Excel C API wrapper by Mark Joshi and others.

Before starting with my example, my trials and my question, a couple of words about how to reproduce these codes: once you've downloaded and installed xlw from its website, you should find a folder named XLL_Project, whose contents is a Visual Studio solution ("Template.sln") if this IDE/platform has been selected during installation; this solution is made of a cppinterface.h, that is, an header file which defines what functions should be present in your final .xll, and a source.cpp, which specifies how said functions should behave.

Code

Let an interface function like the following one is in cppinterface.h and its behavior is in source.cpp:

// ===== cppinterface.h =====

std::string // Asks the user to enter the number of pancakes eaten for breakfast by different people. Analyzes the data and output which person ate the most pancakes
    PancakeGlutton(const MyArray& people, // Range of people
                   const MyArray& pancakeNumber // Range of pancakes eaten by each person
                  );

// ===== source.pp =====

std::string PancakeGlutton(const MyArray& people, const MyArray& pancakeNumber)
{
    if (people.size() != pancakeNumber.size())
        throw("Error: arrays must have the same dimension!");
    std::map< std::string, int > myMap;
    for (unsigned long i = 0; i < people.size(); i++)
    {
        myMap.insert(std::pair< std::string, int >(people[i], pancakeNumber[i]));
    }
    return "x"; // Of course this a strange output just for the sake of this example
}

As you can see, there's an attempt to populate a std::map< std::string, int > using the xlw class MyArray: in the Excel spreadsheet people could be a range of strings, hence C++ code should see something like a std::vector< std::string > and thus it should use it to populate myMap with proper std::pair< std::string, int >.

However, this doesn't build.

Main issue is that MyArray is a typedef defined in mycontainers.h:

// ===== mycontainers.h =====
// Line 68
// ...
   typedef std::vector<double> MyArray;

Therefore, no way it can be translated into a container able to manage strings: just doubles. Even though in his book's chapter related to xlw Joshi says...

[...] The class MyArray is also defined via a typedef in MyContainers.h. The default is to typedef to std::vector. It must have .size(), a constructor taking the size, and operator[] defined.

...it is not sufficient to add

typedef std::vector< std::string > MyStrArray;

neither to mycontainers.h nor to any header file of my project.

Questions

  1. How may I populate that std::map< std::string, int> by reading a range of strings from Excel?
  2. How may I define new containers via typedef according to my desired types/classes in order to manage all possible inputs from Excel?

Aucun commentaire:

Enregistrer un commentaire