Module: Xcellus

Defined in:
lib/xcellus.rb,
ext/xcellus_bin/xcellus_bin.c

Overview

Xcellus provides a clean interface to an underlying, native XLSX parser/writer

Defined Under Namespace

Classes: Instance

Constant Summary collapse

VERSION =
'2.0.4'.freeze

Class Method Summary collapse

Class Method Details

._append(handle, data) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/xcellus_bin/xcellus_bin.c', line 98

VALUE rb_xcellus_append(VALUE self, VALUE handle, VALUE data) {
	if (RB_TYPE_P(handle, T_STRING) == 0 || RB_TYPE_P(data, T_STRING) == 0)
		return Qnil;
	struct go_xcellus_append_return result = go_xcellus_append(RSTRING_PTR(handle), RSTRING_PTR(data));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	return Qtrue;
}

._close(handle) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/xcellus_bin/xcellus_bin.c', line 71

VALUE rb_xcellus_close(VALUE self, VALUE handle) {
	if (RB_TYPE_P(handle, T_STRING) == 0) return Qnil;
	struct go_xcellus_end_return result = go_xcellus_end(RSTRING_PTR(handle));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	return Qtrue;
}

._find_in_column(handle, sheetName, index, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'ext/xcellus_bin/xcellus_bin.c', line 38

VALUE rb_xcellus_find_in_column(VALUE self, VALUE handle, VALUE sheetName, VALUE index, VALUE value) {
	if (RB_TYPE_P(handle, T_STRING) == 0) return Qnil;
	if (RB_TYPE_P(sheetName, T_STRING) == 0) return Qnil;
	if (RB_TYPE_P(index, T_FIXNUM) == 0) return Qnil;
	if (RB_TYPE_P(value, T_STRING) == 0) return Qnil;
	struct go_xcellus_find_in_column_return result = go_xcellus_find_in_column(RSTRING_PTR(handle), RSTRING_PTR(sheetName), RSTRING_PTR(value), FIX2INT(index));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	long long foundIndex = result.r2;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	return INT2NUM(foundIndex);
}

._load(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/xcellus_bin/xcellus_bin.c', line 23

VALUE rb_xcellus_load(VALUE self, VALUE path) {
	if (RB_TYPE_P(path, T_STRING) == 0) return Qnil;
	struct go_xcellus_load_return result = go_xcellus_load(RSTRING_PTR(path));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	char *handle = result.r2;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	VALUE retval = rb_str_new_cstr(handle);
	return retval;
}

._replace_row(handle, sheetName, data, index) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/xcellus_bin/xcellus_bin.c', line 55

VALUE rb_xcellus_replace_row(VALUE self, VALUE handle, VALUE sheetName, VALUE data, VALUE index) {
	if (RB_TYPE_P(handle, T_STRING) == 0) return Qnil;
	if (RB_TYPE_P(sheetName, T_STRING) == 0) return Qnil;
	if (RB_TYPE_P(index, T_FIXNUM) == 0) return Qnil;
	if (RB_TYPE_P(data, T_STRING) == 0) return Qnil;
	struct go_xcellus_replace_row_return result = go_xcellus_replace_row(RSTRING_PTR(handle), RSTRING_PTR(sheetName), RSTRING_PTR(data), FIX2INT(index));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	return Qtrue;
}

._save(handle, path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/xcellus_bin/xcellus_bin.c', line 84

VALUE rb_xcellus_save(VALUE self, VALUE handle, VALUE path) {
	if (RB_TYPE_P(handle, T_STRING) == 0) return Qnil;
	if (RB_TYPE_P(path, T_STRING) == 0) return Qnil;
	struct go_xcellus_save_return result = go_xcellus_save(RSTRING_PTR(handle), RSTRING_PTR(path));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}
	return Qtrue;
}

._transform(str) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'ext/xcellus_bin/xcellus_bin.c', line 5

VALUE rb_xcellus_transform(VALUE self, VALUE str) {
	if (RB_TYPE_P(str, T_STRING) == 0) return Qnil;
	struct go_xcellus_process_return result = go_xcellus_process(RSTRING_PTR(str));
	bool failed = result.r0 == 1;
	char *error_description = result.r1;
	GoInt buffer_len = result.r2;
	char *buffer = result.r3;
	if(failed) {
		VALUE errObj = rb_exc_new2(rb_eStandardError, error_description);
		free(error_description);
		rb_exc_raise(errObj);
	}

    VALUE retval = rb_str_new(buffer, buffer_len);
    free(buffer);
    return retval;
}

.load(path) ⇒ Object

Loads a XLSX file from the provided path, returning an object of type Xcellus::Instance, that exposes methods for manipulating data in the file



40
41
42
43
44
45
46
# File 'lib/xcellus.rb', line 40

def load(path)
  unless path.kind_of? String
    raise ArgumentError, 'Xcellus.load expects a string path'
  end
  handle = Xcellus::_load(path)
  return Xcellus::Instance.new(handle)
end

.transform(input) ⇒ Object

Transforms a provided array of objects into a XLSX file, and returns it as a StringIO object. Example: Xcellus.transform [

{
  title: 'Brian\'s Worksheet',
  headers: [:Artist, :Track, :Playcount],
  rows: [
      ['Metallica',               'Hero of the Day',      242],
      ['Metallica',               'The Shortest Straw',   186],
      ['Queens of the Stone Age', 'My God Is the Sun',    276],
      ['Queens of the Stone Age', 'I Sat by the Ocean',   270],
      ['Gorillaz',                'On Melancholy Hill',   203],
      ['Gorillaz',                'Kids With Guns',       184],
  ]
}

]

> StringIO

The returned XLSX file will contain a single worksheet named “Brian’s Worksheet”, and the provided headers and rows.



30
31
32
33
34
35
# File 'lib/xcellus.rb', line 30

def transform(input)
  unless input.kind_of? Array
    raise ArgumentError, 'Xcellus.transform only accepts Arrays'
  end
  StringIO.new(Xcellus::_transform(input.to_json))
end

.with(path) ⇒ Object

with opens the provided file and passes the loaded instance to the provided block. It ensures ‘close` is called, freeing resources. Example: Xcellus.with ’/path/to/file’ do |file|

file.append ...
file.replace_row ...
file.save '/new/path/to/file'

end

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
# File 'lib/xcellus.rb', line 56

def with(path)
  raise ArgumentError, 'with requires a block' unless block_given?
  instance = load(path)
  result = yield instance
  instance.close
  return result
end