Class: XlsxWriter::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxwriter/worksheet.rb,
ext/xlsxwriter/worksheet.c

Constant Summary collapse

THIN_CHARS =

Thiner characters list used for column width logic mimicking axlsx behaviour

'^.acfijklrstxzFIJL()-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

:nodoc:



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

VALUE
worksheet_init(int argc, VALUE *argv, VALUE self) {
  char *name = NULL;
  VALUE opts = Qnil;
  VALUE auto_width = Qtrue;
  struct workbook *wb_ptr;
  struct worksheet *ptr;

  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);

  rb_check_arity(argc, 1, 2);
  if (argc == 2) {
    switch (TYPE(argv[1])) {
    case T_HASH:
      opts = argv[1];
      break;
    case T_STRING:
    case T_SYMBOL:
      name = StringValueCStr(argv[1]);
      break;
    case T_NIL:
      break;
    default:
      rb_raise(rb_eArgError, "wrong type of name");
      break;
    }
  }

  if (!NIL_P(opts)) {
    VALUE val = rb_hash_aref(opts, ID2SYM(rb_intern("auto_width")));
    if (val == Qfalse) {
      auto_width = Qfalse;
    }
    val = rb_hash_aref(opts, ID2SYM(rb_intern("name")));
    if (!NIL_P(val) && !name) {
      name = StringValueCStr(val);
    }
  }

  rb_iv_set(self, "@workbook", argv[0]);
  rb_iv_set(self, "@use_auto_width", auto_width);
  rb_iv_set(self, "@col_auto_widths", rb_ary_new());

  TypedData_Get_Struct(argv[0], struct workbook, &workbook_type, wb_ptr);
  ptr->worksheet = workbook_add_worksheet(wb_ptr->workbook, name);
  if (!ptr->worksheet)
    rb_raise(eXlsxWriterError, "worksheet was not created");
  return self;
}

Instance Attribute Details

#col_auto_widthsObject (readonly)

Last row number written with #add_row



6
7
8
# File 'lib/xlsxwriter/worksheet.rb', line 6

def col_auto_widths
  @col_auto_widths
end

#current_rowObject (readonly)

Last row number written with #add_row



6
7
8
# File 'lib/xlsxwriter/worksheet.rb', line 6

def current_row
  @current_row
end

#workbookObject (readonly)

Instance Method Details

#activateself

Set the worksheet to be active on opening the workbook.

Returns:

  • (self)


789
790
791
792
793
# File 'ext/xlsxwriter/worksheet.c', line 789

VALUE
worksheet_activate_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, activate);
  return self;
}

#add_data_validation(cell, options) ⇒ self #add_data_validation(range, options) ⇒ self #add_data_validation(row, col, options) ⇒ self #add_data_validation(row_from, col_from, row_to, col_to, options) ⇒ self

Adds data validation or limits user input to a list of values.

Overloads:

  • #add_data_validation(cell, options) ⇒ self

    Returns:

    • (self)
  • #add_data_validation(range, options) ⇒ self

    Returns:

    • (self)
  • #add_data_validation(row, col, options) ⇒ self

    Returns:

    • (self)
  • #add_data_validation(row_from, col_from, row_to, col_to, options) ⇒ self

    Returns:

    • (self)


1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# File 'ext/xlsxwriter/worksheet.c', line 1267

VALUE
worksheet_data_validation_(int argc, VALUE *argv, VALUE self) {
  lxw_data_validation data_validation = {0};
  rb_check_arity(argc, 2, 5);
  char range = 0;
  if (argc > 3) {
    range = 1;
  } else if (TYPE(argv[0]) == T_STRING) {
    char *str = RSTRING_PTR(argv[0]);
    if (strstr(str, ":")) {
      range = 1;
    } else if ((str[0] >= 'A' && str[0] <= 'Z') ||
               (str[0] >= 'a' && str[0] <= 'z'))
      range = argc > 2;
  }

  lxw_row_t row, row2;
  lxw_col_t col, col2;
  int larg;
  if (range) {
    larg = extract_range(argc, argv, &row, &col, &row2, &col2);
  } else {
    larg = extract_cell(argc, argv, &row, &col);
  }

  if (larg != argc - 1) {
    rb_raise(rb_eArgError, "Wrong number of arguments");
  }
  VALUE opts = argv[larg];
  if (TYPE(opts) != T_HASH) {
    rb_raise(rb_eTypeError, "Wrong type for options %"PRIsVALUE", Hash expected", rb_obj_class(opts));
  }

  VALUE val;
  val = rb_hash_aref(opts, ID2SYM(rb_intern("validate")));
  if (!NIL_P(val)) {
    data_validation.validate = NUM2INT(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("criteria")));
  if (!NIL_P(val)) {
    data_validation.criteria = NUM2INT(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("ignore_blank")));
  if (!NIL_P(val) && val) {
    data_validation.ignore_blank = LXW_VALIDATION_ON;
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("show_input")));
  if (!NIL_P(val) && val) {
    data_validation.show_input = LXW_VALIDATION_ON;
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("show_error")));
  if (!NIL_P(val) && val) {
    data_validation.show_error = LXW_VALIDATION_ON;
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("error_type")));
  if (!NIL_P(val)) {
    data_validation.error_type = NUM2INT(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("dropdown")));
  if (!NIL_P(val) && val) {
    data_validation.dropdown = LXW_VALIDATION_ON;
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("input_title")));
  if (!NIL_P(val)) {
    data_validation.input_title = RSTRING_PTR(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("input_message")));
  if (!NIL_P(val)) {
    data_validation.input_message = RSTRING_PTR(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("error_title")));
  if (!NIL_P(val)) {
    data_validation.error_title = RSTRING_PTR(val);
  }

  val = rb_hash_aref(opts, ID2SYM(rb_intern("error_message")));
  if (!NIL_P(val)) {
    data_validation.error_message = RSTRING_PTR(val);
  }

  VALUE is_datetime;
