xlsxwriter 0.2.1.pre → 0.2.1.pre.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ #include <ruby.h>
2
+ #include <xlsxwriter.h>
3
+
4
+ #ifndef __CHARTSHEET__
5
+ #define __CHARTSHEET__
6
+
7
+ struct chartsheet {
8
+ lxw_chartsheet *chartsheet;
9
+ };
10
+
11
+ void init_xlsxwriter_chartsheet();
12
+
13
+ extern VALUE cChartsheet;
14
+
15
+ #endif // __CHARTSHEET__
@@ -0,0 +1,104 @@
1
+ #include <ruby.h>
2
+ #include <ruby/intern.h>
3
+ #include <xlsxwriter_ext.h>
4
+
5
+ #ifndef __COMMON__
6
+ #define __COMMON__
7
+
8
+ #define LXW_NO_RESULT_CALL(cls, method, ...) { \
9
+ struct cls *ptr; \
10
+ Data_Get_Struct(self, struct cls, ptr); \
11
+ cls ## _ ## method(ptr->cls, ##__VA_ARGS__); }
12
+
13
+ #define LXW_ERR_RESULT_CALL(cls, method, ...) { \
14
+ struct cls *ptr; \
15
+ Data_Get_Struct(self, struct cls, ptr); \
16
+ lxw_error err = cls ## _ ## method(ptr->cls, ##__VA_ARGS__); \
17
+ handle_lxw_error(err); }
18
+
19
+
20
+ struct _protect_options {
21
+ uint8_t with_options;
22
+ const char *password;
23
+ lxw_protection options;
24
+ };
25
+
26
+ static inline struct _protect_options
27
+ _extract_protect_options(int argc, VALUE *argv) {
28
+ VALUE val;
29
+ VALUE opts = Qnil;
30
+ struct _protect_options po = {0};
31
+ // All options are off by default
32
+ if (argc > 0 && !NIL_P(argv[0])) {
33
+ switch (TYPE(argv[0])) {
34
+ case T_STRING:
35
+ po.password = StringValueCStr(argv[0]);
36
+ break;
37
+ case T_HASH:
38
+ opts = argv[0];
39
+ break;
40
+ default:
41
+ rb_raise(rb_eArgError, "Wrong argument %"PRIsVALUE", expected a String or Hash", rb_obj_class(argv[0]));
42
+ }
43
+ }
44
+
45
+ if (argc > 1) {
46
+ if (TYPE(argv[1]) == T_HASH) {
47
+ opts = argv[1];
48
+ } else {
49
+ rb_raise(rb_eArgError, "Expected a Hash, but got %"PRIsVALUE, rb_obj_class(argv[1]));
50
+ }
51
+ }
52
+
53
+ if (!NIL_P(opts)) {
54
+ #define PR_OPT(field) { \
55
+ val = rb_hash_aref(opts, ID2SYM(rb_intern(#field))); \
56
+ if (!NIL_P(val) && val) { \
57
+ po.options.field = 1; \
58
+ po.with_options = 1; \
59
+ } \
60
+ }
61
+ PR_OPT(no_select_locked_cells);
62
+ PR_OPT(no_select_unlocked_cells);
63
+ PR_OPT(format_cells);
64
+ PR_OPT(format_columns);
65
+ PR_OPT(format_rows);
66
+ PR_OPT(insert_columns);
67
+ PR_OPT(insert_rows);
68
+ PR_OPT(insert_hyperlinks);
69
+ PR_OPT(delete_columns);
70
+ PR_OPT(delete_rows);
71
+ PR_OPT(sort);
72
+ PR_OPT(autofilter);
73
+ PR_OPT(pivot_tables);
74
+ PR_OPT(scenarios);
75
+ PR_OPT(objects);
76
+ #undef PR_OPT
77
+ }
78
+
79
+ return po;
80
+ }
81
+
82
+
83
+ struct _header_options {
84
+ char with_options;
85
+ const char *str;
86
+ lxw_header_footer_options options;
87
+ };
88
+
89
+ static inline struct _header_options
90
+ _extract_header_options(int argc, VALUE *argv) {
91
+ struct _header_options ho = { 0 };
92
+ ho.str = StringValueCStr(argv[0]);
93
+ if (argc > 1 && !NIL_P(argv[1])) {
94
+ VALUE margin = rb_hash_aref(argv[1], ID2SYM(rb_intern("margin")));
95
+ if (!NIL_P(margin)) {
96
+ ho.with_options = '\1';
97
+ ho.options.margin = NUM2DBL(margin);
98
+ }
99
+ }
100
+ return ho;
101
+ }
102
+
103
+
104
+ #endif // __COMMON__
@@ -2,7 +2,10 @@
2
2
  #include <ruby.h>
3
3
  #include <ruby/thread.h>
4
4
  #include <xlsxwriter.h>
5
+
5
6
  #include "chart.h"
7
+ #include "chartsheet.h"
8
+ #include "common.h"
6
9
  #include "format.h"
7
10
  #include "workbook.h"
8
11
  #include "workbook_properties.h"
@@ -197,6 +200,38 @@ workbook_add_worksheet_(int argc, VALUE *argv, VALUE self) {
197
200
  return worksheet;
198
201
  }
199
202
 
203
+ /* call-seq:
204
+ * wb.add_chartsheet([name]) -> cs
205
+ * wb.add_chartsheet([name]) { |ws| block } -> obj
206
+ *
207
+ * Adds a chartsheet named +name+ to the workbook.
208
+ *
209
+ * If a block is passed, the last statement is returned.
210
+ *
211
+ * wb.add_chartsheet('Cool chart') do |cs|
212
+ * cs.chart = chart
213
+ * end
214
+ */
215
+ VALUE
216
+ workbook_add_chartsheet_(int argc, VALUE *argv, VALUE self) {
217
+ VALUE chartsheet = Qnil;
218
+
219
+ rb_check_arity(argc, 0, 1);
220
+
221
+ struct workbook *ptr;
222
+ Data_Get_Struct(self, struct workbook, ptr);
223
+ if (ptr->workbook) {
224
+ chartsheet = rb_funcall(cChartsheet, rb_intern("new"), argc + 1, self, argv[0]);
225
+ }
226
+
227
+ if (rb_block_given_p()) {
228
+ VALUE res = rb_yield(chartsheet);
229
+ return res;
230
+ }
231
+
232
+ return chartsheet;
233
+ }
234
+
200
235
  /* call-seq:
201
236
  * wb.add_format(key, definition) -> wb
202
237
  *
@@ -210,7 +245,7 @@ workbook_add_worksheet_(int argc, VALUE *argv, VALUE self) {
210
245
  * :font_color:: Text color.
211
246
  * :bold, :italic, underline :: Bold, italic, underlined text.
212
247
  * :font_strikeout:: Striked out text.
213
- * :font_script:: Superscript (XlsxWrtiter::Format::FONT_SUPERSCRIPT) or subscript (XlsxWriter::Format::FONT_SUBSCRIPT).
248
+ * :font_script:: Superscript (XlsxWriter::Format::FONT_SUPERSCRIPT) or subscript (XlsxWriter::Format::FONT_SUBSCRIPT).
214
249
  * :num_format:: Defines numerical format with mask, like <code>'d mmm yyyy'</code>
215
250
  * or <code>'#,##0.00'</code>.
216
251
  * :num_format_index:: Defines numerical format from special {pre-defined set}[https://libxlsxwriter.github.io/format_8h.html#a688aa42bcc703d17e125d9a34c721872].
@@ -291,12 +326,7 @@ workbook_add_chart_(VALUE self, VALUE type) {
291
326
  */
292
327
  VALUE
293
328
  workbook_add_vba_project_(VALUE self, VALUE filename) {
294
- struct workbook *ptr;
295
- Data_Get_Struct(self, struct workbook, ptr);
296
-
297
- lxw_error err = workbook_add_vba_project(ptr->workbook, StringValueCStr(filename));
298
- handle_lxw_error(err);
299
-
329
+ LXW_ERR_RESULT_CALL(workbook, add_vba_project, StringValueCStr(filename));
300
330
  return self;
301
331
  }
302
332
 
@@ -316,12 +346,7 @@ workbook_set_default_xf_indices_(VALUE self) {
316
346
  */
317
347
  VALUE
318
348
  workbook_set_vba_name_(VALUE self, VALUE name) {
319
- struct workbook *ptr;
320
- Data_Get_Struct(self, struct workbook, ptr);
321
-
322
- lxw_error err = workbook_set_vba_name(ptr->workbook, StringValueCStr(name));
323
- handle_lxw_error(err);
324
-
349
+ LXW_ERR_RESULT_CALL(workbook, set_vba_name, StringValueCStr(name));
325
350
  return name;
326
351
  }
327
352
 
@@ -346,9 +371,8 @@ workbook_properties_(VALUE self) {
346
371
  */
347
372
  VALUE
348
373
  workbook_define_name_(VALUE self, VALUE name, VALUE formula) {
349
- struct workbook *ptr;
350
- Data_Get_Struct(self, struct workbook, ptr);
351
- workbook_define_name(ptr->workbook, StringValueCStr(name), StringValueCStr(formula));
374
+ LXW_ERR_RESULT_CALL(workbook, define_name, StringValueCStr(name), StringValueCStr(formula));
375
+
352
376
  return self;
353
377
  }
354
378
 
@@ -362,11 +386,7 @@ workbook_define_name_(VALUE self, VALUE name, VALUE formula) {
362
386
  */
363
387
  VALUE
364
388
  workbook_validate_sheet_name_(VALUE self, VALUE name) {
365
- struct workbook *ptr;
366
- lxw_error err;
367
- Data_Get_Struct(self, struct workbook, ptr);
368
- err = workbook_validate_sheet_name(ptr->workbook, StringValueCStr(name));
369
- handle_lxw_error(err);
389
+ LXW_ERR_RESULT_CALL(workbook, validate_sheet_name, StringValueCStr(name));
370
390
  return Qtrue;
371
391
  }
372
392
 
@@ -428,6 +448,7 @@ init_xlsxwriter_workbook() {
428
448
  rb_define_method(cWorkbook, "add_format", workbook_add_format_, 2);
429
449
  rb_define_method(cWorkbook, "add_vba_project", workbook_add_vba_project_, 1);
430
450
  rb_define_method(cWorkbook, "add_worksheet", workbook_add_worksheet_, -1);
451
+ rb_define_method(cWorkbook, "add_chartsheet", workbook_add_chartsheet_, -1);
431
452
  rb_define_method(cWorkbook, "define_name", workbook_define_name_, 2);
432
453
  rb_define_method(cWorkbook, "properties", workbook_properties_, 0);
433
454
  rb_define_method(cWorkbook, "set_default_xf_indices", workbook_set_default_xf_indices_, 0);
@@ -1,4 +1,5 @@
1
1
  #include "chart.h"
2
+ #include "common.h"
2
3
  #include "rich_string.h"
3
4
  #include "worksheet.h"
4
5
  #include "workbook.h"
@@ -10,8 +11,7 @@ void worksheet_free(void *p);
10
11
 
11
12
 
12
13
  VALUE
13
- worksheet_alloc(VALUE klass)
14
- {
14
+ worksheet_alloc(VALUE klass) {
15
15
  VALUE obj;
16
16
  struct worksheet *ptr;
17
17
 
@@ -33,9 +33,8 @@ worksheet_init(int argc, VALUE *argv, VALUE self) {
33
33
 
34
34
  Data_Get_Struct(self, struct worksheet, ptr);
35
35
 
36
- if (argc > 2 || argc < 1) {
37
- rb_raise(rb_eArgError, "wrong number of arguments");
38
- } else if (argc == 2) {
36
+ rb_check_arity(argc, 1, 2);
37
+ if (argc == 2) {
39
38
  switch (TYPE(argv[1])) {
40
39
  case T_HASH:
41
40
  opts = argv[1];
@@ -120,11 +119,9 @@ worksheet_write_string_(int argc, VALUE *argv, VALUE self) {
120
119
  }
121
120
 
122
121
  const char *str = StringValueCStr(value);
123
- struct worksheet *ptr;
124
122
  VALUE workbook = rb_iv_get(self, "@workbook");
125
123
  lxw_format *format = workbook_get_format(workbook, format_key);
126
- Data_Get_Struct(self, struct worksheet, ptr);
127
- worksheet_write_string(ptr->worksheet, row, col, str, format);
124
+ LXW_NO_RESULT_CALL(worksheet, write_string, row, col, str, format);
128
125
  return self;
129
126
  }
130
127
 
@@ -155,11 +152,9 @@ worksheet_write_number_(int argc, VALUE *argv, VALUE self) {
155
152
  }
156
153
 
157
154
  const double num = NUM2DBL(value);
158
- struct worksheet *ptr;
159
155
  VALUE workbook = rb_iv_get(self, "@workbook");
160
156
  lxw_format *format = workbook_get_format(workbook, format_key);
161
- Data_Get_Struct(self, struct worksheet, ptr);
162
- worksheet_write_number(ptr->worksheet, row, col, num, format);
157
+ LXW_NO_RESULT_CALL(worksheet, write_number, row, col, num, format);
163
158
  return self;
164
159
  }
165
160
 
@@ -190,11 +185,9 @@ worksheet_write_formula_(int argc, VALUE *argv, VALUE self) {
190
185
  }
191
186
 
192
187
  const char *str = RSTRING_PTR(value);
193
- struct worksheet *ptr;
194
188
  VALUE workbook = rb_iv_get(self, "@workbook");
195
189
  lxw_format *format = workbook_get_format(workbook, format_key);
196
- Data_Get_Struct(self, struct worksheet, ptr);
197
- worksheet_write_formula(ptr->worksheet, row, col, str, format);
190
+ LXW_NO_RESULT_CALL(worksheet, write_formula, row, col, str, format);
198
191
  return self;
199
192
  }
200
193
 
@@ -226,11 +219,9 @@ VALUE worksheet_write_array_formula_(int argc, VALUE *argv, VALUE self) {
226
219
  ++larg;
227
220
  }
228
221
 
229
- struct worksheet *ptr;
230
222
  VALUE workbook = rb_iv_get(self, "@workbook");
231
223
  lxw_format *format = workbook_get_format(workbook, format_key);
232
- Data_Get_Struct(self, struct worksheet, ptr);
233
- worksheet_write_array_formula(ptr->worksheet, row_from, col_from, row_to, col_to, str, format);
224
+ LXW_NO_RESULT_CALL(worksheet, write_array_formula, row_from, col_from, row_to, col_to, str, format);
234
225
  return self;
235
226
  }
236
227
 
@@ -261,11 +252,9 @@ worksheet_write_datetime_(int argc, VALUE *argv, VALUE self) {
261
252
  }
262
253
 
263
254
  struct lxw_datetime datetime = value_to_lxw_datetime(value);
264
- struct worksheet *ptr;
265
255
  VALUE workbook = rb_iv_get(self, "@workbook");
266
256
  lxw_format *format = workbook_get_format(workbook, format_key);
267
- Data_Get_Struct(self, struct worksheet, ptr);
268
- worksheet_write_datetime(ptr->worksheet, row, col, &datetime, format);
257
+ LXW_NO_RESULT_CALL(worksheet, write_datetime, row, col, &datetime, format);
269
258
  return self;
270
259
  }
271
260
 
@@ -360,11 +349,9 @@ worksheet_write_boolean_(int argc, VALUE *argv, VALUE self) {
360
349
  ++larg;
361
350
  }
362
351
 
363
- struct worksheet *ptr;
364
352
  VALUE workbook = rb_iv_get(self, "@workbook");
365
353
  lxw_format *format = workbook_get_format(workbook, format_key);
366
- Data_Get_Struct(self, struct worksheet, ptr);
367
- worksheet_write_boolean(ptr->worksheet, row, col, bool_value, format);
354
+ LXW_NO_RESULT_CALL(worksheet, write_boolean, row, col, bool_value, format);
368
355
  return self;
369
356
  }
370
357
 
@@ -388,11 +375,9 @@ worksheet_write_blank_(int argc, VALUE *argv, VALUE self) {
388
375
  ++larg;
389
376
  }
390
377
 
391
- struct worksheet *ptr;
392
378
  VALUE workbook = rb_iv_get(self, "@workbook");
393
379
  lxw_format *format = workbook_get_format(workbook, format_key);
394
- Data_Get_Struct(self, struct worksheet, ptr);
395
- worksheet_write_blank(ptr->worksheet, row, col, format);
380
+ LXW_NO_RESULT_CALL(worksheet, write_blank, row, col, format);
396
381
  return self;
397
382
  }
398
383
 
@@ -429,11 +414,9 @@ worksheet_write_formula_num_(int argc, VALUE *argv, VALUE self) {
429
414
  }
430
415
 
431
416
  const char *str = RSTRING_PTR(formula);
432
- struct worksheet *ptr;
433
417
  VALUE workbook = rb_iv_get(self, "@workbook");
434
418
  lxw_format *format = workbook_get_format(workbook, format_key);
435
- Data_Get_Struct(self, struct worksheet, ptr);
436
- worksheet_write_formula_num(ptr->worksheet, row, col, str, format, NUM2DBL(value));
419
+ LXW_NO_RESULT_CALL(worksheet, write_formula_num, row, col, str, format, NUM2DBL(value));
437
420
  return self;
438
421
  }
439
422
 
@@ -473,13 +456,10 @@ worksheet_write_rich_string_(int argc, VALUE *argv, VALUE self) {
473
456
  rb_raise(rb_eArgError, "No value specified");
474
457
  }
475
458
 
476
- struct worksheet *ptr;
477
459
  lxw_format *format = workbook_get_format(workbook, format_key);
478
460
  lxw_rich_string_tuple **rich_string = serialize_rich_string(value);
479
461
  if (rich_string) {
480
- Data_Get_Struct(self, struct worksheet, ptr);
481
- lxw_error err = worksheet_write_rich_string(ptr->worksheet, row, col, rich_string, format);
482
- handle_lxw_error(err);
462
+ LXW_ERR_RESULT_CALL(worksheet, write_rich_string, row, col, rich_string, format);
483
463
  }
484
464
  return self;
485
465
  }
@@ -752,10 +732,7 @@ worksheet_merge_range_(int argc, VALUE *argv, VALUE self) {
752
732
  ++larg;
753
733
  }
754
734
 
755
- struct worksheet *ptr;
756
- Data_Get_Struct(self, struct worksheet, ptr);
757
-
758
- worksheet_merge_range(ptr->worksheet, row1, col1, row2, col2, str, format);
735
+ LXW_NO_RESULT_CALL(worksheet, merge_range, row1, col1, row2, col2, str, format);
759
736
  return self;
760
737
  }
761
738
 
@@ -778,10 +755,7 @@ worksheet_autofilter_(int argc, VALUE *argv, VALUE self) {
778
755
  rb_check_arity(argc, 1, 4);
779
756
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);
780
757
 
781
- struct worksheet *ptr;
782
- Data_Get_Struct(self, struct worksheet, ptr);
783
-
784
- worksheet_autofilter(ptr->worksheet, row_from, col_from, row_to, col_to);
758
+ LXW_NO_RESULT_CALL(worksheet, autofilter, row_from, col_from, row_to, col_to);
785
759
  return self;
786
760
  }
787
761
 
@@ -791,21 +765,17 @@ worksheet_autofilter_(int argc, VALUE *argv, VALUE self) {
791
765
  */
792
766
  VALUE
793
767
  worksheet_activate_(VALUE self) {
794
- struct worksheet *ptr;
795
- Data_Get_Struct(self, struct worksheet, ptr);
796
- worksheet_activate(ptr->worksheet);
768
+ LXW_NO_RESULT_CALL(worksheet, activate);
797
769
  return self;
798
770
  }
799
771
 
800
772
  /* call-seq: ws.select -> self
801
773
  *
802
- * Set the worksheet to be selected on openong the workbook.
774
+ * Set the worksheet to be selected on opening the workbook.
803
775
  */
804
776
  VALUE
805
777
  worksheet_select_(VALUE self) {
806
- struct worksheet *ptr;
807
- Data_Get_Struct(self, struct worksheet, ptr);
808
- worksheet_select(ptr->worksheet);
778
+ LXW_NO_RESULT_CALL(worksheet, select);
809
779
  return self;
810
780
  }
811
781
 
@@ -815,9 +785,7 @@ worksheet_select_(VALUE self) {
815
785
  */
816
786
  VALUE
817
787
  worksheet_hide_(VALUE self) {
818
- struct worksheet *ptr;
819
- Data_Get_Struct(self, struct worksheet, ptr);
820
- worksheet_hide(ptr->worksheet);
788
+ LXW_NO_RESULT_CALL(worksheet, hide);
821
789
  return self;
822
790
  }
823
791
 
@@ -828,9 +796,7 @@ worksheet_hide_(VALUE self) {
828
796
  */
829
797
  VALUE
830
798
  worksheet_set_first_sheet_(VALUE self) {
831
- struct worksheet *ptr;
832
- Data_Get_Struct(self, struct worksheet, ptr);
833
- worksheet_set_first_sheet(ptr->worksheet);
799
+ LXW_NO_RESULT_CALL(worksheet, set_first_sheet);
834
800
  return self;
835
801
  }
836
802
 
@@ -876,9 +842,7 @@ worksheet_freeze_panes_(int argc, VALUE *argv, VALUE self) {
876
842
  */
877
843
  VALUE
878
844
  worksheet_split_panes_(VALUE self, VALUE vertical, VALUE horizontal) {
879
- struct worksheet *ptr;
880
- Data_Get_Struct(self, struct worksheet, ptr);
881
- worksheet_split_panes(ptr->worksheet, NUM2DBL(vertical), NUM2DBL(horizontal));
845
+ LXW_NO_RESULT_CALL(worksheet, split_panes, NUM2DBL(vertical), NUM2DBL(horizontal));
882
846
  return self;
883
847
  }
884
848
 
@@ -901,10 +865,7 @@ worksheet_set_selection_(int argc, VALUE *argv, VALUE self) {
901
865
  rb_check_arity(argc, 1, 4);
902
866
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);
903
867
 
904
- struct worksheet *ptr;
905
- Data_Get_Struct(self, struct worksheet, ptr);
906
-
907
- worksheet_set_selection(ptr->worksheet, row_from, col_from, row_to, col_to);
868
+ LXW_NO_RESULT_CALL(worksheet, set_selection, row_from, col_from, row_to, col_to);
908
869
  return self;
909
870
  }
910
871
 
@@ -914,9 +875,7 @@ worksheet_set_selection_(int argc, VALUE *argv, VALUE self) {
914
875
  */
915
876
  VALUE
916
877
  worksheet_set_landscape_(VALUE self) {
917
- struct worksheet *ptr;
918
- Data_Get_Struct(self, struct worksheet, ptr);
919
- worksheet_set_landscape(ptr->worksheet);
878
+ LXW_NO_RESULT_CALL(worksheet, set_landscape);
920
879
  return self;
921
880
  }
922
881
 
@@ -926,9 +885,7 @@ worksheet_set_landscape_(VALUE self) {
926
885
  */
927
886
  VALUE
928
887
  worksheet_set_portrait_(VALUE self) {
929
- struct worksheet *ptr;
930
- Data_Get_Struct(self, struct worksheet, ptr);
931
- worksheet_set_portrait(ptr->worksheet);
888
+ LXW_NO_RESULT_CALL(worksheet, set_portrait);
932
889
  return self;
933
890
  }
934
891
 
@@ -938,9 +895,7 @@ worksheet_set_portrait_(VALUE self) {
938
895
  */
939
896
  VALUE
940
897
  worksheet_set_page_view_(VALUE self) {
941
- struct worksheet *ptr;
942
- Data_Get_Struct(self, struct worksheet, ptr);
943
- worksheet_set_page_view(ptr->worksheet);
898
+ LXW_NO_RESULT_CALL(worksheet, set_page_view);
944
899
  return self;
945
900
  }
946
901
 
@@ -950,22 +905,18 @@ worksheet_set_page_view_(VALUE self) {
950
905
  */
951
906
  VALUE
952
907
  worksheet_set_paper_(VALUE self, VALUE paper_type) {
953
- struct worksheet *ptr;
954
- Data_Get_Struct(self, struct worksheet, ptr);
955
- worksheet_set_paper(ptr->worksheet, NUM2INT(paper_type));
908
+ LXW_NO_RESULT_CALL(worksheet, set_paper, NUM2INT(paper_type));
956
909
  return self;
957
910
  }
958
911
 
959
912
  /* Sets the worksheet margins (Numeric) for the printed page. */
960
913
  VALUE
961
914
  worksheet_set_margins_(VALUE self, VALUE left, VALUE right, VALUE top, VALUE bottom) {
962
- struct worksheet *ptr;
963
- Data_Get_Struct(self, struct worksheet, ptr);
964
- worksheet_set_margins(ptr->worksheet, NUM2DBL(left), NUM2DBL(right), NUM2DBL(top), NUM2DBL(bottom));
915
+ LXW_NO_RESULT_CALL(worksheet, set_margins, NUM2DBL(left), NUM2DBL(right), NUM2DBL(top), NUM2DBL(bottom));
965
916
  return self;
966
917
  }
967
918
 
968
- /* call-seq: ws.set_header(text, opts) -> self
919
+ /* call-seq: ws.set_header(text[, opts]) -> self
969
920
  *
970
921
  * See {libxlsxwriter doc}[https://libxlsxwriter.github.io/worksheet_8h.html#a4070c24ed5107f33e94f30a1bf865ba9]
971
922
  * for the +text+ control characters.
@@ -973,24 +924,18 @@ worksheet_set_margins_(VALUE self, VALUE left, VALUE right, VALUE top, VALUE bot
973
924
  * Currently the only supported option is +:margin+ (Numeric).
974
925
  */
975
926
  VALUE
976
- worksheet_set_header_(VALUE self, VALUE val, VALUE opts) {
977
- const char *str = StringValueCStr(val);
978
- lxw_header_footer_options options = { .margin = 0.0 };
979
- char with_options = '\0';
980
- if (!NIL_P(opts)) {
981
- VALUE margin = rb_hash_aref(opts, ID2SYM(rb_intern("margin")));
982
- if (!NIL_P(margin)) {
983
- with_options = '\1';
984
- options.margin = NUM2DBL(margin);
985
- }
986
- }
927
+ worksheet_set_header_(int argc, VALUE *argv, VALUE self) {
928
+ rb_check_arity(argc, 1, 2);
929
+ struct _header_options ho = _extract_header_options(argc, argv);
987
930
  struct worksheet *ptr;
988
931
  Data_Get_Struct(self, struct worksheet, ptr);
989
- if (with_options) {
990
- worksheet_set_header(ptr->worksheet, str);
932
+ lxw_error err;
933
+ if (!ho.with_options) {
934
+ err = worksheet_set_header(ptr->worksheet, ho.str);
991
935
  } else {
992
- worksheet_set_header_opt(ptr->worksheet, str, &options);
936
+ err = worksheet_set_header_opt(ptr->worksheet, ho.str, &ho.options);
993
937
  }
938
+ handle_lxw_error(err);
994
939
  return self;
995
940
  }
996
941
 
@@ -999,24 +944,18 @@ worksheet_set_header_(VALUE self, VALUE val, VALUE opts) {
999
944
  * See #set_header for params description.
1000
945
  */
1001
946
  VALUE
1002
- worksheet_set_footer_(VALUE self, VALUE val, VALUE opts) {
1003
- const char *str = StringValueCStr(val);
1004
- lxw_header_footer_options options = { .margin = 0.0 };
1005
- char with_options = '\0';
1006
- if (!NIL_P(opts)) {
1007
- VALUE margin = rb_hash_aref(opts, ID2SYM(rb_intern("margin")));
1008
- if (!NIL_P(margin)) {
1009
- with_options = '\1';
1010
- options.margin = NUM2DBL(margin);
1011
- }
1012
- }
947
+ worksheet_set_footer_(int argc, VALUE *argv, VALUE self) {
1013
948
  struct worksheet *ptr;
949
+ rb_check_arity(argc, 1, 2);
950
+ struct _header_options ho = _extract_header_options(argc, argv);
1014
951
  Data_Get_Struct(self, struct worksheet, ptr);
1015
- if (with_options) {
1016
- worksheet_set_footer(ptr->worksheet, str);
952
+ lxw_error err;
953
+ if (!ho.with_options) {
954
+ err = worksheet_set_footer(ptr->worksheet, ho.str);
1017
955
  } else {
1018
- worksheet_set_footer_opt(ptr->worksheet, str, &options);
956
+ err = worksheet_set_footer_opt(ptr->worksheet, ho.str, &ho.options);
1019
957
  }
958
+ handle_lxw_error(err);
1020
959
  return self;
1021
960
  }
1022
961
 
@@ -1035,9 +974,7 @@ worksheet_set_h_pagebreaks_(VALUE self, VALUE val) {
1035
974
  rows[i] = NUM2INT(rb_ary_entry(val, i));
1036
975
  }
1037
976
  rows[len] = 0;
1038
- struct worksheet *ptr;
1039
- Data_Get_Struct(self, struct worksheet, ptr);
1040
- worksheet_set_h_pagebreaks(ptr->worksheet, rows);
977
+ LXW_NO_RESULT_CALL(worksheet, set_h_pagebreaks, rows);
1041
978
  return val;
1042
979
  }
1043
980
 
@@ -1056,18 +993,14 @@ worksheet_set_v_pagebreaks_(VALUE self, VALUE val) {
1056
993
  cols[i] = value_to_col(rb_ary_entry(val, i));
1057
994
  }
1058
995
  cols[len] = 0;
1059
- struct worksheet *ptr;
1060
- Data_Get_Struct(self, struct worksheet, ptr);
1061
- worksheet_set_v_pagebreaks(ptr->worksheet, cols);
996
+ LXW_NO_RESULT_CALL(worksheet, set_v_pagebreaks, cols);
1062
997
  return val;
1063
998
  }
1064
999
 
1065
1000
  /* Changes the default print direction */
1066
1001
  VALUE
1067
1002
  worksheet_print_across_(VALUE self) {
1068
- struct worksheet *ptr;
1069
- Data_Get_Struct(self, struct worksheet, ptr);
1070
- worksheet_print_across(ptr->worksheet);
1003
+ LXW_NO_RESULT_CALL(worksheet, print_across);
1071
1004
  return self;
1072
1005
  }
1073
1006
 
@@ -1077,9 +1010,7 @@ worksheet_print_across_(VALUE self) {
1077
1010
  */
1078
1011
  VALUE
1079
1012
  worksheet_set_zoom_(VALUE self, VALUE val) {
1080
- struct worksheet *ptr;
1081
- Data_Get_Struct(self, struct worksheet, ptr);
1082
- worksheet_set_zoom(ptr->worksheet, NUM2INT(val));
1013
+ LXW_NO_RESULT_CALL(worksheet, set_zoom, NUM2INT(val));
1083
1014
  return self;
1084
1015
  }
1085
1016
 
@@ -1091,38 +1022,28 @@ worksheet_set_zoom_(VALUE self, VALUE val) {
1091
1022
  */
1092
1023
  VALUE
1093
1024
  worksheet_gridlines_(VALUE self, VALUE value) {
1094
- struct worksheet *ptr;
1095
- Data_Get_Struct(self, struct worksheet, ptr);
1096
-
1097
- worksheet_gridlines(ptr->worksheet, NUM2INT(value));
1098
-
1025
+ LXW_NO_RESULT_CALL(worksheet, gridlines, NUM2INT(value));
1099
1026
  return value;
1100
1027
  }
1101
1028
 
1102
1029
  /* Center the worksheet data horizontally between the margins on the printed page */
1103
1030
  VALUE
1104
1031
  worksheet_center_horizontally_(VALUE self){
1105
- struct worksheet *ptr;
1106
- Data_Get_Struct(self, struct worksheet, ptr);
1107
- worksheet_center_horizontally(ptr->worksheet);
1032
+ LXW_NO_RESULT_CALL(worksheet, center_horizontally);
1108
1033
  return self;
1109
1034
  }
1110
1035
 
1111
1036
  /* Center the worksheet data vertically between the margins on the printed page */
1112
1037
  VALUE
1113
1038
  worksheet_center_vertically_(VALUE self) {
1114
- struct worksheet *ptr;
1115
- Data_Get_Struct(self, struct worksheet, ptr);
1116
- worksheet_center_vertically(ptr->worksheet);
1117
- return self;
1039
+ LXW_NO_RESULT_CALL(worksheet, center_vertically);
1040
+ return self;
1118
1041
  }
1119
1042
 
1120
1043
  /* Print rows and column header (wich is disabled by default). */
1121
1044
  VALUE
1122
1045
  worksheet_print_row_col_headers_(VALUE self) {
1123
- struct worksheet *ptr;
1124
- Data_Get_Struct(self, struct worksheet, ptr);
1125
- worksheet_print_row_col_headers(ptr->worksheet);
1046
+ LXW_NO_RESULT_CALL(worksheet, print_row_col_headers);
1126
1047
  return self;
1127
1048
  }
1128
1049
 
@@ -1132,9 +1053,7 @@ worksheet_print_row_col_headers_(VALUE self) {
1132
1053
  */
1133
1054
  VALUE
1134
1055
  worksheet_repeat_rows_(VALUE self, VALUE row_from, VALUE row_to) {
1135
- struct worksheet *ptr;
1136
- Data_Get_Struct(self, struct worksheet, ptr);
1137
- worksheet_repeat_rows(ptr->worksheet, NUM2INT(row_from), NUM2INT(row_to));
1056
+ LXW_NO_RESULT_CALL(worksheet, repeat_rows, NUM2INT(row_from), NUM2INT(row_to));
1138
1057
  return self;
1139
1058
  }
1140
1059
 
@@ -1144,9 +1063,7 @@ worksheet_repeat_rows_(VALUE self, VALUE row_from, VALUE row_to) {
1144
1063
  */
1145
1064
  VALUE
1146
1065
  worksheet_repeat_columns_(VALUE self, VALUE col_from, VALUE col_to) {
1147
- struct worksheet *ptr;
1148
- Data_Get_Struct(self, struct worksheet, ptr);
1149
- worksheet_repeat_columns(ptr->worksheet, value_to_col(col_from), value_to_col(col_to));
1066
+ LXW_NO_RESULT_CALL(worksheet, repeat_columns, value_to_col(col_from), value_to_col(col_to));
1150
1067
  return self;
1151
1068
  }
1152
1069
 
@@ -1165,10 +1082,7 @@ worksheet_print_area_(int argc, VALUE *argv, VALUE self) {
1165
1082
  rb_check_arity(argc, 1, 4);
1166
1083
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);
1167
1084
 
1168
- struct worksheet *ptr;
1169
- Data_Get_Struct(self, struct worksheet, ptr);
1170
-
1171
- worksheet_print_area(ptr->worksheet, row_from, col_from, row_to, col_to);
1085
+ LXW_NO_RESULT_CALL(worksheet, print_area, row_from, col_from, row_to, col_to);
1172
1086
  return self;
1173
1087
  }
1174
1088
 
@@ -1179,9 +1093,7 @@ worksheet_print_area_(int argc, VALUE *argv, VALUE self) {
1179
1093
  */
1180
1094
  VALUE
1181
1095
  worksheet_fit_to_pages_(VALUE self, VALUE width, VALUE height) {
1182
- struct worksheet *ptr;
1183
- Data_Get_Struct(self, struct worksheet, ptr);
1184
- worksheet_fit_to_pages(ptr->worksheet, NUM2INT(width), NUM2INT(height));
1096
+ LXW_NO_RESULT_CALL(worksheet, fit_to_pages, NUM2INT(width), NUM2INT(height));
1185
1097
  return self;
1186
1098
  }
1187
1099
 
@@ -1191,9 +1103,7 @@ worksheet_fit_to_pages_(VALUE self, VALUE width, VALUE height) {
1191
1103
  */
1192
1104
  VALUE
1193
1105
  worksheet_set_start_page_(VALUE self, VALUE start_page) {
1194
- struct worksheet *ptr;
1195
- Data_Get_Struct(self, struct worksheet, ptr);
1196
- worksheet_set_start_page(ptr->worksheet, NUM2INT(start_page));
1106
+ LXW_NO_RESULT_CALL(worksheet, set_start_page, NUM2INT(start_page));
1197
1107
  return start_page;
1198
1108
  }
1199
1109
 
@@ -1203,27 +1113,21 @@ worksheet_set_start_page_(VALUE self, VALUE start_page) {
1203
1113
  */
1204
1114
  VALUE
1205
1115
  worksheet_set_print_scale_(VALUE self, VALUE scale) {
1206
- struct worksheet *ptr;
1207
- Data_Get_Struct(self, struct worksheet, ptr);
1208
- worksheet_set_print_scale(ptr->worksheet, NUM2INT(scale));
1116
+ LXW_NO_RESULT_CALL(worksheet, set_print_scale, NUM2INT(scale));
1209
1117
  return scale;
1210
1118
  }
1211
1119
 
1212
1120
  /* Sets text direction to rtl (e.g. for worksheets on Hebrew or Arabic). */
1213
1121
  VALUE
1214
1122
  worksheet_right_to_left_(VALUE self) {
1215
- struct worksheet *ptr;
1216
- Data_Get_Struct(self, struct worksheet, ptr);
1217
- worksheet_right_to_left(ptr->worksheet);
1123
+ LXW_NO_RESULT_CALL(worksheet, right_to_left);
1218
1124
  return self;
1219
1125
  }
1220
1126
 
1221
1127
  /* Hides all zero values. */
1222
1128
  VALUE
1223
1129
  worksheet_hide_zero_(VALUE self) {
1224
- struct worksheet *ptr;
1225
- Data_Get_Struct(self, struct worksheet, ptr);
1226
- worksheet_hide_zero(ptr->worksheet);
1130
+ LXW_NO_RESULT_CALL(worksheet, hide_zero);
1227
1131
  return self;
1228
1132
  }
1229
1133
 
@@ -1235,9 +1139,7 @@ worksheet_hide_zero_(VALUE self) {
1235
1139
  */
1236
1140
  VALUE
1237
1141
  worksheet_set_tab_color_(VALUE self, VALUE color) {
1238
- struct worksheet *ptr;
1239
- Data_Get_Struct(self, struct worksheet, ptr);
1240
- worksheet_set_tab_color(ptr->worksheet, NUM2INT(color));
1142
+ LXW_NO_RESULT_CALL(worksheet, set_tab_color, NUM2INT(color));
1241
1143
  return color;
1242
1144
  }
1243
1145
 
@@ -1250,65 +1152,8 @@ worksheet_set_tab_color_(VALUE self, VALUE color) {
1250
1152
  VALUE
1251
1153
  worksheet_protect_(int argc, VALUE *argv, VALUE self) {
1252
1154
  rb_check_arity(argc, 0, 2);
1253
- uint8_t with_options = '\0';
1254
- VALUE val;
1255
- VALUE opts = Qnil;
1256
- // All options are off by default
1257
- lxw_protection options = {};
1258
- const char *password = NULL;
1259
- if (argc > 0 && !NIL_P(argv[0])) {
1260
- switch (TYPE(argv[0])) {
1261
- case T_STRING:
1262
- password = StringValueCStr(argv[0]);
1263
- break;
1264
- case T_HASH:
1265
- opts = argv[0];
1266
- break;
1267
- default:
1268
- rb_raise(rb_eArgError, "Wrong argument %"PRIsVALUE", expected a String or Hash", rb_obj_class(argv[0]));
1269
- }
1270
- }
1271
-
1272
- if (argc > 1) {
1273
- if (TYPE(argv[1]) == T_HASH) {
1274
- opts = argv[1];
1275
- } else {
1276
- rb_raise(rb_eArgError, "Expected a Hash, but got %"PRIsVALUE, rb_obj_class(argv[1]));
1277
- }
1278
- }
1279
-
1280
- if (!NIL_P(opts)) {
1281
- #define PR_OPT(field) { \
1282
- val = rb_hash_aref(opts, ID2SYM(rb_intern(#field))); \
1283
- if (!NIL_P(val) && val) { \
1284
- options.field = 1; \
1285
- with_options = 1; \
1286
- } \
1287
- }
1288
- PR_OPT(no_select_locked_cells);
1289
- PR_OPT(no_select_unlocked_cells);
1290
- PR_OPT(format_cells);
1291
- PR_OPT(format_columns);
1292
- PR_OPT(format_rows);
1293
- PR_OPT(insert_columns);
1294
- PR_OPT(insert_rows);
1295
- PR_OPT(insert_hyperlinks);
1296
- PR_OPT(delete_columns);
1297
- PR_OPT(delete_rows);
1298
- PR_OPT(sort);
1299
- PR_OPT(autofilter);
1300
- PR_OPT(pivot_tables);
1301
- PR_OPT(scenarios);
1302
- PR_OPT(objects);
1303
- #undef PR_OPT
1304
- }
1305
- struct worksheet *ptr;
1306
- Data_Get_Struct(self, struct worksheet, ptr);
1307
- if (with_options) {
1308
- worksheet_protect(ptr->worksheet, password, &options);
1309
- } else {
1310
- worksheet_protect(ptr->worksheet, password, NULL);
1311
- }
1155
+ struct _protect_options po = _extract_protect_options(argc, argv);
1156
+ LXW_NO_RESULT_CALL(worksheet, protect, po.password, (po.with_options ? &po.options : NULL));
1312
1157
  return self;
1313
1158
  }
1314
1159
 
@@ -1318,8 +1163,6 @@ worksheet_protect_(int argc, VALUE *argv, VALUE self) {
1318
1163
  */
1319
1164
  VALUE
1320
1165
  worksheet_outline_settings_(VALUE self, VALUE opts) {
1321
- struct worksheet *ptr;
1322
- Data_Get_Struct(self, struct worksheet, ptr);
1323
1166
  VALUE value;
1324
1167
  #define parse_param(name, default) \
1325
1168
  value = rb_hash_aref(opts, ID2SYM(rb_intern(#name))); \
@@ -1329,7 +1172,7 @@ worksheet_outline_settings_(VALUE self, VALUE opts) {
1329
1172
  parse_param(symbols_right, LXW_TRUE);
1330
1173
  parse_param(auto_style, LXW_FALSE);
1331
1174
  #undef parse_param
1332
- worksheet_outline_settings(ptr->worksheet, visible, symbols_below, symbols_right, auto_style);
1175
+ LXW_NO_RESULT_CALL(worksheet, outline_settings, visible, symbols_below, symbols_right, auto_style);
1333
1176
  return self;
1334
1177
  }
1335
1178
 
@@ -1339,13 +1182,34 @@ worksheet_outline_settings_(VALUE self, VALUE opts) {
1339
1182
  */
1340
1183
  VALUE
1341
1184
  worksheet_set_default_row_(VALUE self, VALUE height, VALUE hide_unused_rows) {
1342
- struct worksheet *ptr;
1343
1185
  uint8_t hide_ur = !NIL_P(hide_unused_rows) && hide_unused_rows != Qfalse ? 1 : 0;
1344
- Data_Get_Struct(self, struct worksheet, ptr);
1345
- worksheet_set_default_row(ptr->worksheet, NUM2DBL(height), hide_ur);
1186
+ LXW_NO_RESULT_CALL(worksheet, set_default_row, NUM2DBL(height), hide_ur);
1346
1187
  return self;
1347
1188
  }
1348
1189
 
1190
+ /* call-seq: ws.horizontal_dpi -> int
1191
+ *
1192
+ * Returns the horizontal dpi.
1193
+ */
1194
+ VALUE
1195
+ worksheet_get_horizontal_dpi_(VALUE self) {
1196
+ struct worksheet *ptr;
1197
+ Data_Get_Struct(self, struct worksheet, ptr);
1198
+ return INT2NUM(ptr->worksheet->horizontal_dpi);
1199
+ }
1200
+
1201
+ /* call-seq: ws.horizontal_dpi=(dpi) -> dpi
1202
+ *
1203
+ * Sets the horizontal dpi.
1204
+ */
1205
+ VALUE
1206
+ worksheet_set_horizontal_dpi_(VALUE self, VALUE val) {
1207
+ struct worksheet *ptr;
1208
+ Data_Get_Struct(self, struct worksheet, ptr);
1209
+ ptr->worksheet->horizontal_dpi = NUM2INT(val);
1210
+ return val;
1211
+ }
1212
+
1349
1213
  /* call-seq: ws.vertical_dpi -> int
1350
1214
  *
1351
1215
  * Returns the vertical dpi.
@@ -1538,12 +1402,7 @@ worksheet_data_validation_(int argc, VALUE *argv, VALUE self) {
1538
1402
  */
1539
1403
  VALUE
1540
1404
  worksheet_set_vba_name_(VALUE self, VALUE name) {
1541
- struct worksheet *ptr;
1542
- Data_Get_Struct(self, struct worksheet, ptr);
1543
-
1544
- lxw_error err = worksheet_set_vba_name(ptr->worksheet, StringValueCStr(name));
1545
- handle_lxw_error(err);
1546
-
1405
+ LXW_ERR_RESULT_CALL(worksheet, set_vba_name, StringValueCStr(name));
1547
1406
  return name;
1548
1407
  }
1549
1408
 
@@ -1707,8 +1566,8 @@ init_xlsxwriter_worksheet() {
1707
1566
  rb_define_method(cWorksheet, "set_page_view", worksheet_set_page_view_, 0);
1708
1567
  rb_define_method(cWorksheet, "paper=", worksheet_set_paper_, 1);
1709
1568
  rb_define_method(cWorksheet, "set_margins", worksheet_set_margins_, 4);
1710
- rb_define_method(cWorksheet, "set_header", worksheet_set_header_, 1);
1711
- rb_define_method(cWorksheet, "set_footer", worksheet_set_footer_, 1);
1569
+ rb_define_method(cWorksheet, "set_header", worksheet_set_header_, -1);
1570
+ rb_define_method(cWorksheet, "set_footer", worksheet_set_footer_, -1);
1712
1571
  rb_define_method(cWorksheet, "h_pagebreaks=", worksheet_set_h_pagebreaks_, 1);
1713
1572
  rb_define_method(cWorksheet, "v_pagebreaks=", worksheet_set_v_pagebreaks_, 1);
1714
1573
  rb_define_method(cWorksheet, "print_across", worksheet_print_across_, 0);
@@ -1730,6 +1589,8 @@ init_xlsxwriter_worksheet() {
1730
1589
  rb_define_method(cWorksheet, "outline_settings=", worksheet_outline_settings_, 1);
1731
1590
  rb_define_method(cWorksheet, "set_default_row", worksheet_set_default_row_, 2);
1732
1591
 
1592
+ rb_define_method(cWorksheet, "horizontal_dpi", worksheet_get_horizontal_dpi_, 0);
1593
+ rb_define_method(cWorksheet, "horizontal_dpi=", worksheet_set_horizontal_dpi_, 1);
1733
1594
  rb_define_method(cWorksheet, "vertical_dpi", worksheet_get_vertical_dpi_, 0);
1734
1595
  rb_define_method(cWorksheet, "vertical_dpi=", worksheet_set_vertical_dpi_, 1);
1735
1596