1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
* xlsx-read-pivot.c: MS Excel XLS import for pivot tables (tm)
*
* (C) 2005-2008 Jody Goldberg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
**/
#include <gnumeric-config.h>
#include <gnumeric.h>
#include "ms-biff.h"
#include "biff-types.h"
#include "boot.h"
#include "ms-excel-read.h"
#include "ms-excel-util.h"
#include <workbook.h>
#include <sheet.h>
#include <ranges.h>
#include <gnm-data-cache-source.h>
#include <gnm-sheet-slicer.h>
#include <go-data-cache.h>
#include <go-data-cache-field.h>
#include <go-data-slicer-field.h>
#include <go-data-slicer.h>
#include <goffice/goffice.h>
#include <gsf/gsf-infile.h>
#include <gsf/gsf-input.h>
#include <gsf/gsf-utils.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#undef NO_DEBUG_EXCEL
#ifndef NO_DEBUG_EXCEL
#define d(level, code) do { if (ms_excel_pivot_debug > level) { code } } while (0)
#else
#define d(level, code)
#endif
typedef struct {
GnmXLImporter *imp;
GODataCache *cache;
GArray *indexed, *inlined;
} XLSReadPivot;
static gboolean
check_next (BiffQuery *q, unsigned int len)
{
ms_biff_query_next (q);
if (q->length == len)
return TRUE;
if (len < 10)
g_warning ("%x : expected len %d not %d", q->opcode, len, q->length);
else
g_warning ("%x : expected len %d (0x%x) not %d (0x%x)", q->opcode, len, len, q->length, q->length);
return FALSE;
}
static gboolean
check_next_min (BiffQuery *q, unsigned int len)
{
ms_biff_query_next (q);
if (q->length >= len)
return TRUE;
if (len < 10)
g_warning ("%x : expected >= len %d not %d", q->opcode, len, q->length);
else
g_warning ("%x : expected >= len %d (0x%x) not %d (0x%x)", q->opcode, len, len, q->length, q->length);
return FALSE;
}
/**************************************************************************
* Pivot Cache
*/
static GnmValue *
xls_read_pivot_cache_value (XLSReadPivot *s, BiffQuery *q)
{
guint16 opcode;
if (ms_biff_query_peek_next (q, &opcode)) {
switch (opcode) {
case BIFF_SXNUM: if (check_next (q, 8)) {
double v = GSF_LE_GET_DOUBLE (q->data + 0);
d (1, fprintf (stderr, "%g (num);\n", v););
return go_val_new_float (v);
}
break;
case BIFF_SXBOOL: if (check_next (q, 2)) {
gboolean v = 0 != GSF_LE_GET_GINT16 (q->data + 0);
d (1, fprintf (stderr, "%s (bool);\n", v ? "true" : "false"););
return go_val_new_bool (v);
}
break;
case BIFF_SXERR: if (check_next (q, 2)) {
gint16 v = GSF_LE_GET_GINT16 (q->data + 0);
d (1, fprintf (stderr, "%hx (err);\n", v););
return xls_value_new_err (NULL, v);
}
break;
case BIFF_SXINT: if (check_next (q, 2)) {
gint16 v = GSF_LE_GET_GINT16 (q->data + 0);
d (1, fprintf (stderr, "%hx (short);\n", v););
return go_val_new_float (v);
}
break;
case BIFF_SXSTRING: if (check_next_min (q, 2)) {
char *v = excel_biff_text_2 (s->imp, q, 0);
d (1, fprintf (stderr, "'%s' (string);\n", v););
return go_val_new_str_nocopy (v);
}
break;
case BIFF_SXDTR: if (check_next (q, 8)) { /* date time */
guint16 y = GSF_LE_GET_GUINT16 (q->data + 0);
guint16 m = GSF_LE_GET_GUINT16 (q->data + 2);
guint8 d = GSF_LE_GET_GUINT8 (q->data + 4);
guint8 h = GSF_LE_GET_GUINT8 (q->data + 5);
guint8 mi = GSF_LE_GET_GUINT8 (q->data + 6);
guint8 se = GSF_LE_GET_GUINT8 (q->data + 7);
GDate date;
d (1, fprintf (stderr, "%hu-%hu-%hhuT%hhu:%hhu:%hhu (data);\n",
y, m, d, h, mi, se););
g_date_set_dmy (&date, d, m, y);
if (g_date_valid (&date)) {
unsigned d_serial = go_date_g_to_serial (&date,
workbook_date_conv (s->imp->wb));
double time_frac = h + ((double)mi / 60.) + ((double)se / 3600.);
GnmValue *res = value_new_float (d_serial + time_frac / 24.);
value_set_fmt (res, go_format_default_date_time ());
return res;
}
g_warning ("Invalid date in pivot cache.");
}
break;
case BIFF_SXNIL: if (check_next (q, 0)) {
d (1, fprintf (stderr, "(empty);\n"););
return go_val_new_empty ();
}
default :
d (0, fprintf (stderr, "UNEXPECTED RECORD %hx;\n", opcode););
break;
}
}
d (0, fprintf (stderr, "missing value;\n"););
return NULL;
}
static GOValArray *
xls_read_pivot_cache_values (XLSReadPivot *s, BiffQuery *q, unsigned int n, const char *type)
{
/* TODO : go_val_array_sized_new */
GPtrArray *res = g_ptr_array_sized_new (n);
GnmValue *v;
unsigned int i;
d (1, fprintf (stderr, "/* %u %s items */ ;\n", n, type););
for (i = 0 ; i < n ; i++) {
if (NULL == (v = xls_read_pivot_cache_value (s, q))) {
/* TODO : go_val_array_set_size */
g_ptr_array_set_size (res, i);
return res;
}
/* TODO : go_val_array_add */
g_ptr_array_add (res, v);
}
return res;
}
static void
xls_read_pivot_cache_group (XLSReadPivot *s, BiffQuery *q, GODataCacheField *field)
{
guint16 opcode;
if (ms_biff_query_peek_next (q, &opcode) &&
opcode == BIFF_SXNUMGROUP &&
check_next (q, 2)) {
GOValBucketer bucketer;
GOValArray *bucket_bounds;
GError *valid;
guint16 flags = GSF_LE_GET_GUINT16 (q->data + 0);
#if 0
gboolean const autoMinima = 0 != (flags & 1);
gboolean const autoMaxima = 0 != (flags & 2);
#endif
unsigned const type = (flags >> 2) & 0xf;
go_val_bucketer_init (&bucketer);
bucketer.details.series.step = 1.;
d (0, fprintf (stderr, "group with 0x%hx flag type = %d;\n", flags, type););
switch (type) {
case 1 : bucketer.type = GO_VAL_BUCKET_SECOND; break;
case 2 : bucketer.type = GO_VAL_BUCKET_MINUTE; break;
case 3 : bucketer.type = GO_VAL_BUCKET_HOUR; break;
case 4 : bucketer.type = GO_VAL_BUCKET_DAY_OF_YEAR; break;
case 5 : bucketer.type = GO_VAL_BUCKET_MONTH; break;
case 6 : bucketer.type = GO_VAL_BUCKET_CALENDAR_QUARTER; break;
case 7 : bucketer.type = GO_VAL_BUCKET_YEAR; break;
default:
/* default to linear */;
case 8 : bucketer.type = GO_VAL_BUCKET_SERIES_LINEAR; break;
}
bucket_bounds = xls_read_pivot_cache_values (s, q, 3, "group bounds");
if (bucketer.type == GO_VAL_BUCKET_SERIES_LINEAR) {
bucketer.details.series.minimum = go_val_as_float (go_val_array_index (bucket_bounds, 0));
bucketer.details.series.maximum = go_val_as_float (go_val_array_index (bucket_bounds, 1));
bucketer.details.series.step = go_val_as_float (go_val_array_index (bucket_bounds, 2));
} else {
bucketer.details.dates.minimum = go_val_as_float (go_val_array_index (bucket_bounds, 0));
bucketer.details.dates.maximum = go_val_as_float (go_val_array_index (bucket_bounds, 1));
}
/* TODO : min/max/step : TODO */
if (NULL == (valid = go_val_bucketer_validate (&bucketer)))
g_object_set (G_OBJECT (field), "bucketer", &bucketer, NULL);
else {
g_warning ("Skipping invalid pivot field group for field '%s' because : %s",
go_data_cache_field_get_name (field)->str,
valid->message);
g_error_free (valid);
}
go_val_array_free (bucket_bounds);
}
}
static void
xls_read_pivot_cache_field (XLSReadPivot *s, BiffQuery *q, unsigned int field_num)
{
GODataCacheField *field;
guint16 opcode, flags = GSF_LE_GET_GUINT16 (q->data + 0);
unsigned const index_type = flags & 3; /* XL : allAtoms.someUnhashed = 1 or 2 */
#if 0
4 - must be 0
gboolean const has_child = flags & 8;
8 - fHasChild (1 bit): A bit that specifies whether ifdbParent specifies a reference to a parent grouping cache field. For more information, see Grouping. If the fCalculatedField field is equal to 1, then this field MUST be equal to 0.
10 - fRangeGroup (1 bit): A bit that specifies whether this cache field is grouped using numeric
grouping or date grouping, as specified by Grouping. If this field is equal to 1, then this record
MUST be followed by a sequence of SXString records, as specified by the GRPSXOPER rule. The
quantity of SXString records is specified by csxoper. Also, if this field is equal to 1, then this
record MUST be followed by a sequence of records that conforms to the SXRANGE rule that
specifies the grouping properties for the ranges of values.
20 - fNumField (1 bit): A bit that specifies whether the cache items in this
cache field contain at least one numeric cache item, as specified by
SXNum. If fDateInField is equal to 1, this field MUST be
equal to 0.
40 - fTextEtcField (1 bit): A bit that specifies whether the cache items contain text data. If
fNumField is 1, this field MUST be ignored.
80 - fnumMinMaxValid (1 bit): A bit that specifies whether a valid minimum or maximum value can
be computed for the cache field. MUST be equal to 1 if fDateInField or fNumField is equal to 1.
100 - fShortIitms (1 bit): A bit that specifies whether the there are more than
255 cache items in this cache field. If catm is greater than 255, this
value MUST be equal to 1; otherwise it MUST be 0.
200 - fNonDates (1 bit): A bit that specifies whether the cache items in this cache field contain values that are not time or date values. If this cache field is a grouping cache field, as specified by Grouping, then this field MUST be ignored. Otherwise, if fDateInField is equal to 1, then this field MUST be 0.
400 - fDateInField (1 bit): A bit that specifies whether the cache items in this cache field contain at least one time or date cache item, as specified by SXDtr. If fNonDates is equal to 1, then this field MUST be equal to 0.
800 - fServerBased (1 bit): A bit that specifies whether this cache field is a server-based page field
when the corresponding pivot field is on the page axis of the PivotTable view , as specified in
source data.
This value applies to an ODBC PivotCache only. MUST NOT be equal to 1 if
fCantGetUniqueItems is equal to 1. If fCantGetUniqueItems is equal to 1, then the ODBC
connection cannot provide a list of unique items for the cache field.
MUST be 0 for a cache field in a non-ODBC PivotCache.
1000 - fCantGetUniqueItems (1 bit): A bit that specifies whether a list of unique values for the cache
field was not available while refreshing the source data. This field applies only to a PivotCache that
uses ODBC source data and is intended to be used in conjunction with optimization features. For
example, the application can optimize memory usage when populating PivotCache records if it has
a list of unique values for a cache field before all the records are retrieved from the ODBC
connection. Or, the application can determine the appropriate setting of fServerBased based on
this value.
MUST be 0 for fields in a non-ODBC PivotCache.
2000 - fCalculatedField (1 bit): A bit that specifies whether this field is a calculated field. The formula
of the calculated field is stored in a directly following SXFormula record. If fHasParent is equal to
1, this field MUST be equal to 0.
#endif
int group_child = GSF_LE_GET_GUINT16 (q->data + 2); /* what use is this ? */
int group_parent = GSF_LE_GET_GUINT16 (q->data + 4);
guint16 count = GSF_LE_GET_GUINT16 (q->data + 6);
guint16 grouped_items = GSF_LE_GET_GUINT16 (q->data + 8);
guint16 base_items = GSF_LE_GET_GUINT16 (q->data + 10);
guint16 std_items = GSF_LE_GET_GUINT16 (q->data + 12);
GOString *name = go_string_new_nocopy (excel_biff_text_2 (s->imp, q, 14));
d(0, fprintf (stderr, "FIELD [%d] '%s' type %d, has %d %d %d %d items, and flags = 0x%hx, parent = %d, child = %d;\n",
field_num, name ? name->str : "<UNDEFINED>", index_type, count, grouped_items, base_items, std_items, flags,
group_parent, group_child););
field = g_object_new (GO_DATA_CACHE_FIELD_TYPE, "name", name, NULL);
go_string_unref (name);
/* KLUDGE TODO TODO TODO */
if (flags == 0x11)
g_object_set (field, "group-parent", group_parent, NULL);
d (4, gsf_mem_dump (q->data + 2, MIN (q->length, 0xc)););
/* SQL type of field (ignored) */
if (ms_biff_query_peek_next (q, &opcode) && opcode == BIFF_SXFDBTYPE && check_next (q, 2)) ;
switch (index_type) {
case 1 :
if (grouped_items > 0) {
go_data_cache_field_set_vals (field, TRUE,
xls_read_pivot_cache_values (s, q, grouped_items, "grouped"));
xls_read_pivot_cache_group (s, q, field);
}
if (std_items > 0) {
go_data_cache_field_set_vals (field, FALSE,
xls_read_pivot_cache_values (s, q, std_items, "shared"));
g_array_append_val (s->indexed, field_num);
}
break;
case 2 : /* items follow the last normal field and are preceded by an index */
g_array_append_val (s->inlined, field_num);
break;
default :
g_warning ("unknown index type %d for field '%s' (#%u)",
index_type, name ? name->str : "<UNDEFINED>", field_num);
}
go_data_cache_add_field (s->cache, field);
}
static gboolean
xls_read_pivot_cache (XLSReadPivot *s, BiffQuery *q)
{
unsigned int i, record_count;
unsigned int num_records, num_fields;
guint16 stream_id, opcode;
char *refreshedBy;
s->cache = g_object_new (GO_DATA_CACHE_TYPE, NULL);
/* SXDB */
if (!ms_biff_query_peek_next (q, &opcode) || opcode != BIFF_SXDB || !check_next_min (q, 20))
return FALSE;
num_records = GSF_LE_GET_GUINT32 (q->data + 0);
stream_id = GSF_LE_GET_GUINT16 (q->data + 4);
num_fields = GSF_LE_GET_GUINT16 (q->data + 12); /* base + grouped + calced */
refreshedBy = excel_biff_text_2 (s->imp, q, 18);
g_object_set (s->cache, "refreshed-by", refreshedBy, NULL);
s->indexed = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), num_fields);
s->inlined = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), num_fields);
d (1, {
guint16 flags = GSF_LE_GET_GUINT16 (q->data + 6);
guint16 rec_per_block = GSF_LE_GET_GUINT16 (q->data + 8); /* seems constant */
guint16 base_fields = GSF_LE_GET_GUINT16 (q->data + 10); /* base */
/* guint16 zero */
guint16 type = GSF_LE_GET_GUINT16 (q->data + 16);
fprintf (stderr, "num_rec = %u;\nstream_id = %hu;\n"
"rec per block = %hu;\nbase fields = %hu;\ntotal fields = %hu;\n"
"last modified by = '%s';type = 0x%x, flags = 0x%x;\n",
num_records, stream_id, rec_per_block, base_fields,
num_fields, refreshedBy, type, flags);
});
g_free (refreshedBy);
/* SXDBEX (optional SXDB extension) */
if (ms_biff_query_peek_next (q, &opcode) && opcode == BIFF_SXDBEX && check_next (q, 12)) {
GOVal *refreshedDate = value_new_float (GSF_LE_GET_DOUBLE (q->data + 0));
value_set_fmt (refreshedDate, go_format_default_date_time ());
g_object_set (s->cache, "refreshed-on", refreshedDate, NULL);
d (0, {
guint32 num_fmla = GSF_LE_GET_GUINT32 (q->data + 8);
fprintf (stderr, "num_fmla %u : last refresh %s\n",
num_fmla, value_peek_string (refreshedDate));
});
go_val_free (refreshedDate);
}
for (i = 0 ; i < num_fields ; i++)
if (ms_biff_query_peek_next (q, &opcode) && opcode == BIFF_SXFDB && check_next_min (q, 12))
xls_read_pivot_cache_field (s, q, i);
else {
fprintf (stderr, "expected FDB not %hx\n", opcode);
return FALSE;
}
go_data_cache_import_start (s->cache, num_records);
record_count = 0;
while (ms_biff_query_peek_next (q, &opcode) && opcode != BIFF_EOF) {
switch (opcode) {
case BIFF_SXDDB: if (check_next (q, s->indexed->len)) {
for (i = 0 ; i < s->indexed->len; i++) {
go_data_cache_set_index (s->cache,
g_array_index (s->indexed, unsigned int, i), record_count,
GSF_LE_GET_GINT8 (q->data + i));
d (1, fprintf (stderr, "%hhu ", GSF_LE_GET_GINT8 (q->data + i)););
}
d (1, fprintf (stderr, "\n"); );
if (s->inlined->len > 0) {
GOValArray *vals = xls_read_pivot_cache_values (s, q, s->inlined->len, "inline");
for (i = 0 ; i < s->inlined->len; i++)
go_data_cache_set_val (s->cache,
g_array_index (s->inlined, unsigned int, i), record_count,
go_val_array_index_steal (vals, i));
go_val_array_free (vals);
}
record_count++;
}
break;
default:
ms_biff_query_next (q);
ms_biff_query_dump (q);
}
}
go_data_cache_import_done (s->cache, record_count);
record_count = 0;
g_array_free (s->inlined, TRUE);
g_array_free (s->indexed, TRUE);
return TRUE;
}
static void
xls_read_pivot_cache_by_id (XLSReadPivot *s, GsfInfile *container, guint16 n)
{
BiffQuery *q;
GsfInput *cache_stream, *dir;
char name[5];
if (NULL == container)
return; /* pre-Excel 95 without ole */
dir = gsf_infile_child_by_name (container, "_SX_DB_CUR"); /* Excel 97 */
if (NULL == dir)
dir = gsf_infile_child_by_name (container, "_SX_DB"); /* Excel 95 */
if (NULL == dir)
return;
snprintf (name, sizeof (name), "%04hX", n);
cache_stream = gsf_infile_child_by_name (GSF_INFILE (dir), name);
if (NULL != cache_stream) {
q = ms_biff_query_new (cache_stream);
d (0, fprintf (stderr, "{ /* PIVOT CACHE [%s] */\n", name););
if (!xls_read_pivot_cache (s, q)) {
g_object_unref (s->cache);
s->cache = NULL;
} else
d (2, go_data_cache_dump (s->cache, NULL, NULL););
d (0, fprintf (stderr, "}; /* PIVOT CACHE [%s] */\n", name););
ms_biff_query_destroy (q);
g_object_unref (cache_stream);
}
g_object_unref (dir);
}
/**************************************************************************
* Pivot Source
*/
void
xls_read_SXStreamID (GnmXLImporter *imp, BiffQuery *q, GsfInfile *container)
{
XLSReadPivot s;
guint16 opcode, cache_id;
GODataCacheSource *cache_src = NULL;
XL_CHECK_CONDITION (q->length >= 2);
cache_id = GSF_LE_GET_GUINT16 (q->data);
s.imp = imp;
s.cache = NULL;
if (cache_id < imp->pivot.cache_by_id->len)
s.cache = g_ptr_array_index (imp->pivot.cache_by_id, cache_id);
else
g_ptr_array_set_size (imp->pivot.cache_by_id, cache_id+1);
if (NULL == s.cache) {
xls_read_pivot_cache_by_id (&s, container, cache_id);
g_ptr_array_index (imp->pivot.cache_by_id, cache_id) = s.cache;
}
if (ms_biff_query_peek_next (q, &opcode) &&
opcode == BIFF_SXVS &&
check_next (q, 2)) {
guint16 source_type = GSF_LE_GET_GUINT16 (q->data);
ms_biff_query_peek_next (q, &opcode);
switch (source_type) {
case 1 : /* Sheet */
switch (opcode) {
case BIFF_DCONREF: if (check_next_min (q, 8)) {
GnmRange r;
guint8 *source_name = excel_biff_text_2 (imp, q, 6);
xls_read_range8 (&r, q->data);
if (source_name) {
if (source_name[0] == 2) {
Sheet *sheet = workbook_sheet_by_name (imp->wb, source_name+1);
if (NULL == sheet) {
sheet = sheet_new (imp->wb, source_name+1, XLS_MaxCol,
(imp->ver >= MS_BIFF_V8) ?
XLS_MaxRow_V8 : XLS_MaxRow_V7);
workbook_sheet_attach (imp->wb, sheet);
}
cache_src = gnm_data_cache_source_new (sheet, &r, NULL);
} else
; /* TODO : the rest of dcon-file */
}
#if 0
g_printerr ("Sheet : ref '%s' ! %s\n", source_name ? (char*)source_name : "<<unknown>",
range_as_string (&r));
#endif
g_free (source_name);
}
break;
case BIFF_DCONNAME: if (check_next_min (q, 2)) {
char *name = excel_biff_text_2 (imp, q, 0);
g_object_set_data_full (G_OBJECT (s.cache),
"src-name", name, (GDestroyNotify) g_free);
g_print ("Sheet : name '%s'\n", name);
}
break;
case BIFF_DCONBIN: g_print ("Sheet : binname\n");
break;
default:
g_print ("missing sheet type : %hx\n", source_type);
break;
}
break;
case 2 : /* external */
break;
case 4 : /* consolidation */
break;
case 0x10 : /* scenarios : no source data */
break;
default :
g_print ("unknown source type : %hx\n", source_type);
break;
}
}
if (NULL != cache_src) {
if (NULL != s.cache)
go_data_cache_set_source (s.cache, cache_src);
else
g_object_unref (cache_src);
}
}
/*********************************************************/
void
xls_read_SXIVD (BiffQuery *q, ExcelReadSheet *esheet)
{
GnmXLImporter *imp = esheet->container.importer;
GODataSlicerFieldType t = imp->pivot.ivd_index ? GDS_FIELD_TYPE_COL : GDS_FIELD_TYPE_ROW;
unsigned int i;
g_return_if_fail (imp->pivot.ivd_index < 2);
imp->pivot.ivd_index++;
d(3, ms_biff_query_dump (q););
for (i = 0 ; 2*i < q->length ; i++) {
guint16 const indx = GSF_LE_GET_GUINT16 (q->data + i*2);
/* ignore special orientation index. */
if (0xfffe != indx) {
go_data_slicer_field_set_field_type_pos (
go_data_slicer_get_field ((GODataSlicer *)imp->pivot.slicer, indx),
t, i);
}
}
}
static void
xls_read_SXVI (BiffQuery *q, ExcelReadSheet *esheet, unsigned int i)
{
guint16 const type = GSF_LE_GET_GUINT16 (q->data + 0);
guint16 const flags = GSF_LE_GET_GUINT16 (q->data + 2);
guint16 const cache_index = GSF_LE_GET_GUINT16 (q->data + 4);
// guint16 const name_len = GSF_LE_GET_GUINT16 (q->data + 6);
GnmXLImporter *imp = esheet->container.importer;
GODataCacheField *dcf = go_data_slicer_field_get_cache_field (imp->pivot.slicer_field);
char const *type_str = "unknown";
XL_CHECK_CONDITION (NULL != dcf);
d(0, { switch (type) {
case 0xFE: type_str = "Page"; break;
case 0xFF: type_str = "Null"; break;
case 0x00: type_str = "Data"; break;
case 0x01: type_str = "Default"; break;
case 0x02: type_str = "SUM"; break;
case 0x03: type_str = "COUNTA"; break;
case 0x04: type_str = "COUNT"; break;
case 0x05: type_str = "AVERAGE"; break;
case 0x06: type_str = "MAX"; break;
case 0x07: type_str = "MIN"; break;
case 0x08: type_str = "PRODUCT"; break;
case 0x09: type_str = "STDEV"; break;
case 0x0A: type_str = "STDEVP"; break;
case 0x0B: type_str = "VAR"; break;
case 0x0C: type_str = "VARP"; break;
case 0x0D: type_str = "Grand total"; break;
default : type_str = "UNKNOWN"; break;
}
g_print ("[%u] %s %s %s %s %s = %hu\n", i, type_str,
(flags & 1) ? "hidden " : "",
(flags & 2) ? "detailHid " : "",
(flags & 4) ? "calc " : "",
(flags & 8) ? "missing " : "", cache_index);
});
if (type == 0x00 && (flags & 1))
{
XL_CHECK_CONDITION (cache_index != 0xffff);
g_print ("hide : ");
go_data_cache_dump_value (go_data_cache_field_get_val (dcf, cache_index));
g_print ("\n");
}
}
void
xls_read_SXVD (BiffQuery *q, ExcelReadSheet *esheet)
{
static GODataSlicerFieldType const axis_bits[] = {
GDS_FIELD_TYPE_ROW, GDS_FIELD_TYPE_COL,
GDS_FIELD_TYPE_PAGE, GDS_FIELD_TYPE_DATA
};
static GOAggregateBy const aggregation_bits[] = {
GO_AGGREGATE_AUTO, GO_AGGREGATE_BY_SUM, GO_AGGREGATE_BY_COUNTA,
GO_AGGREGATE_BY_AVERAGE, GO_AGGREGATE_BY_MAX, GO_AGGREGATE_BY_MIN,
GO_AGGREGATE_BY_PRODUCT, GO_AGGREGATE_BY_COUNT, GO_AGGREGATE_BY_STDDEV,
GO_AGGREGATE_BY_STDDEVP, GO_AGGREGATE_BY_VAR, GO_AGGREGATE_BY_VARP
};
GnmXLImporter *imp = esheet->container.importer;
unsigned int i, len_name, axis, aggregations, num_items, aggregate_by = 0;
guint16 opcode;
XL_CHECK_CONDITION (q->length >= 10);
axis = GSF_LE_GET_GUINT16 (q->data + 0);
/* num_subtotal = GSF_LE_GET_GUINT16 (q->data + 2); */
aggregations = GSF_LE_GET_GUINT16 (q->data + 4);
num_items = GSF_LE_GET_GUINT16 (q->data + 6);
len_name = GSF_LE_GET_GUINT16 (q->data + 8);
imp->pivot.slicer_field = g_object_new (GO_DATA_SLICER_FIELD_TYPE,
"data-cache-field-index", imp->pivot.field_count++,
NULL);
go_data_slicer_add_field (GO_DATA_SLICER (imp->pivot.slicer),
imp->pivot.slicer_field);
for (i = 0 ; i < G_N_ELEMENTS (axis_bits) ; i++)
if ((axis & (1 << i)))
go_data_slicer_field_set_field_type_pos (imp->pivot.slicer_field,
axis_bits[i], G_MAXINT);
for (i = 0 ; i < G_N_ELEMENTS (aggregation_bits) ; i++)
if ((aggregations & (1 << i)))
aggregate_by |= (1 << aggregation_bits[i]);
g_object_set (G_OBJECT (imp->pivot.slicer_field),
"aggregations", aggregate_by, NULL);
for (i = 0 ; i < num_items ; i++)
if (ms_biff_query_peek_next (q, &opcode) && BIFF_SXVI == opcode &&
check_next_min (q, 8)) {
xls_read_SXVI (q, esheet, i);
}
if (ms_biff_query_peek_next (q, &opcode) && BIFF_SXVDEX == opcode &&
check_next_min (q, 12)) {
/* Ignore */
}
}
void
xls_read_SXVIEW (BiffQuery *q, ExcelReadSheet *esheet)
{
GnmXLImporter *imp = esheet->container.importer;
unsigned int first_header_row, cache_id, name_len, data_field_name_len,
data_field_axis, data_field_pos, num_fields,
num_row_fields, num_column_fields, num_page_fields,
num_data_fields, num_data_rows, data_columns, flags, autoformat;
guint32 len;
GOString *name = NULL, *data_field_name = NULL;
GnmCellPos first_data;
GnmRange range;
GODataCache *cache = NULL;
XL_CHECK_CONDITION (q->length >= 44);
xls_read_range16 (&range, q->data);
first_header_row = GSF_LE_GET_GINT16 (q->data + 8);
first_data.row = GSF_LE_GET_GINT16 (q->data + 10);
first_data.col = GSF_LE_GET_GINT16 (q->data + 12);
cache_id = GSF_LE_GET_GINT16 (q->data + 14) + 1;
data_field_axis = GSF_LE_GET_GUINT16 (q->data + 18); /* Default axis for a data field */
data_field_pos = GSF_LE_GET_GUINT16 (q->data + 20); /* Default position for a data field */
num_fields = GSF_LE_GET_GUINT16 (q->data + 22); /* Number of fields */
num_row_fields = GSF_LE_GET_GUINT16 (q->data + 24); /* Number of row fields */
num_column_fields = GSF_LE_GET_GUINT16 (q->data + 26); /* Number of column fields */
num_page_fields = GSF_LE_GET_GUINT16 (q->data + 28); /* Number of page fields */
num_data_fields = GSF_LE_GET_GUINT16 (q->data + 30); /* Number of data fields */
num_data_rows = GSF_LE_GET_GUINT16 (q->data + 32); /* Number of data rows */
data_columns = GSF_LE_GET_GUINT16 (q->data + 34); /* Number of data columns */
flags = GSF_LE_GET_GUINT16 (q->data + 36);
autoformat = GSF_LE_GET_GUINT16 (q->data + 38); /* Index to the PivotTable autoformat */
name_len = GSF_LE_GET_GINT16 (q->data + 40);
data_field_name_len = GSF_LE_GET_GINT16 (q->data + 42);
if (cache_id < imp->pivot.cache_by_id->len)
cache = g_ptr_array_index (imp->pivot.cache_by_id, cache_id);
name = go_string_new_nocopy (
excel_get_text (imp, q->data + 44, name_len,
&len, q->length - 44));
data_field_name = go_string_new_nocopy (
excel_get_text (imp, q->data + 44 + len, data_field_name_len,
&len, q->length - 44 - len));
d(0, fprintf (stderr, "Slicer in : %s named '%s';\n",
range_as_string (&range), name ? name->str : "<UNDEFINED>"););
if (NULL != imp->pivot.slicer)
g_object_unref (imp->pivot.slicer);
imp->pivot.slicer = g_object_new (GNM_SHEET_SLICER_TYPE,
"name", name,
"cache", cache,
"range", &range,
"sheet", esheet->sheet,
"first-header-row", MAX (first_header_row - range.start.row, 0),
"first-data-row", MAX (first_data.row - range.start.row, 0),
"first-data-col", MAX (first_data.col - range.start.col, 0),
#if 0
"row-page-count",
"col-page-count",
#endif
NULL);
go_string_unref (name);
go_string_unref (data_field_name);
imp->pivot.field_count = 0;
imp->pivot.ivd_index = 0;
/* Usually followed by num_fields SXVD */
}
|