Skip to content

Commit

Permalink
[xlsb] fix max ccolspan (#1030)
Browse files Browse the repository at this point in the history
* [xlsb] fix max ccolspan

* [misc] fix sign-compare warning

* [xlsb] fix reading long spans. This is not entirely correct. See the comment.

* [xlsb] fix text rotation output
  • Loading branch information
JanMarvin authored May 26, 2024
1 parent bf71bbc commit 6adae4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/helper_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void wide_to_long(
R_xlen_t idx = 0;

bool has_dims = false;
if (dims.size() == n * m) has_dims = true;
if (dims.size() == (size_t)(n * m)) has_dims = true;

for (auto i = 0; i < m; ++i) {
Rcpp::checkUserInterrupt();
Expand Down
33 changes: 25 additions & 8 deletions src/xlsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ int styles_bin(std::string filePath, std::string outPath, bool debug) {
if (fields->fJustLast)
out << " justifyLastLine=\"" << fields->fJustLast <<"\"";
if (trot)
out << " textRotation=\"" << trot <<"\"";
out << " textRotation=\"" << (uint16_t)trot <<"\"";

out << "/>";
out << "</xf>" << std::endl;
Expand Down Expand Up @@ -1205,7 +1205,7 @@ int externalreferences_bin(std::string filePath, std::string outPath, bool debug
uint32_t col = UncheckedCol(bin, swapit);
value = readbin(value, bin, swapit);
out << "<cell r=\"" << int_to_col(col + 1) << row + 1 << "\" t=\"b\">" << std::endl;
out << "<v>" << value << "</v>" << std::endl;
out << "<v>" << (uint16_t)value << "</v>" << std::endl;
out << "</cell>" << std::endl;

break;
Expand Down Expand Up @@ -2546,15 +2546,32 @@ int worksheet_bin(std::string filePath, bool chartsheet, std::string outPath, bo
// fReserved = 7

ccolspan = readbin(ccolspan, bin, swapit);
if (ccolspan >= 16) Rcpp::stop("ccolspan to large");
if (ccolspan > 16) Rcpp::stop("ccolspan to large");
std::string spans;

std::vector<int32_t> spans_int;
if (ccolspan) {
// rgBrtColspan
// not sure if these are alway around. maybe ccolspan is a counter
colMic = readbin(colMic, bin, swapit);
colLast = readbin(colLast, bin, swapit);
// has number of rgBrtColspan elements. For now take max and min value.
// there are other types of spans (1:1 1024:16384), but unsure how this is
// supposed to be handled. It's possible to have elements like spans =
// "1:1 16384:16384" which we will read as spans = "1:16384"
for (uint8_t clpn = 0; clpn < ccolspan; ++clpn) {
colMic = readbin(colMic, bin, swapit);
colLast = readbin(colLast, bin, swapit);

if (debug) {
spans = std::to_string(colMic + 1) + ":" + std::to_string(colLast + 1);
Rcpp::Rcout << (int32_t)clpn << ": " << spans << std::endl;
}

spans_int.push_back(colMic);
spans_int.push_back(colLast);
}

int32_t min_span = *std::min_element(spans_int.begin(), spans_int.end());
int32_t max_span = *std::max_element(spans_int.begin(), spans_int.end());

spans = std::to_string(colMic + 1) + ":" + std::to_string(colLast + 1);
spans = std::to_string(min_span + 1) + ":" + std::to_string(max_span + 1);
}

out << "<row r=\""; // << bin.tellg()
Expand Down

0 comments on commit 6adae4f

Please sign in to comment.