File: xlsx-write-pivot.c

package info (click to toggle)
gnumeric 1.10.8-1squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 90,968 kB
  • ctags: 23,303
  • sloc: ansic: 248,235; xml: 51,894; sh: 10,491; makefile: 2,822; perl: 2,466; yacc: 1,272; python: 205
file content (328 lines) | stat: -rw-r--r-- 11,712 bytes parent folder | download
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
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * xlsx-write-pivot.c : export pivot tables (tm) to MS Office Open xlsx files.
 *
 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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 <go-data-cache-impl.h>
#include <go-data-cache-field.h>
#include <gnm-data-cache-source.h>

/*
 *
 * DO * NOT * COMPILE * DIRECTLY *
 * DO * NOT * COMPILE * DIRECTLY *
 * DO * NOT * COMPILE * DIRECTLY *
 *
 * included via xlsx-write.c
 **/
static void
xlsx_write_pivot_val (XLSXWriteState *state, GsfXMLOut *xml,
		      GOVal const *v)
{
	switch (v->type) {
	case VALUE_CELLRANGE:
	case VALUE_ARRAY:
		g_warning ("REMOVE THIS CODE WHEN WE MOVE TO GOFFICE");
		break;

	case VALUE_EMPTY:
		gsf_xml_out_simple_element (xml, "m", NULL);
		break;

	case VALUE_BOOLEAN:
		gsf_xml_out_start_element (xml, "b");
		xlsx_add_bool (xml, "v", v->v_bool.val);
		gsf_xml_out_end_element (xml);
		break;

	case VALUE_FLOAT: {
		GOFormat const *fmt = go_val_get_fmt (v);
		if (NULL != fmt && go_format_is_date (fmt)) {
			char *d = format_value (state->date_fmt, v, NULL, -1, workbook_date_conv (state->base.wb));
			gsf_xml_out_start_element (xml, "d");
			gsf_xml_out_add_cstr_unchecked (xml, "v", d);
			gsf_xml_out_end_element (xml);
		} else {
			gsf_xml_out_start_element (xml, "n");
			gsf_xml_out_add_float (xml, "v", v->v_float.val, -1);
			gsf_xml_out_end_element (xml);
		}
		break;
	}

	case VALUE_ERROR :
		gsf_xml_out_start_element (xml, "e");
		gsf_xml_out_add_cstr (xml, "v", v->v_err.mesg->str);
		gsf_xml_out_end_element (xml);
		break;

	case VALUE_STRING :
		gsf_xml_out_start_element (xml, "s");
		gsf_xml_out_add_cstr (xml, "v", v->v_str.val->str);
		gsf_xml_out_end_element (xml);
		break;
	}
}

static char const *
xlsx_write_pivot_cache_records (XLSXWriteState *state, GODataCache const *cache,
				GsfOutput *cache_def_part, unsigned int cache_records_num)
{
	unsigned int i, j;
	GsfXMLOut *xml;
	char *name = g_strdup_printf ("pivotCacheRecords%u.xml", cache_records_num);
	GsfOutput *record_part = gsf_outfile_new_child_full (state->pivotCache.dir, name, FALSE,
		"content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml",
		NULL);
	char const *record_id = gsf_outfile_open_pkg_relate (GSF_OUTFILE_OPEN_PKG (record_part),
		GSF_OUTFILE_OPEN_PKG (cache_def_part),
		"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheRecords");

	xml = gsf_xml_out_new (record_part);

	gsf_xml_out_start_element (xml, "pivotCacheRecords");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns:r", ns_rel);

	gsf_xml_out_add_int (xml, "count", go_data_cache_num_items (cache));
	for (j = 0 ; j < go_data_cache_num_items (cache); j++) {
		gsf_xml_out_start_element (xml, "r");
		for (i = 0 ; i < go_data_cache_num_fields (cache); i++) {
			GODataCacheField *field = go_data_cache_get_field (cache, i);
			switch (go_data_cache_field_ref_type (field)) {
			case GO_DATA_CACHE_FIELD_TYPE_INDEXED_I8 :
			case GO_DATA_CACHE_FIELD_TYPE_INDEXED_I16 :	/* fallthrough */
			case GO_DATA_CACHE_FIELD_TYPE_INDEXED_I32 :	/* fallthrough */
				gsf_xml_out_start_element (xml, "x");
				gsf_xml_out_add_int (xml, "v",
					go_data_cache_get_index (cache, field, j));
				gsf_xml_out_end_element (xml);
				break;

			case GO_DATA_CACHE_FIELD_TYPE_INLINE :
				xlsx_write_pivot_val (state, xml,
					go_data_cache_field_get_val (field, j));
			break;

			case GO_DATA_CACHE_FIELD_TYPE_NONE :
				continue;
			}
		}
		gsf_xml_out_end_element (xml); /* </r> */
	}
	gsf_xml_out_end_element (xml); /* </pivotCacheRecords> */

	g_object_unref (xml);
	gsf_output_close (record_part);
	g_object_unref (record_part);
	g_free (name);

	return record_id;
}

