I'm creating a class to assist in creating excel files (second question). I have a member function that creates a cell at a specific row and column location and saves it to an array.
ExcelSheet::SetValue(int row, int column, std::string szValue) {
myWorksheet[row][column] = szValue;
}
But I'm overloading that function with the option of specifying the column as a letter/number combination, as in Excel. I want to be able to convert inputs like "A3" to Row 3 Column 1, or AB19 to Row 19 Column 28. I.E.,
ExcelSheet::SetValue(std::string szCell, std::string value) {
// search for the alphabetical part of szCell using regex and
// convert it to a column number
myWorksheet[row][column] = szValue;
}
The Excel column pattern goes like this:
A, B, C, D, E... Z (1-26)
AA, AB, AC, AD, AE... AZ (27-52)
BA, BB, BC, BD, BE... BZ (53-78)
...
ZA, ZB, ZC... ZZ
AAA, AAB, AAC... AAZ
ABA, ABB, ABC... ABZ
...
AZA, AZB, AZC... AZZ
BAA, BAB, BAC... BAZ
...
So I'm thinking some sort of 26-based loop would do the trick, but I'm not even sure where to start when it comes to implementing it.
Update:
My almost-working code: It compiles, but does not get the row correctly for some unclear reason.
#include <vector>
#include <string>
#include <iostream>
#include <regex>
class VectorClass {
std::vector<std::string> vectorString;
public:
void PrintVector(std::string szValue) {
std::string str = "space";
vectorString.push_back(szValue);
vectorString.push_back(str);
std::cout << "Loop:" << std::endl;
for(int i=0; i < vectorString.size(); i++) {
std::cout << vectorString[i] << std::endl;
}
}
int GetColumn(std::string szValue) {
std::regex rgx("^([a-zA-Z]+)");
std::smatch result;
if( regex_search(szValue, result, rgx) ) {
std::string szResult = result[0];
int numletters = szResult.length();
const char * colstr = szResult.c_str();
//char colstr[5];
//int numofletters = 0;
//for(int n=0;n<szResult.length();n++) {
// colstr[n] = szResult.substr(n,1);
// numofletters++;
//}
//char colstr[]="AEF";
int col=0;
for(int i=0; i<numletters; i++) {
col = 26*col + colstr[i] - 'A' + 1;
}
return col;
//return atoi(szResult.c_str());
}
else
return -1;
}
int GetRow(std::string szValue) {
std::regex rgx("^[a-zA-Z]+(\d+)$");
std::smatch result;
if( regex_search(szValue, result, rgx) ) {
std::cout << "test: " << result.size() << std::endl;
for(size_t i=0; i<result.size(); ++i) {
std::cout << result[i] << std::endl;
}
std::string szResult = result[0];
return atoi(szResult.c_str());
}
else
return -1;
}
};
int main () {
VectorClass myclass;
//myclass.PrintVector("ship");
std::cout << "A1 = Column " << myclass.GetColumn("A1") << ", Row " << myclass.GetRow("A1") << std::endl;
std::cout << "B32 = Column " << myclass.GetColumn("B32") << ", Row " << myclass.GetRow("B32") << std::endl;
std::cout << "Z65 = Column " << myclass.GetColumn("Z65") << ", Row " << myclass.GetRow("Z65") << std::endl;
std::cout << "AA12 = Column " << myclass.GetColumn("AA12") << ", Row " << myclass.GetRow("AA12") << std::endl;
std::cout << "AB366 = Column " << myclass.GetColumn("AB366") << ", Row " << myclass.GetRow("AB366") << std::endl;
std::cout << "FAB43 = Column " << myclass.GetColumn("ZAB43") << ", Row " << myclass.GetRow("ZAB43") << std::endl;
std::cout << "ZZZ43456 = Column " << myclass.GetColumn("XDE43456") << ", Row " << myclass.GetRow("XDE43456") << std::endl;
std::cin.get();
return 0;
}