Class: XlsxWriter::Workbook

Inherits:
Object
  • Object
show all
Defined in:
ext/xlsxwriter/xlsxwriter.c

Defined Under Namespace

Classes: Chart, Properties

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/xlsxwriter/workbook.c', line 33

VALUE
workbook_init(int argc, VALUE *argv, VALUE self) {
  struct workbook *ptr;
  lxw_workbook_options options = {
    .constant_memory = 0,
    .tmpdir = NULL
  };

  if (argc < 1 || argc > 2) {
    rb_raise(rb_eArgError, "wrong number of arguments");
    return self;
  } else if (argc == 2) {
    VALUE const_mem = rb_hash_aref(argv[1], ID2SYM(rb_intern("constant_memory")));
    if (!NIL_P(const_mem) && const_mem) {
      options.constant_memory = 1;
      VALUE tmpdir = rb_hash_aref(argv[1], ID2SYM(rb_intern("tmpdir")));
      if (!NIL_P(tmpdir))
        options.tmpdir = RSTRING_PTR(tmpdir);
    }
  }

  Data_Get_Struct(self, struct workbook, ptr);

  size_t len = RSTRING_LEN(argv[0]);
  ptr->path = malloc(len + 1);
  strncpy(ptr->path, RSTRING_PTR(argv[0]), len + 1);
  if (options.constant_memory) {
    ptr->workbook = workbook_new_opt(ptr->path, &options);
  } else {
    ptr->workbook = workbook_new(ptr->path);
  }
  ptr->properties = NULL;
  rb_iv_set(self, "@font_sizes", rb_hash_new());

  return self;
}

Instance Attribute Details

#font_sizesObject (readonly)

Class Method Details

.new(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'ext/xlsxwriter/workbook.c', line 22

VALUE
workbook_new_(int argc, VALUE *argv, VALUE self) {
  VALUE workbook = rb_call_super(argc, argv);
  if (rb_block_given_p()) {
    rb_yield(workbook);
    workbook_release(workbook);
    return Qnil;
  }
  return workbook;
}

Instance Method Details

#add_chart(type) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'ext/xlsxwriter/workbook.c', line 174

VALUE
workbook_add_chart_(VALUE self, VALUE type) {
  VALUE chart = rb_funcall(cChart, rb_intern("new"), 2, self, type);
  if (rb_block_given_p()) {
    VALUE res = rb_yield(chart);
    return res;
  }
  return chart;
}

#add_format(key, opts) ⇒ Object



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
# File 'ext/xlsxwriter/workbook.c', line 147

VALUE
workbook_add_format_(VALUE self, VALUE key, VALUE opts) {
  struct workbook *ptr;
  lxw_format *format;
  Data_Get_Struct(self, struct workbook, ptr);

  if (!ptr->formats) {
    ptr->formats = st_init_numtable();
  }

  format = workbook_add_format(ptr->workbook);
  st_insert(ptr->formats, rb_to_id(key), (st_data_t)format);
  format_apply_opts(format, opts);

  VALUE font_size = rb_hash_aref(opts, ID2SYM(rb_intern("font_size")));
  if (!NIL_P(font_size)) {
    VALUE bold = rb_hash_aref(opts, ID2SYM(rb_intern("bold")));
    if (!NIL_P(bold) && bold) {
      rb_hash_aset(rb_iv_get(self, "@font_sizes"), key, rb_float_new(NUM2DBL(font_size) * 1.5));
    } else {
      rb_hash_aset(rb_iv_get(self, "@font_sizes"), key, font_size);
    }
  }

  return self;
}

#add_worksheet(*args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/xlsxwriter/workbook.c', line 122

VALUE
workbook_add_worksheet_(int argc, VALUE *argv, VALUE self) {
  VALUE worksheet = Qnil;

  if (argc > 1) {
    rb_raise(rb_eArgError, "wrong number of arguments");
    return Qnil;
  }

  struct workbook *ptr;
  Data_Get_Struct(self, struct workbook, ptr);
  if (ptr->workbook) {
    VALUE mXlsxWriter = rb_const_get(rb_cObject, rb_intern("XlsxWriter"));
    VALUE cWorksheet = rb_const_get(mXlsxWriter, rb_intern("Worksheet"));
    worksheet = rb_funcall(cWorksheet, rb_intern("new"), argc + 1, self, argv[0]);
  }

  if (rb_block_given_p()) {
    VALUE res = rb_yield(worksheet);
    return res;
  }

  return worksheet;
}

#closeObject



70
71
72
73
74
75
76
77
# File 'ext/xlsxwriter/workbook.c', line 70

VALUE
workbook_release(VALUE self) {
  struct workbook *ptr;
  Data_Get_Struct(self, struct workbook, ptr);

  workbook_free(ptr);
  return self;
}

#define_name(name, formula) ⇒ Object



199
200
201
202
203
204
205
# File 'ext/xlsxwriter/workbook.c', line 199

VALUE
workbook_define_name_(VALUE self, VALUE name, VALUE formula) {
  struct workbook *ptr;
  Data_Get_Struct(self, struct workbook, ptr);
  workbook_define_name(ptr->workbook, StringValueCStr(name), StringValueCStr(formula));
  return self;
}

#propertiesObject



192
193
194
195
196
197
# File 'ext/xlsxwriter/workbook.c', line 192

VALUE
workbook_properties_(VALUE self) {
  VALUE props = rb_obj_alloc(cWorkbookProperties);
  rb_obj_call_init(props, 1, &self);
  return props;
}

#set_default_xf_indicesObject



184
185
186
187
188
189
190
# File 'ext/xlsxwriter/workbook.c', line 184

VALUE
workbook_set_default_xf_indices_(VALUE self) {
  struct workbook *ptr;
  Data_Get_Struct(self, struct workbook, ptr);
  lxw_workbook_set_default_xf_indices(ptr->workbook);
  return self;
}

#validate_worksheet_name(name) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'ext/xlsxwriter/workbook.c', line 207

VALUE
workbook_validate_worksheet_name_(VALUE self, VALUE name) {
  struct workbook *ptr;
  lxw_error err;
  Data_Get_Struct(self, struct workbook, ptr);
  err = workbook_validate_worksheet_name(ptr->workbook, StringValueCStr(name));
  handle_lxw_error(err);
  return Qtrue;
}