static void
xlsx_write_pivot_cache_source (XLSXWriteState *state, GsfXMLOut *xml, GODataCache const *cache)
{
	GODataCacheSource const *src = go_data_cache_get_source (cache);

	if (NULL == src)
		return;

	if (IS_GNM_DATA_CACHE_SOURCE (src)) {
		GnmDataCacheSource const *ssrc = GNM_DATA_CACHE_SOURCE (src);
		Sheet const *src_sheet	= gnm_data_cache_source_get_sheet (ssrc);
		GnmRange const	*r	= gnm_data_cache_source_get_range (ssrc);
		char const	*name	= gnm_data_cache_source_get_name  (ssrc);
		gsf_xml_out_start_element (xml, "cacheSource");
		gsf_xml_out_add_cstr_unchecked (xml, "type", "worksheet");
		gsf_xml_out_start_element (xml, "worksheetSource");
		if (NULL != r)		xlsx_add_range (xml, "ref", r);
		if (NULL != src_sheet)	gsf_xml_out_add_cstr (xml, "sheet", src_sheet->name_unquoted);
		if (NULL != name)	gsf_xml_out_add_cstr (xml, "name", name);
		/* "id" == sheetId : do we need this ? */
		gsf_xml_out_end_element (xml); /* </worksheetSource> */
		gsf_xml_out_end_element (xml); /* </cacheSource> */
	} else {
		g_warning ("UNSUPPORTED  GODataCacheSource of type %s", G_OBJECT_TYPE_NAME(src));
	}
}