#define parse_array_0(x)
#define parse_array_1(prefix)                                           \
    if (TYPE(val) == T_ARRAY) {                                         \
      int len = RARRAY_LEN(val);                                        \
      int i;                                                            \
      data_validation.prefix##_list = malloc(sizeof(char *) * (len + 1)); \
      data_validation.prefix##_list[len] = NULL;                        \
      for (i = 0; i < len; ++i) {                                       \
        data_validation.prefix##_list[i] = RSTRING_PTR(rb_ary_entry(val, i)); \
      }                                                                 \
    } else

#define parse_value(prefix, key, handle_array)                          \
  val = rb_hash_aref(opts, ID2SYM(rb_intern(key)));                     \
  if (!NIL_P(val)) {                                                    \
    switch(TYPE(val)) {                                                 \
    case T_FLOAT: case T_FIXNUM: case T_BIGNUM:                         \
      data_validation.prefix##_number = NUM2DBL(val);                   \
      break;                                                            \
    case T_STRING:                                                      \
      data_validation.prefix##_formula = RSTRING_PTR(val);              \
      if (data_validation.prefix##_formula[0] != '=') {                 \
        data_validation.prefix##_formula = 0;                           \
        rb_raise(rb_eArgError, "Is not a formula: %"PRIsVALUE, val);    \
      }                                                                 \
      break;                                                            \
    default:                                                            \
      is_datetime = rb_funcall(val, rb_intern("is_a?"), 1, rb_cTime);   \
      parse_array_##handle_array(prefix)                                \
      if (is_datetime || rb_respond_to(val, rb_intern("to_time"))) {    \
        if (!is_datetime)                                               \
          val = rb_funcall(val, rb_intern("to_time"), 0);               \
        data_validation.prefix##_datetime = value_to_lxw_datetime(val); \
      } else {                                                          \
        rb_raise(rb_eTypeError, "Cannot handle " key " type %"PRIsVALUE, rb_class_of(val)); \
      }                                                                 \
    }                                                                   \
  }

  parse_value(minimum, "min", 0);
  parse_value(maximum, "max", 0);
  parse_value(value, "value", 1);

#undef parse_array_0
#undef parse_array_1
#undef parse_value

  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  lxw_error err;
  if (range) {
    err = worksheet_data_validation_range(ptr->worksheet, row, col, row2, col2, &data_validation);
  } else {
    err = worksheet_data_validation_cell(ptr->worksheet, row, col, &data_validation);
  }

  if (data_validation.value_list) {
    free(data_validation.value_list);
  }

  handle_lxw_error(err);

  return self;
}

#add_row(row, style: nil, height: nil, types: nil, skip_empty: false) ⇒ Object

Write a row. If no types passed XlsxWriter tries to deduce them automatically.

Both types and style may be an array as well as a symbol. In the latter case they are applied to all cells in the row.

height is a Numeric that specifies the row height.

If skip_empty is set to true empty cells are not added to the sheet. Otherwise they are added with type :blank.



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
# File 'lib/xlsxwriter/worksheet.rb', line 20

def add_row(row, style: nil, height: nil, types: nil, skip_empty: false)
  row_idx = @current_row ||= 0
  @current_row += 1

  style_ary = style if style.is_a?(Array)
  types_ary = types if types.is_a?(Array)

  row.each_with_index do |value, idx|
    cell_style = style_ary ? style_ary[idx] : style
    cell_type  = types_ary ? types_ary[idx] : types

    case cell_type && cell_type.to_sym
    when :string
      value = value.to_s
      write_string(row_idx, idx, value, cell_style)
      update_col_auto_width(idx, value, cell_style)
    when :rich_string
      write_rich_string(row_idx, idx, value, cell_style)
    when :number
      write_number(row_idx, idx, value.to_f, cell_style)
    when :formula
      write_formula(row_idx, idx, value, cell_style)
    when :datetime, :date, :time
      write_datetime(row_idx, idx, value.to_time, cell_style)
    when :url
      write_url(row_idx, idx, value, cell_style)
      update_col_auto_width(idx, value.to_s, cell_style)
    when :boolean
      write_boolean(row_idx, idx, value, cell_style)
    when :blank
      write_blank(row_idx, idx, cell_style)
    when :skip, :empty
      # write nothing
      nil
    when nil
      case value
      when Numeric
        write_number(row_idx, idx, value, cell_style)
      when TrueClass, FalseClass
        write_boolean(row_idx, idx, value, cell_style)
      when Time, (defined?(Date) ? Date : Time)
        write_datetime(row_idx, idx, value, cell_style)
      when '', nil
        write_blank(row_idx, idx, cell_style) unless skip_empty
      when /\A=/
        write_formula(row_idx, idx, value, cell_style)
      when String
        write_string(row_idx, idx, value, cell_style)
        update_col_auto_width(idx, value, cell_style)
      when RichString
        write_rich_string(row_idx, idx, value, cell_style)
      else # assume string
        value = value.to_s
        write_string(row_idx, idx, value, cell_style)
        update_col_auto_width(idx, value, cell_style)
      end
    else
      raise ArgumentError, "Unknown cell type #{cell_type}."
    end
  end

  set_row(row_idx, height: height) if height

  nil
end

#apply_auto_widthsObject

Apply cols automatic widths calculated by #add_row.



87
88
89
90
91
# File 'lib/xlsxwriter/worksheet.rb', line 87

def apply_auto_widths
  @col_auto_widths.each_with_index do |width, idx|
    set_column(idx, idx, width: width) if width
  end
end

#autofilter(range) ⇒ self #autofilter(cell_from, cell_to) ⇒ self #autofilter(row_from, col_from, row_to, col_to) ⇒ self

Applies autofilter to the range.

