Module: Rbxl::Native

Defined in:
ext/rbxl_native/native.c

Class Method Summary collapse

Class Method Details

.generate_sheet(rows) ⇒ Object

Rbxl::Native.generate_sheet(rows) → XML string

rows: Array of Arrays, each inner array is a row of cell values



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'ext/rbxl_native/native.c', line 609

static VALUE rb_native_generate(VALUE self, VALUE rows)
{
    (void)self;
    Check_Type(rows, T_ARRAY);

    VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));
    VALUE cWriteOnlyCell = rb_const_get(mRbxl, rb_intern("WriteOnlyCell"));

    long num_rows = RARRAY_LEN(rows);

    /* Find max columns for dimension ref */
    int max_cols = 1;
    for (long i = 0; i < num_rows; i++) {
        VALUE row = rb_ary_entry(rows, i);
        int len = (int)RARRAY_LEN(row);
        if (len > max_cols) max_cols = len;
    }

    dynbuf buf;
    dynbuf_init(&buf);

    #define W(s) dynbuf_append(&buf, s, sizeof(s) - 1)

    W("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
      "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">\n"
      "  <dimension ref=\"A1:");
    write_column_name(&buf, max_cols);
    write_int(&buf, num_rows);
    W("\"/>\n  <sheetData>");

    for (long i = 0; i < num_rows; i++) {
        VALUE row = rb_ary_entry(rows, i);
        Check_Type(row, T_ARRAY);
        long row_num = i + 1;
        long ncols = RARRAY_LEN(row);

        W("<row r=\"");
        write_int(&buf, row_num);
        W("\">");

        for (long j = 0; j < ncols; j++) {
            write_cell(&buf, (int)(j + 1), (int)row_num, rb_ary_entry(row, j), cWriteOnlyCell);
        }

        W("</row>");
    }

    W("</sheetData>\n</worksheet>");

    #undef W

    VALUE result = make_utf8_str(buf.data, (long)buf.len);
    dynbuf_free(&buf);
    return result;
}

.parse_sheet(xml_str, shared_strings) ⇒ Object




431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'ext/rbxl_native/native.c', line 431

static VALUE rb_native_parse(VALUE self, VALUE xml_str, VALUE shared_strings)
{
    (void)self;
    Check_Type(xml_str, T_STRING);
    Check_Type(shared_strings, T_ARRAY);

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 0;
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);

    return run_parse(&ctx, xml_str);
}

.parse_sheet_full(xml_str, shared_strings) ⇒ Object




453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'ext/rbxl_native/native.c', line 453

static VALUE rb_native_parse_full(VALUE self, VALUE xml_str, VALUE shared_strings)
{
    (void)self;
    Check_Type(xml_str, T_STRING);
    Check_Type(shared_strings, T_ARRAY);

    VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 1;
    ctx.cReadOnlyCell      = rb_const_get(mRbxl, rb_intern("ReadOnlyCell"));
    ctx.cRow               = rb_const_get(mRbxl, rb_intern("Row"));
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);
    dynbuf_init(&ctx.cell_ref);

    return run_parse(&ctx, xml_str);
}