static void
xlsx_write_pivot_val_array (XLSXWriteState *state, GsfXMLOut *xml,
			    GOValArray const *vals, char const *name)
{
	unsigned int i;
	GOVal const *v;

	gsf_xml_out_start_element (xml, name);
	gsf_xml_out_add_uint (xml, "count", vals->len);
	for (i = 0 ; i < vals->len ; i++)
		if (NULL != (v = g_ptr_array_index (vals, i)))
			xlsx_write_pivot_val (state, xml, v);
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_date (XLSXWriteState *state, GsfXMLOut *xml,
		 char const *id, gnm_float v)
{
	GOVal *tmp = go_val_new_float (v);
	char *d = format_value (state->date_fmt, tmp, NULL, -1, workbook_date_conv (state->base.wb));
	gsf_xml_out_add_cstr_unchecked (xml, id, d);
	g_free (d);
	go_val_free (tmp);
}

static void
xlsx_write_pivot_cache_field (XLSXWriteState *state, GsfXMLOut *xml,
			      GODataCacheField const *field)
{
	GOValArray const *vals;

	gsf_xml_out_start_element (xml, "cacheField");
	gsf_xml_out_add_cstr (xml, "name", go_data_cache_field_get_name (field)->str);
	gsf_xml_out_add_int (xml, "numFmtId", 0);	/* TODO */

	if (NULL != (vals = go_data_cache_field_get_vals (field, FALSE)))
		xlsx_write_pivot_val_array (state, xml, vals, "sharedItems");

	if (NULL != (vals = go_data_cache_field_get_vals (field, TRUE))) {
		int		parent_group;
		GOValBucketer  *bucketer = NULL;
		const char *group_by = NULL;

		g_object_get (G_OBJECT (field),
			      "group-parent", &parent_group,
			      "bucketer", &bucketer,
			      NULL);
		gsf_xml_out_start_element (xml, "fieldGroup");
		if (parent_group >= 0)
			gsf_xml_out_add_int (xml, "base", parent_group);

		gsf_xml_out_start_element (xml, "rangePr");
		switch (bucketer->type) {
		case GO_VAL_BUCKET_SECOND		: group_by = "seconds"; break;
		case GO_VAL_BUCKET_MINUTE		: group_by = "minutes"; break;
		case GO_VAL_BUCKET_HOUR			: group_by = "hours"; break;
		case GO_VAL_BUCKET_DAY_OF_YEAR		: group_by = "days"; break;
		case GO_VAL_BUCKET_MONTH		: group_by = "months"; break;
		case GO_VAL_BUCKET_CALENDAR_QUARTER	: group_by = "quarters"; break;
		case GO_VAL_BUCKET_YEAR			: group_by = "years"; break;
		default:
							  /* default to linear */;
		case GO_VAL_BUCKET_SERIES_LINEAR 	:break;
		}
		if (group_by) gsf_xml_out_add_cstr_unchecked (xml, "groupBy", group_by);
		if (bucketer->type == GO_VAL_BUCKET_SERIES_LINEAR) {
			gsf_xml_out_add_float (xml, "startNum",		bucketer->details.series.minimum, -1);
			gsf_xml_out_add_float (xml, "endNum",		bucketer->details.series.maximum, -1);
			gsf_xml_out_add_float (xml, "groupInterval",	bucketer->details.series.step, -1);
		} else {
			xlsx_write_date (state, xml, "startDate", bucketer->details.dates.minimum);
			xlsx_write_date (state, xml, "endDate",   bucketer->details.dates.maximum);
		}
		gsf_xml_out_end_element (xml); /* </rangePr> */

		xlsx_write_pivot_val_array (state, xml, vals, "groupItems");
		gsf_xml_out_end_element (xml); /* </fieldGroup> */
	}

	gsf_xml_out_end_element (xml); /* </cacheField> */
}

static char const *
xlsx_write_pivot_cache_definition (XLSXWriteState *state, GsfOutfile *wb_part,
				   GODataCache const *cache, unsigned int cache_def_num)
{
	GsfXMLOut *xml;
	int i, n;
	char const *record_id;
	char *name = g_strdup_printf ("pivotCacheDefinition%u.xml", cache_def_num);
	GsfOutput *cache_def_part = gsf_outfile_new_child_full (state->pivotCache.dir, name, FALSE,
		"content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml",
		NULL);
	char const *cache_def_id = gsf_outfile_open_pkg_relate (GSF_OUTFILE_OPEN_PKG (cache_def_part),
		GSF_OUTFILE_OPEN_PKG (wb_part),
		"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition");

	record_id = xlsx_write_pivot_cache_records (state, cache, cache_def_part, cache_def_num);

	xml = gsf_xml_out_new (cache_def_part);

	gsf_xml_out_start_element (xml, "pivotCacheDefinition");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns:r", ns_rel);

	gsf_xml_out_add_cstr (xml, "r:id", record_id);
	if (cache->refreshed_by) gsf_xml_out_add_cstr (xml, "refreshedBy", cache->refreshed_by);
	if (cache->refreshed_on) gsf_xml_out_add_float (xml, "refreshedDate", go_val_as_float (cache->refreshed_on), -1);
	gsf_xml_out_add_int (xml, "createdVersion",	cache->XL_created_ver);
	gsf_xml_out_add_int (xml, "refreshedVersion",	cache->XL_refresh_ver);
	gsf_xml_out_add_uint (xml, "recordCount",	go_data_cache_num_items  (cache));
	xlsx_add_bool (xml, "upgradeOnRefresh", cache->refresh_upgrades);
	xlsx_write_pivot_cache_source (state, xml, cache);

	gsf_xml_out_start_element (xml, "cacheFields");
	n = go_data_cache_num_fields (cache);
	gsf_xml_out_add_uint (xml, "count", n);
	for (i = 0 ; i < n ; i++)
		xlsx_write_pivot_cache_field (state, xml, go_data_cache_get_field (cache, i));
	gsf_xml_out_end_element (xml); /* </cacheFields> */

	gsf_xml_out_end_element (xml); /* </pivotCacheDefinition> */

	g_object_unref (xml);
	gsf_output_close (cache_def_part);
	g_object_unref (cache_def_part);
	g_free (name);

	return cache_def_id;
}

static GSList *
xlsx_write_pivots (XLSXWriteState *state, GsfOutfile *wb_part)
{
	GHashTable *caches = excel_collect_pivot_caches (state->base.wb);
	GHashTableIter iter;
	GSList *refs = NULL;
	gpointer key, value;
	char const *cache_def_id;

	if (caches == NULL)
		return NULL;

	state->date_fmt = xlsx_pivot_date_fmt ();
	state->pivotCache.count = state->pivotTable.count = 0;
	state->pivotCache.dir = (GsfOutfile *)gsf_outfile_new_child (state->xl_dir, "pivotCache", TRUE);
	state->pivotTable.dir = (GsfOutfile *)gsf_outfile_new_child (state->xl_dir, "pivotTable", TRUE);

	g_hash_table_iter_init (&iter, caches);
	while (g_hash_table_iter_next (&iter, &key, &value))
		if (NULL != key) {
			cache_def_id = xlsx_write_pivot_cache_definition (state, wb_part, key, GPOINTER_TO_UINT(value));
			refs = g_slist_prepend (refs, (gpointer)cache_def_id);
		}

	gsf_output_close (GSF_OUTPUT (state->pivotCache.dir));
	gsf_output_close (GSF_OUTPUT (state->pivotTable.dir));
	g_hash_table_destroy (caches);
	go_format_unref	(state->date_fmt);

	return g_slist_reverse (refs);
}