ws.autofilter('A1:D5')
ws.autofilter('A1', 'D5')
ws.autofilter(0, 0, 4, 3)

Overloads:

  • #autofilter(range) ⇒ self

    Returns:

    • (self)
  • #autofilter(cell_from, cell_to) ⇒ self

    Returns:

    • (self)
  • #autofilter(row_from, col_from, row_to, col_to) ⇒ self

    Returns:

    • (self)


773
774
775
776
777
778
779
780
781
782
783
# File 'ext/xlsxwriter/worksheet.c', line 773

VALUE
worksheet_autofilter_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row_from, row_to;
  lxw_col_t col_from, col_to;

  rb_check_arity(argc, 1, 4);
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);

  LXW_NO_RESULT_CALL(worksheet, autofilter, row_from, col_from, row_to, col_to);
  return self;
}

#center_horizontallyObject

Center the worksheet data horizontally between the margins on the printed page



1053
1054
1055
1056
1057
# File 'ext/xlsxwriter/worksheet.c', line 1053

VALUE
worksheet_center_horizontally_(VALUE self){
  LXW_NO_RESULT_CALL(worksheet, center_horizontally);
  return self;
}

#center_verticallyObject

Center the worksheet data vertically between the margins on the printed page



1060
1061
1062
1063
1064
# File 'ext/xlsxwriter/worksheet.c', line 1060

VALUE
worksheet_center_vertically_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, center_vertically);
  return self;
}

#fit_to_pages(width, height) ⇒ self

Fits the printed area to a specific number of pages both vertically and horizontally.

Returns:

  • (self)


1117
1118
1119
1120
1121
# File 'ext/xlsxwriter/worksheet.c', line 1117

VALUE
worksheet_fit_to_pages_(VALUE self, VALUE width, VALUE height) {
  LXW_NO_RESULT_CALL(worksheet, fit_to_pages, NUM2INT(width), NUM2INT(height));
  return self;
}

#freeObject

:nodoc:



94
95
96
97
98
99
100
101
# File 'ext/xlsxwriter/worksheet.c', line 94

VALUE
worksheet_release(VALUE self) {
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);

  worksheet_clear(ptr);
  return self;
}

#freeze_panes(cell) ⇒ self #freeze_panes(row, col) ⇒ self

Divides the worksheet into horizontal or vertical panes and freezes them.

The specified cell is the top left in the right bottom pane.

In order to freeze only rows/cols pass 0 as row or col.

Advanced usage (with 2nd cell and type) is not documented yet.

Overloads:

  • #freeze_panes(cell) ⇒ self

    Returns:

    • (self)
  • #freeze_panes(row, col) ⇒ self

    Returns:

    • (self)


838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'ext/xlsxwriter/worksheet.c', line 838

VALUE
worksheet_freeze_panes_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row, row2;
  lxw_col_t col, col2;
  uint8_t type = 0;
  rb_check_arity(argc, 1, 5);
  int larg = extract_cell(argc, argv, &row, &col);
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  if (larg >= argc) {
    worksheet_freeze_panes(ptr->worksheet, row, col);
  } else {
    larg += extract_cell(argc - larg, argv + larg, &row2, &col2);
    if (larg < argc) {
      type = NUM2INT(argv[larg]);
    }
    worksheet_freeze_panes_opt(ptr->worksheet, row, col, row2, col2, type);
  }
  return self;
}

#get_column_auto_width(col) ⇒ Object



93
94
95
# File 'lib/xlsxwriter/worksheet.rb', line 93

def get_column_auto_width(col)
  @col_auto_widths[extract_column(col)]
end

#gridlines=(option) ⇒ Object

Display or hide screen and print gridlines using one of the values XlsxWriter::Worksheet::GRIDLINES_SHOW_SCREEN, GRIDLINES_SHOW_PRINT, GRIDLINES_SHOW_ALL.



1046
1047
1048
1049
1050
# File 'ext/xlsxwriter/worksheet.c', line 1046

VALUE
worksheet_gridlines_(VALUE self, VALUE value) {
  LXW_NO_RESULT_CALL(worksheet, gridlines, NUM2INT(value));
  return value;
}

#h_pagebreaks=(breaks) ⇒ Object

Adds horizontal page breaks to the worksheet.

wb.h_pagebreaks = [20, 40, 80]


991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'ext/xlsxwriter/worksheet.c', line 991

VALUE
worksheet_set_h_pagebreaks_(VALUE self, VALUE val) {
  const size_t len = RARRAY_LEN(val);
  lxw_row_t rows[len+1];
  size_t i;
  for (i = 0; i<len; ++i) {
    rows[i] = NUM2INT(rb_ary_entry(val, i));
  }
  rows[len] = 0;
  LXW_NO_RESULT_CALL(worksheet, set_h_pagebreaks, rows);
  return val;
}

#hideself

Hide the worksheet.

Returns:

  • (self)


809
810
811
812
813
# File 'ext/xlsxwriter/worksheet.c', line 809

VALUE
worksheet_hide_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, hide);
  return self;
}

#hide_zeroObject

Hides all zero values.



1151
1152
1153
1154
1155
# File 'ext/xlsxwriter/worksheet.c', line 1151

VALUE
worksheet_hide_zero_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, hide_zero);
  return self;
}

#horizontal_dpiInteger

Returns the horizontal dpi.

Returns:

  • (Integer)


1217
1218
1219
1220
1221
1222
# File 'ext/xlsxwriter/worksheet.c', line 1217

VALUE
worksheet_get_horizontal_dpi_(VALUE self) {
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  return INT2NUM(ptr->worksheet->horizontal_dpi);
}

#horizontal_dpi=(dpi) ⇒ Object

Sets the horizontal dpi.



1228
1229
1230
1231
1232
1233
1234
# File 'ext/xlsxwriter/worksheet.c', line 1228

