CellMatrix to map<string, string>

For discussion of the xlw project for producing xlls.

CellMatrix to map<string, string>

Postby krad23 » Thu Oct 09, 2014 1:46 pm

I'm trying to get the input from Excel into a map<string, string> and then output it back to Excel. So I'm using CellMatrix, as it accepts anything. While it works if I have only characters in my cells, if one has a number it returns "non string cell asked to be a string". How can I fix it?

Code: Select all
CellMatrix Map(const CellMatrix &x) {

   std::map<std::string, std::string> in;

   MatrixToMap(x,in);

   CellMatrix out(in.size(),2);
   MapToMatrix(in,out);

   return out;
}

void MatrixToMap(const CellMatrix &source, std::map<std::string, std::string> &dest) {
   
    if (source.ColumnsInStructure() != 2) {
      THROW_XLW("Expected a two-column array of strings");
    }

   for (size_t i = 0; i < source.RowsInStructure(); i++) {
        dest[source(i, 0)] = source(i, 1);
    }
}

void MapToMatrix(const std::map<std::string, std::string> &source, CellMatrix &dest) {
   size_t i = 0;

   for (std::map<std::string, std::string>::const_iterator it = source.begin(); it != source.end(); it++) {
      dest(i, 0) = it->first;
      dest(i, 1) = it->second;
      i++;
   }
}


I've tried different stuff with
Code: Select all
dest[source(i, 0).StringValue()] = source(i, 1).StringValue()
or
Code: Select all
dest(i, 0) = it->first.c_str()
but with the same result. I just started using XLW today.
krad23
 
Posts: 6
Joined: Thu Oct 09, 2014 1:33 pm

Re: CellMatrix to map<string, string>

Postby mj » Fri Oct 10, 2014 12:37 am

well you could test the type of each cell. If it has a number then use a stringstream to convert it to a string.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am


Return to xlw

Who is online

Users browsing this forum: No registered users and 2 guests