Update of /cvsroot/objecthandler/ObjectHandler/ohxl
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22756/ohxl
Modified Files:
rangereference.cpp rangereference.hpp
Log Message:
enhanced support for processing of range references
Index: rangereference.cpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/ohxl/rangereference.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** rangereference.cpp 20 Sep 2006 20:17:07 -0000 1.1
--- rangereference.cpp 26 Oct 2006 11:29:50 -0000 1.2
***************
*** 25,40 ****
RangeReference::RangeReference(const std::string &address)
! : originalAddress_(address) {
! std::string addressUpper = uppercase(address);
! static boost::regex r(
! "=?'?.*\\[([\\w\\s]+)(?:\\.XLS)?\\]([\\w\\s]+)'?!R(\\d*)C(\\d*)(?::R(\\d*)C(\\d*))?");
! boost::smatch m;
! if (!boost::regex_match(addressUpper, m, r)) {
std::ostringstream err;
err << "the string '" << address << "' is not a valid range reference";
throw Exception(err.str().c_str());
}
bookName_ = m[1];
--- 25,46 ----
RangeReference::RangeReference(const std::string &address)
! : addressOriginal_(address) {
! addressUpper_ = uppercase(address);
! if (!init1() && !init2()) {
std::ostringstream err;
err << "the string '" << address << "' is not a valid range reference";
throw Exception(err.str().c_str());
}
+ }
+
+ bool RangeReference::init1() {
+ // try to handle case where book name != sheet name
+ static boost::regex r(
+ "=?'?.*\\[([\\w\\s]+)(?:\\.XLS)?\\]([\\w\\s]+)'?!R(\\d*)C(\\d*)(?::R(\\d*)C(\\d*))?");
+ boost::smatch m;
+ if (!boost::regex_match(addressUpper_, m, r))
+ return false;
bookName_ = m[1];
***************
*** 51,54 ****
--- 57,85 ----
colEndNum_ = -1;
}
+ return true;
+ }
+
+ bool RangeReference::init2() {
+ // try to handle case of book containing single sheet w/same name
+ static boost::regex r(
+ "=?'?([\\w\\s]+)(?:\\.XLS)'?!R(\\d*)C(\\d*)(?::R(\\d*)C(\\d*))?");
+ boost::smatch m;
+ if (!boost::regex_match(addressUpper_, m, r))
+ return false;
+
+ bookName_ = m[1];
+ sheetName_ = m[1];
+ rowStartNum_ = boost::lexical_cast<int>(m[2]);
+ colStartNum_ = boost::lexical_cast<int>(m[3]);
+ if (m[4].matched) {
+ multicell_ = true;
+ rowEndNum_ = boost::lexical_cast<int>(m[4]);
+ colEndNum_ = boost::lexical_cast<int>(m[5]);
+ } else {
+ multicell_ = false;
+ rowEndNum_ = -1;
+ colEndNum_ = -1;
+ }
+ return true;
}
***************
*** 82,86 ****
bool RangeReference::operator==(const RangeReference &r) const {
! if (this->originalAddress_ == r.originalAddress_)
return true;
return this->bookName_ == r.bookName_
--- 113,117 ----
bool RangeReference::operator==(const RangeReference &r) const {
! if (this->addressOriginal_ == r.addressOriginal_)
return true;
return this->bookName_ == r.bookName_
***************
*** 94,98 ****
std::ostream &operator<<(std::ostream &out, const RangeReference &r) {
! out << "originalAddress = " << r.originalAddress_ << std::endl;
out << "bookName = " << r.bookName_ << std::endl;
out << "sheetName = " << r.sheetName_ << std::endl;
--- 125,129 ----
std::ostream &operator<<(std::ostream &out, const RangeReference &r) {
! out << "originalAddress = " << r.addressOriginal_ << std::endl;
out << "bookName = " << r.bookName_ << std::endl;
out << "sheetName = " << r.sheetName_ << std::endl;
Index: rangereference.hpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/ohxl/rangereference.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** rangereference.hpp 20 Sep 2006 20:17:07 -0000 1.1
--- rangereference.hpp 26 Oct 2006 11:29:50 -0000 1.2
***************
*** 32,58 ****
The following formats are supported:
! normal:
"[Book1.xls]Sheet1!R1C1"
! some excel functions prepend an '=' sign:
"=[Book1.xls]Sheet1!R1C1"
! if book or sheet name contains spaces then Excel encloses the text
in single quotes:
"'[Bo ok1.xls]Sheet1'!R1C1"
! for a new book that hasn't yet been saved, excel omits the .xls suffix:
"[Book1]Sheet1!R1C1"
! sometimes the string includes the filesystem path:
"='C:\\path\\to\\[Book1.xls]Sheet1'!R1C1"
In all cases, the cell reference, represented above as R1C1, may also
be given as R1C1:R9C9 i.e. a range consisting of multiple cells, in
which case the RangeReference constructor sets multicell_ to true.
-
- There is one further case which is not supported. If a workbook contains
- a single sheet which has the same name as the book, Excel gives the string
- reference in the following format, which the RangeReference constructor
- doesn't recognize:
- "xxx.xls!R1C1"
-
- Unsupported formats cause an exception to be thrown.
*/
--- 32,55 ----
The following formats are supported:
! Normal:
"[Book1.xls]Sheet1!R1C1"
! Some excel functions prepend an '=' sign:
"=[Book1.xls]Sheet1!R1C1"
! If book or sheet name contains spaces then Excel encloses the text
in single quotes:
"'[Bo ok1.xls]Sheet1'!R1C1"
! For a new book that hasn't yet been saved, excel omits the .xls suffix:
"[Book1]Sheet1!R1C1"
! Sometimes the string includes the filesystem path:
"='C:\\path\\to\\[Book1.xls]Sheet1'!R1C1"
+ If the book contains a single sheet with the same name as the book,
+ then the sheet name is omitted altogether and the book name is not
+ enclosed in []s:
+ "same_name.xls!R1C1"
+ "'same name.xls'!R1C1"
In all cases, the cell reference, represented above as R1C1, may also
be given as R1C1:R9C9 i.e. a range consisting of multiple cells, in
which case the RangeReference constructor sets multicell_ to true.
*/
***************
*** 64,68 ****
friend std::ostream &operator<<(std::ostream&, const RangeReference&);
private:
! std::string originalAddress_;
std::string bookName_;
std::string sheetName_;
--- 61,66 ----
friend std::ostream &operator<<(std::ostream&, const RangeReference&);
private:
! std::string addressOriginal_;
! std::string addressUpper_;
std::string bookName_;
std::string sheetName_;
***************
*** 72,75 ****
--- 70,75 ----
int rowEndNum_;
int colEndNum_;
+ bool init1();
+ bool init2();
};
|