VALUE
worksheet_set_horizontal_dpi_(VALUE self, VALUE val) {
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  ptr->worksheet->horizontal_dpi = NUM2INT(val);
  return val;
}

#insert_chart(cell, chart, opts = {}) ⇒ self #insert_chart(row, col, chart, opts = {}) ⇒ self

Inserts chart from fname into the worksheet (at cell).

For available opts see #insert_image.

Overloads:

  • #insert_chart(cell, chart, opts = {}) ⇒ self

    Returns:

    • (self)
  • #insert_chart(row, col, chart, opts = {}) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_insert_chart_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE chart, opts = Qnil;
  lxw_chart_options options;
  char with_options = '\0';

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    chart = argv[larg];
    ++larg;
  } else {
    rb_raise(rb_eArgError, "No chart specified");
  }

  if (larg < argc) {
    opts = argv[larg];
    ++larg;
  }

  if (!NIL_P(opts)) {
    options = val_to_lxw_chart_options(opts, &with_options);
  }

  struct worksheet *ptr;
  struct chart *chart_ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  TypedData_Get_Struct(chart, struct chart, &chart_type, chart_ptr);

  if (with_options) {
    worksheet_insert_chart_opt(ptr->worksheet, row, col, chart_ptr->chart, &options);
  } else {
    worksheet_insert_chart(ptr->worksheet, row, col, chart_ptr->chart);
  }

  return self;
}

#insert_image(cell, fname, opts = {}) ⇒ self #insert_image(row, col, fname, opts = {}) ⇒ self

Inserts image from fname into the worksheet (at cell).

Available opts

:offset, :x_offset, :y_offset

Image offset (:offset for both; Integer).

:scale, :x_scale, :y_scale

Image scale (:scale for both; Numeric).

Overloads:

  • #insert_image(cell, fname, opts = {}) ⇒ self

    Returns:

    • (self)
  • #insert_image(row, col, fname, opts = {}) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_insert_image_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE fname = Qnil;
  VALUE opts = Qnil;
  lxw_image_options options;
  char with_options = '\0';

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    fname = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    opts = argv[larg];
    ++larg;
  }

  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);

  if (!NIL_P(opts)) {
    options = val_to_lxw_image_options(opts, &with_options);
  }

  if (with_options) {
    worksheet_insert_image_opt(ptr->worksheet, row, col, StringValueCStr(fname), &options);
  } else {
    worksheet_insert_image(ptr->worksheet, row, col, StringValueCStr(fname));
  }

  return self;
}

#insert_image_buffer(cell, data, options) ⇒ self #insert_image_buffer(row, col, data, options) ⇒ self

Adds data validation or limits user input to a list of values.

Overloads:

  • #insert_image_buffer(cell, data, options) ⇒ self

    Returns:

    • (self)
  • #insert_image_buffer(row, col, data, options) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_insert_image_buffer_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE data = Qnil;
  VALUE opts = Qnil;
  lxw_image_options options;
  char with_options = '\0';

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    data = argv[larg];
    ++larg;
  } else {
    rb_raise(rb_eArgError, "Cannot embed image without data");
  }

  if (larg < argc) {
    opts = argv[larg];
    ++larg;
  }
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);

  if (!NIL_P(opts)) {
    options = val_to_lxw_image_options(opts, &with_options);
  }

  lxw_error error;
  if (with_options) {
    error = worksheet_insert_image_buffer_opt(ptr->worksheet, row, col, (const unsigned char *) RSTRING_PTR(data), RSTRING_LEN(data), &options);
  } else {
    error = worksheet_insert_image_buffer(ptr->worksheet, row, col, (const unsigned char *) RSTRING_PTR(data), RSTRING_LEN(data));
  }
  handle_lxw_error(error);

  return self;
}

#merge_range(range, value = "", format = nil) ⇒ self #merge_range(cell_from, cell_to, value = "", format = nil) ⇒ self #merge_range(row_from, col_from, row_to, col_to, value = "", format = nil) ⇒ self

Merges range, setting string value with format.

ws.merge_range('A1:D5')
ws.merge_range('A1', 'D5', 'Merged cells')
ws.merge_range(0, 0, 4, 3)

Overloads:

  • #merge_range(range, value = "", format = nil) ⇒ self

    Returns:

    • (self)
  • #merge_range(cell_from, cell_to, value = "", format = nil) ⇒ self

    Returns:

    • (self)
  • #merge_range(row_from, col_from, row_to, col_to, value = "", format = nil) ⇒ self

    Returns:

    • (self)


737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'ext/xlsxwriter/worksheet.c', line 737

VALUE
worksheet_merge_range_(int argc, VALUE *argv, VALUE self) {
  char *str = "";
  lxw_format *format = NULL;
  lxw_col_t col1, col2;
  lxw_row_t row1, row2;
  VALUE workbook = rb_iv_get(self, "@workbook");

  rb_check_arity(argc, 1, 6);
  int larg = extract_range(argc, argv, &row1, &col1, &row2, &col2);

  if (larg < argc) {
    str = StringValueCStr(argv[larg]);
    ++larg;
  }

  if (larg < argc) {
    format = workbook_get_format(workbook, argv[larg]);
    ++larg;
  }

  LXW_NO_RESULT_CALL(worksheet, merge_range, row1, col1, row2, col2, str, format);
  return self;
}

#outline_settings=(opts) ⇒ Object

Sets the Outline and Grouping display properties.



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'ext/xlsxwriter/worksheet.c', line 1187

VALUE
worksheet_outline_settings_(VALUE self, VALUE opts) {
  VALUE value;
#define parse_param(name, default)                                      \
  value = rb_hash_aref(opts, ID2SYM(rb_intern(#name)));                 \
  uint8_t name = NIL_P(value) ? default : (value ? LXW_TRUE : LXW_FALSE)
  parse_param(visible, LXW_TRUE);
  parse_param(symbols_below, LXW_TRUE);
  parse_param(symbols_right, LXW_TRUE);
  parse_param(auto_style, LXW_FALSE);
#undef parse_param
  LXW_NO_RESULT_CALL(worksheet, outline_settings, visible, symbols_below, symbols_right, auto_style);
  return self;
}

#paper=(type) ⇒ Object

Sets the paper type for printing. See libxlsxwriter doc for options.



929
930
931
932
933
# File 'ext/xlsxwriter/worksheet.c', line 929

VALUE
worksheet_set_paper_(VALUE self, VALUE paper_type) {
  LXW_NO_RESULT_CALL(worksheet, set_paper, NUM2INT(paper_type));
  return self;
}

Changes the default print direction



1024
1025
1026
1027
1028
# File 'ext/xlsxwriter/worksheet.c', line 1024

VALUE
worksheet_print_across_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, print_across);
  return self;
}

Specifies area of the worksheet to be printed.



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'ext/xlsxwriter/worksheet.c', line 1100

VALUE
worksheet_print_area_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row_from, row_to;
  lxw_col_t col_from, col_to;

  rb_check_arity(argc, 1, 4);
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);

  LXW_NO_RESULT_CALL(worksheet, print_area, row_from, col_from, row_to, col_to);
  return self;
}

Print rows and column header (wich is disabled by default).



1067
1068
1069
1070
1071
# File 'ext/xlsxwriter/worksheet.c', line 1067

VALUE
worksheet_print_row_col_headers_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, print_row_col_headers);
  return self;
}

Sets the scale factor of the printed page (10 <= scale <= 400).



1137
1138
1139
1140
1141
# File 'ext/xlsxwriter/worksheet.c', line 1137

VALUE
worksheet_set_print_scale_(VALUE self, VALUE scale) {
  LXW_NO_RESULT_CALL(worksheet, set_print_scale, NUM2INT(scale));
  return scale;
}

#protect(password[, opts]) ⇒ self

Protects the worksheet elements from modification. See libxlsxwriter doc for options.

Returns:

  • (self)


1175
1176
1177
1178
1179
1180
1181
# File 'ext/xlsxwriter/worksheet.c', line 1175

VALUE
worksheet_protect_(int argc, VALUE *argv, VALUE self) {
  rb_check_arity(argc, 0, 2);
  struct _protect_options po = _extract_protect_options(argc, argv);
  LXW_NO_RESULT_CALL(worksheet, protect, po.password, (po.with_options ? &po.options : NULL));
  return self;
}

#repeat_columns(col_from, col_to) ⇒ Object

Sets columns to be repeatedly printed out on all pages.



1087
1088
1089
1090
1091
# File 'ext/xlsxwriter/worksheet.c', line 1087

VALUE
worksheet_repeat_columns_(VALUE self, VALUE col_from, VALUE col_to) {
  LXW_NO_RESULT_CALL(worksheet, repeat_columns, value_to_col(col_from), value_to_col(col_to));
  return self;
}

#repeat_rows(row_from, row_to) ⇒ Object

Sets rows to be repeatedly printed out on all pages.



1077
1078
1079
1080
1081
# File 'ext/xlsxwriter/worksheet.c', line 1077

VALUE
worksheet_repeat_rows_(VALUE self, VALUE row_from, VALUE row_to) {
  LXW_NO_RESULT_CALL(worksheet, repeat_rows, NUM2INT(row_from), NUM2INT(row_to));
  return self;
}

#right_to_leftObject

Sets text direction to rtl (e.g. for worksheets on Hebrew or Arabic).



1144
1145
1146
1147
1148
# File 'ext/xlsxwriter/worksheet.c', line 1144

VALUE
worksheet_right_to_left_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, right_to_left);
  return self;
}

#selectself

Set the worksheet to be selected on opening the workbook.

Returns:

  • (self)


799
800
801
802
803
# File 'ext/xlsxwriter/worksheet.c', line 799

VALUE
worksheet_select_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, select);
  return self;
}

#set_column(col_from, col_to, width: nil, format: nil, hide: nil) ⇒ self

Set properties of the cols range.

Returns:

  • (self)


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

VALUE
worksheet_set_column_(VALUE self, VALUE col_from, VALUE col_to, VALUE opts) {
  double width = LXW_DEF_COL_WIDTH;
  lxw_format *format = NULL;
  lxw_row_col_options options = {
    .hidden = 0,
    .collapsed = 0,
    .level = 0
  };
  VALUE val;
  lxw_col_t col1 = value_to_col(col_from);
  lxw_col_t col2 = value_to_col(col_to);
  VALUE workbook = rb_iv_get(self, "@workbook");

  val = rb_hash_aref(opts, ID2SYM(rb_intern("width")));
  if (val != Qnil)
    width = NUM2DBL(val);

  val = rb_hash_aref(opts, ID2SYM(rb_intern("format")));
  format = workbook_get_format(workbook, val);

  val = rb_hash_aref(opts, ID2SYM(rb_intern("hide")));
  if (val != Qnil && val)
    options.hidden = 1;

  val = rb_hash_aref(opts, ID2SYM(rb_intern("collapse")));
  if (val != Qnil && val)
    options.collapsed = 1;

  val = rb_hash_aref(opts, ID2SYM(rb_intern("level")));
  if (val != Qnil)
    options.level = NUM2INT(val);

  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  if (options.hidden || options.collapsed || options.level) {
    worksheet_set_column_opt(ptr->worksheet, col1, col2, width, format, &options);
  } else {
    worksheet_set_column(ptr->worksheet, col1, col2, width, format);
  }
  return self;
}

#set_default_row(height, hide_unuser_rows) ⇒ self

Sets the default row properties for the worksheet.

Returns:

  • (self)


1206
1207
1208
1209
1210
1211
# File 'ext/xlsxwriter/worksheet.c', line 1206

VALUE
worksheet_set_default_row_(VALUE self, VALUE height, VALUE hide_unused_rows) {
  uint8_t hide_ur = !NIL_P(hide_unused_rows) && hide_unused_rows != Qfalse ? 1 : 0;
  LXW_NO_RESULT_CALL(worksheet, set_default_row, NUM2DBL(height), hide_ur);
  return self;
}

#set_fitst_sheetself

Set the sheet to be the first visible in the sheets list (which is placed under the work area in Excel).

Returns:

  • (self)


820
821
822
823
824
# File 'ext/xlsxwriter/worksheet.c', line 820

VALUE
worksheet_set_first_sheet_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, set_first_sheet);
  return self;
}

See #set_header for params description.



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'ext/xlsxwriter/worksheet.c', line 969

VALUE
worksheet_set_footer_(int argc, VALUE *argv, VALUE self) {
  struct worksheet *ptr;
  rb_check_arity(argc, 1, 2);
  struct _header_options ho = _extract_header_options(argc, argv);
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  lxw_error err;
  if (!ho.with_options) {
    err = worksheet_set_footer(ptr->worksheet, ho.str);
  } else {
    err = worksheet_set_footer_opt(ptr->worksheet, ho.str, &ho.options);
  }
  handle_lxw_error(err);
  return self;
}

#set_header(text[, opts]) ⇒ self

See libxlsxwriter doc for the text control characters.

Currently the only supported option is :margin (Numeric).

Returns:

  • (self)


949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'ext/xlsxwriter/worksheet.c', line 949

VALUE
worksheet_set_header_(int argc, VALUE *argv, VALUE self) {
  rb_check_arity(argc, 1, 2);
  struct _header_options ho = _extract_header_options(argc, argv);
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  lxw_error err;
  if (!ho.with_options) {
    err = worksheet_set_header(ptr->worksheet, ho.str);
  } else {
    err = worksheet_set_header_opt(ptr->worksheet, ho.str, &ho.options);
  }
  handle_lxw_error(err);
  return self;
}

#set_landscapeself

Sets print orientation of the worksheet to landscape.

Returns:

  • (self)


899
900
901
902
903
# File 'ext/xlsxwriter/worksheet.c', line 899

VALUE
worksheet_set_landscape_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, set_landscape);
  return self;
}

#set_margins(left, right, top, bottom) ⇒ Object

Sets the worksheet margins (Numeric) for the printed page.



936
937
938
939
940
# File 'ext/xlsxwriter/worksheet.c', line 936

VALUE
worksheet_set_margins_(VALUE self, VALUE left, VALUE right, VALUE top, VALUE bottom) {
  LXW_NO_RESULT_CALL(worksheet, set_margins, NUM2DBL(left), NUM2DBL(right), NUM2DBL(top), NUM2DBL(bottom));
  return self;
}

#set_page_viewself

Sets worksheet displa mode to “Page View/Layout”.

Returns:

  • (self)


919
920
921
922
923
# File 'ext/xlsxwriter/worksheet.c', line 919

VALUE
worksheet_set_page_view_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, set_page_view);
  return self;
}

#set_portraitself

Sets print orientation of the worksheet to portrait.

Returns:

  • (self)


909
910
911
912
913
# File 'ext/xlsxwriter/worksheet.c', line 909

VALUE
worksheet_set_portrait_(VALUE self) {
  LXW_NO_RESULT_CALL(worksheet, set_portrait);
  return self;
}

#set_row(row, height: nil, format: nil, hide: false) ⇒ self

Set properties of the row.

Returns:

  • (self)


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

VALUE
worksheet_set_row_(VALUE self, VALUE row, VALUE opts) {
  double height = LXW_DEF_ROW_HEIGHT;
  lxw_format *format = NULL;
  lxw_row_col_options options = {
    .hidden = 0,
    .collapsed = 0,
    .level = 0
  };
  VALUE val;
  VALUE workbook = rb_iv_get(self, "@workbook");

  val = rb_hash_aref(opts, ID2SYM(rb_intern("height")));
  if (val != Qnil)
    height = NUM2DBL(val);

  val = rb_hash_aref(opts, ID2SYM(rb_intern("format")));
  format = workbook_get_format(workbook, val);

  val = rb_hash_aref(opts, ID2SYM(rb_intern("hide")));
  if (val != Qnil && val)
    options.hidden = 1;

  val = rb_hash_aref(opts, ID2SYM(rb_intern("collapse")));
  if (val != Qnil && val)
    options.collapsed = 1;

  val = rb_hash_aref(opts, ID2SYM(rb_intern("level")));
  if (val != Qnil)
    options.level = NUM2INT(val);

  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  if (options.hidden || options.collapsed || options.level) {
    worksheet_set_row_opt(ptr->worksheet, NUM2INT(row), height, format, &options);
  } else {
    worksheet_set_row(ptr->worksheet, NUM2INT(row), height, format);
  }
  return self;
}

#set_selection(range) ⇒ self #set_selection(cell_from, cell_to) ⇒ self #set_selection(row_from, col_from, row_to, col_to) ⇒ self

Selects the specified range on the worksheet.

ws.set_selection('A1:G5')
ws.set_selection('A1', 'G5')
ws.set_selection(0, 0, 4, 6)

Overloads:

  • #set_selection(range) ⇒ self

    Returns:

    • (self)
  • #set_selection(cell_from, cell_to) ⇒ self

    Returns:

    • (self)
  • #set_selection(row_from, col_from, row_to, col_to) ⇒ self

    Returns:

    • (self)


883
884
885
886
887
888
889
890
891
892
893
# File 'ext/xlsxwriter/worksheet.c', line 883

VALUE
worksheet_set_selection_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row_from, row_to;
  lxw_col_t col_from, col_to;

  rb_check_arity(argc, 1, 4);
  extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);

  LXW_NO_RESULT_CALL(worksheet, set_selection, row_from, col_from, row_to, col_to);
  return self;
}

#split_panes(vertical, horizontal) ⇒ self

Divides the worksheet int vertical and horizontal panes with respective positions from parameters (Numeric).

If only one split is desired pass 0 for other.

Returns:

  • (self)


866
867
868
869
870
# File 'ext/xlsxwriter/worksheet.c', line 866

VALUE
worksheet_split_panes_(VALUE self, VALUE vertical, VALUE horizontal) {
  LXW_NO_RESULT_CALL(worksheet, split_panes, NUM2DBL(vertical), NUM2DBL(horizontal));
  return self;
}

#start_page=(page) ⇒ Object

Set the number of the first printed page.



1127
1128
1129
1130
1131
# File 'ext/xlsxwriter/worksheet.c', line 1127

VALUE
worksheet_set_start_page_(VALUE self, VALUE start_page) {
  LXW_NO_RESULT_CALL(worksheet, set_start_page, NUM2INT(start_page));
  return start_page;
}

#tab_color=(color) ⇒ Object

Set the tab color (from RGB integer).

ws.tab_color = 0xF0F0F0


1163
1164
1165
1166
1167
# File 'ext/xlsxwriter/worksheet.c', line 1163

VALUE
worksheet_set_tab_color_(VALUE self, VALUE color) {
  LXW_NO_RESULT_CALL(worksheet, set_tab_color, NUM2INT(color));
  return color;
}

#h_pagebreaks=(breaks) ⇒ Object

Adds vertical page breaks to the worksheet.

wb.v_pagebreaks = [20, 40, 80]


1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'ext/xlsxwriter/worksheet.c', line 1010

VALUE
worksheet_set_v_pagebreaks_(VALUE self, VALUE val) {
  const size_t len = RARRAY_LEN(val);
  lxw_col_t cols[len+1];
  size_t i;
  for (i = 0; i<len; ++i) {
    cols[i] = value_to_col(rb_ary_entry(val, i));
  }
  cols[len] = 0;
  LXW_NO_RESULT_CALL(worksheet, set_v_pagebreaks, cols);
  return val;
}

#vba_name=(name) ⇒ Object

Set the VBA name for the worksheet.



1426
1427
1428
1429
1430
# File 'ext/xlsxwriter/worksheet.c', line 1426

VALUE
worksheet_set_vba_name_(VALUE self, VALUE name) {
  LXW_ERR_RESULT_CALL(worksheet, set_vba_name, StringValueCStr(name));
  return name;
}

#vertical_dpiInteger

Returns the vertical dpi.

Returns:

  • (Integer)


1240
1241
1242
1243
1244
1245
# File 'ext/xlsxwriter/worksheet.c', line 1240

VALUE
worksheet_get_vertical_dpi_(VALUE self) {
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  return INT2NUM(ptr->worksheet->vertical_dpi);
}

#vertical_dpi=(dpi) ⇒ Object

Sets the vertical dpi.



1251
1252
1253
1254
1255
1256
1257
# File 'ext/xlsxwriter/worksheet.c', line 1251

VALUE
worksheet_set_vertical_dpi_(VALUE self, VALUE val) {
  struct worksheet *ptr;
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  ptr->worksheet->vertical_dpi = NUM2INT(val);
  return val;
}

#write_array_formula(range, formula, format = nil) ⇒ self #write_array_formula(cell_from, cell_to, formula, format = nil) ⇒ self #write_array_formula(row_from, col_from, row_to, col_to, formula, format = nil) ⇒ self

Writes an array formula into a cell range with format.

Overloads:

  • #write_array_formula(range, formula, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_array_formula(cell_from, cell_to, formula, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_array_formula(row_from, col_from, row_to, col_to, formula, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE worksheet_write_array_formula_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row_from, row_to;
  lxw_col_t col_from, col_to;
  const char *str = NULL;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 6);
  int larg = extract_range(argc, argv, &row_from, &col_from, &row_to, &col_to);

  if (larg < argc) {
    str = StringValueCStr(argv[larg]);
    ++larg;
  } else {
    rb_raise(rb_eArgError, "No formula specified");
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_array_formula, row_from, col_from, row_to, col_to, str, format);
  return self;
}

#write_blank(cell, format = nil) ⇒ self #write_blank(row, col, format = nil) ⇒ self

Make a cell blank with format.

Overloads:

  • #write_blank(cell, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_blank(row, col, format = nil) ⇒ self

    Returns:

    • (self)


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/xlsxwriter/worksheet.c', line 387

VALUE
worksheet_write_blank_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 1, 3);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_blank, row, col, format);
  return self;
}

#write_boolean(cell, value, format = nil) ⇒ self #write_boolean(row, col, value, format = nil) ⇒ self

Writes a boolean value into a cell with format.

Overloads:

  • #write_boolean(cell, value, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_boolean(row, col, value, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_boolean_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  int bool_value = 0;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    bool_value = argv[larg] && !NIL_P(argv[larg]);
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_boolean, row, col, bool_value, format);
  return self;
}

#write_datetime(cell, value, format = nil) ⇒ self #write_datetime(row, col, value, format = nil) ⇒ self

Writes a datetime value into a cell with format.

Overloads:

  • #write_datetime(cell, value, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_datetime(row, col, value, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_datetime_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    value = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  struct lxw_datetime datetime = value_to_lxw_datetime(value);
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_datetime, row, col, &datetime, format);
  return self;
}

#write_formula(cell, formula, format = nil) ⇒ self #write_formula(row, col, formula, format = nil) ⇒ self

Writes a formula into a cell with format.

Overloads:

  • #write_formula(cell, formula, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_formula(row, col, formula, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_formula_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    value = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  const char *str = RSTRING_PTR(value);
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_formula, row, col, str, format);
  return self;
}

#write_formula_num(cell, formula, value, format = nil) ⇒ self #write_formula_num(row, col, formula, value, format = nil) ⇒ self

Writes a formula with value into a cell with format.

Overloads:

  • #write_formula_num(cell, formula, value, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_formula_num(row, col, formula, value, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_formula_num_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE formula = Qnil;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 3, 5);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    formula = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    value = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  const char *str = RSTRING_PTR(formula);
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_formula_num, row, col, str, format, NUM2DBL(value));
  return self;
}

#number_string(cell, value, format = nil) ⇒ self #number_string(row, col, value, format = nil) ⇒ self

Writes a number value into a cell with format.

Overloads:

  • #number_string(cell, value, format = nil) ⇒ self

    Returns:

    • (self)
  • #number_string(row, col, value, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_number_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    value = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  const double num = NUM2DBL(value);
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_number, row, col, num, format);
  return self;
}

#write_rich_string(cell, value) ⇒ self #write_rich_string(row, col, value) ⇒ self

Writes a rich_string value into a cell.

Overloads:

  • #write_rich_string(cell, value) ⇒ self

    Returns:

    • (self)
  • #write_rich_string(row, col, value) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_rich_string_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);
  VALUE workbook = rb_iv_get(self, "@workbook");

  if (larg < argc) {
    value = argv[larg];
    if (TYPE(value) == T_ARRAY)
      value = rb_funcall(cRichString, rb_intern("new"), 2, workbook, value);
    else if (rb_class_of(value) != cRichString) {
      rb_raise(rb_eArgError, "Wrong type of value: %"PRIsVALUE, rb_class_of(value));
    }
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  if (NIL_P(value)) {
    rb_raise(rb_eArgError, "No value specified");
  }

  lxw_format *format = workbook_get_format(workbook, format_key);
  lxw_rich_string_tuple **rich_string = serialize_rich_string(value);
  if (rich_string) {
    LXW_ERR_RESULT_CALL(worksheet, write_rich_string, row, col, rich_string, format);
  }
  return self;
}

#write_string(cell, value, format = nil) ⇒ self #write_string(row, col, value, format = nil) ⇒ self

Writes a string value into a cell with format.

Overloads:

  • #write_string(cell, value, format = nil) ⇒ self

    Returns:

    • (self)
  • #write_string(row, col, value, format = nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_string_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE value = Qnil;
  VALUE format_key = Qnil;

  rb_check_arity(argc, 2, 4);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    value = argv[larg];
    ++larg;
  }

  if (larg < argc) {
    format_key = argv[larg];
    ++larg;
  }

  const char *str = StringValueCStr(value);
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  LXW_NO_RESULT_CALL(worksheet, write_string, row, col, str, format);
  return self;
}

#write_url(cell, url, format = nil, string: nil, tooltip: nil, format: nil) ⇒ self #write_url(row, col, url, format = nil, string: nil, tooltip: nil, format: nil) ⇒ self

Writes a url into a cell with format.

Overloads:

  • #write_url(cell, url, format = nil, string: nil, tooltip: nil, format: nil) ⇒ self

    Returns:

    • (self)
  • #write_url(row, col, url, format = nil, string: nil, tooltip: nil, format: nil) ⇒ self

    Returns:

    • (self)


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

VALUE
worksheet_write_url_(int argc, VALUE *argv, VALUE self) {
  lxw_row_t row;
  lxw_col_t col;
  VALUE url = Qnil; //argv[2];
  VALUE format_key = Qnil; // argv[3];
  VALUE opts = Qnil;

  rb_check_arity(argc, 2, 5);
  int larg = extract_cell(argc, argv, &row, &col);

  if (larg < argc) {
    url = argv[larg];
    ++larg;
  }

  const char *url_str = RSTRING_PTR(url);
  char *string = NULL;
  char *tooltip = NULL;
  while (larg < argc) {
    switch(TYPE(argv[larg])) {
    case T_HASH:
      opts = argv[larg];
      break;
    case T_SYMBOL: case T_STRING: case T_NIL:
      format_key = argv[larg];
      break;
    default:
      rb_raise(rb_eTypeError, "Expected Hash, symbol or string but got %"PRIsVALUE, rb_obj_class(argv[larg]));
    }
    ++larg;
  }

  if (!NIL_P(opts)) {
    VALUE val;
    val = rb_hash_aref(opts, ID2SYM(rb_intern("string")));
    if (!NIL_P(val)) {
      string = StringValueCStr(val);
    }
    val = rb_hash_aref(opts, ID2SYM(rb_intern("tooltip")));
    if (!NIL_P(val)) {
      tooltip = StringValueCStr(val);
    }
    if (NIL_P(format_key)) {
      format_key = rb_hash_aref(opts, ID2SYM(rb_intern("format")));
    }
  }
  struct worksheet *ptr;
  VALUE workbook = rb_iv_get(self, "@workbook");
  lxw_format *format = workbook_get_format(workbook, format_key);
  TypedData_Get_Struct(self, struct worksheet, &worksheet_type, ptr);
  if (string || tooltip) {
    worksheet_write_url_opt(ptr->worksheet, row, col, url_str, format, string, tooltip);
  } else {
    worksheet_write_url(ptr->worksheet, row, col, url_str, format);
  }
  return self;
}

#zoom=(zoom) ⇒ Object

Sets the worksheet zoom factor in the range 10 <= zoom <= 400.



1034
1035
1036
1037
1038
# File 'ext/xlsxwriter/worksheet.c', line 1034

VALUE
worksheet_set_zoom_(VALUE self, VALUE val) {
  LXW_NO_RESULT_CALL(worksheet, set_zoom, NUM2INT(val));
  return self;
}