excel_to_code 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f0ca6f9f95aa9175bde4d6cdce638d4d11d09b3
4
- data.tar.gz: f0ceec9fc7895942395cecb4b03428eb01bac979
3
+ metadata.gz: 880640b9610ce6658c3ba7a5ff8d6f96048d0705
4
+ data.tar.gz: 08f680f1229c0e00d769b3b7024e452a32a5ebd5
5
5
  SHA512:
6
- metadata.gz: 32560230728d0caaab4d270fd3e9faa6190160ad5fe4b198cacab2ca89d80cc4bdf9a039a5c9eabeacd3cb55bdc0bd6f225af6c432e256ba20f1787945d603c7
7
- data.tar.gz: a2b72b13008837e562abbc86100925871ce33ba9f5fa47cf76d012aba306ac3f0756bdba35e77f552afa54f5e9a2d0bd26b8c3410446dbd0e884a44494b04511
6
+ metadata.gz: 244303086ecf46f8be290f2d1c46f4c2b929c21b2bca0f7129d9edeb65e06e068824039250f650d12b3eaa4f85b2e9d08423b042cbf71b68e9a4d9f4c3311e4f
7
+ data.tar.gz: d40bf490120ec061bf94fbf65c32daf136edd1d9abefb833c1e1cf8a0c17940d80c6fe5e8ca8fd436c22aad17db8f3cbdc5312a55338317ab16614b7be107c98
data/src/compile/c/a.out CHANGED
Binary file
@@ -58,6 +58,7 @@ static ExcelValue find(ExcelValue string_to_look_for_v, ExcelValue string_to_loo
58
58
  static ExcelValue iferror(ExcelValue value, ExcelValue value_if_error);
59
59
  static ExcelValue excel_index(ExcelValue array_v, ExcelValue row_number_v, ExcelValue column_number_v);
60
60
  static ExcelValue excel_index_2(ExcelValue array_v, ExcelValue row_number_v);
61
+ static ExcelValue large(ExcelValue array_v, ExcelValue k_v);
61
62
  static ExcelValue left(ExcelValue string_v, ExcelValue number_of_characters_v);
62
63
  static ExcelValue left_1(ExcelValue string_v);
63
64
  static ExcelValue max(int number_of_arguments, ExcelValue *arguments);
@@ -69,6 +70,7 @@ static ExcelValue power(ExcelValue a_v, ExcelValue b_v);
69
70
  static ExcelValue excel_round(ExcelValue number_v, ExcelValue decimal_places_v);
70
71
  static ExcelValue rounddown(ExcelValue number_v, ExcelValue decimal_places_v);
71
72
  static ExcelValue roundup(ExcelValue number_v, ExcelValue decimal_places_v);
73
+ static ExcelValue excel_int(ExcelValue number_v);
72
74
  static ExcelValue string_join(int number_of_arguments, ExcelValue *arguments);
73
75
  static ExcelValue subtotal(ExcelValue type, int number_of_arguments, ExcelValue *arguments);
74
76
  static ExcelValue sumifs(ExcelValue sum_range_v, int number_of_arguments, ExcelValue *arguments);
@@ -172,6 +174,7 @@ const ExcelValue NAME = {.type = ExcelError, .number = 1};
172
174
  const ExcelValue DIV0 = {.type = ExcelError, .number = 2};
173
175
  const ExcelValue REF = {.type = ExcelError, .number = 3};
174
176
  const ExcelValue NA = {.type = ExcelError, .number = 4};
177
+ const ExcelValue NUM = {.type = ExcelError, .number = 5};
175
178
 
176
179
  // This is the error flag
177
180
  static int conversion_error = 0;
@@ -223,6 +226,7 @@ static void inspect_excel_value(ExcelValue v) {
223
226
  case 2: printf("DIV0\n"); break;
224
227
  case 3: printf("REF\n"); break;
225
228
  case 4: printf("NA\n"); break;
229
+ case 5: printf("NUM\n"); break;
226
230
  }
227
231
  break;
228
232
  default:
@@ -588,6 +592,61 @@ static ExcelValue excel_index_2(ExcelValue array_v, ExcelValue offset) {
588
592
  return REF;
589
593
  };
590
594
 
595
+ int compare_doubles (const void *a, const void *b) {
596
+ const double *da = (const double *) a;
597
+ const double *db = (const double *) b;
598
+
599
+ return (*da > *db) - (*da < *db);
600
+ }
601
+
602
+ static ExcelValue large(ExcelValue range_v, ExcelValue k_v) {
603
+ CHECK_FOR_PASSED_ERROR(range_v)
604
+ CHECK_FOR_PASSED_ERROR(k_v)
605
+
606
+ int k = (int) number_from(k_v);
607
+ CHECK_FOR_CONVERSION_ERROR;
608
+
609
+ // Check for edge case where just a single number passed
610
+ if(range_v.type == ExcelNumber) {
611
+ if( k == 1 ) {
612
+ return range_v;
613
+ } else {
614
+ return NUM;
615
+ }
616
+ }
617
+
618
+ // Otherwise grumble if not a range
619
+ if(!range_v.type == ExcelRange) { return VALUE; }
620
+
621
+ // Check that our k is within bounds
622
+ if(k < 1) { return NUM; }
623
+ int range_size = range_v.rows * range_v.columns;
624
+
625
+ // OK this is a really naive implementation.
626
+ // FIXME: implement the BFPRT algorithm
627
+ double *sorted = malloc(sizeof(double)*range_size);
628
+ int sorted_size = 0;
629
+ ExcelValue *array_v = range_v.array;
630
+ ExcelValue x_v;
631
+ int i;
632
+ for(i = 0; i < range_size; i++ ) {
633
+ x_v = array_v[i];
634
+ if(x_v.type == ExcelError) { return x_v; };
635
+ if(x_v.type == ExcelNumber) {
636
+ sorted[sorted_size] = x_v.number;
637
+ sorted_size++;
638
+ }
639
+ }
640
+ // Check other bound
641
+ if(k > sorted_size) { return NUM; }
642
+
643
+ qsort(sorted, sorted_size, sizeof (double), compare_doubles);
644
+
645
+ ExcelValue result = new_excel_number(sorted[sorted_size - k]);
646
+ free(sorted);
647
+ return result;
648
+ }
649
+
591
650
 
592
651
  static ExcelValue excel_match(ExcelValue lookup_value, ExcelValue lookup_array, ExcelValue match_type ) {
593
652
  CHECK_FOR_PASSED_ERROR(lookup_value)
@@ -1061,6 +1120,15 @@ static ExcelValue roundup(ExcelValue number_v, ExcelValue decimal_places_v) {
1061
1120
  return new_excel_number( ceil(number * multiple) / multiple );
1062
1121
  }
1063
1122
 
1123
+ static ExcelValue excel_int(ExcelValue number_v) {
1124
+ CHECK_FOR_PASSED_ERROR(number_v)
1125
+
1126
+ NUMBER(number_v, number)
1127
+ CHECK_FOR_CONVERSION_ERROR
1128
+
1129
+ return new_excel_number(floor(number));
1130
+ }
1131
+
1064
1132
  static ExcelValue string_join(int number_of_arguments, ExcelValue *arguments) {
1065
1133
  int allocated_length = 100;
1066
1134
  int used_length = 0;
@@ -1544,6 +1612,20 @@ int test_functions() {
1544
1612
  assert(count(4,array1).number == 2);
1545
1613
  assert(count(3,array2).number == 3);
1546
1614
  assert(count(4,array3).number == 3);
1615
+
1616
+ // Test Large
1617
+ ExcelValue large_test_array_1[] = { new_excel_number(10), new_excel_number(100), new_excel_number(500), BLANK };
1618
+ ExcelValue large_test_array_1_v = new_excel_range(large_test_array_1, 1, 4);
1619
+ assert(large(large_test_array_1_v, new_excel_number(1)).number == 500);
1620
+ assert(large(large_test_array_1_v, new_excel_number(2)).number == 100);
1621
+ assert(large(large_test_array_1_v, new_excel_number(3)).number == 10);
1622
+ assert(large(large_test_array_1_v, new_excel_number(4)).type == ExcelError);
1623
+ assert(large(new_excel_number(500), new_excel_number(1)).number == 500);
1624
+ ExcelValue large_test_array_2[] = { new_excel_number(10), new_excel_number(100), new_excel_number(500), VALUE };
1625
+ ExcelValue large_test_array_2_v = new_excel_range(large_test_array_2, 1, 4);
1626
+ assert(large(large_test_array_2_v,new_excel_number(2)).type == ExcelError);
1627
+ assert(large(new_excel_number(500),VALUE).type == ExcelError);
1628
+
1547
1629
 
1548
1630
  // Test COUNTA
1549
1631
  ExcelValue count_a_test_array_1[] = { new_excel_number(10), new_excel_number(5), TRUE, FALSE, new_excel_string("Hello"), VALUE, BLANK};
@@ -1867,6 +1949,10 @@ int test_functions() {
1867
1949
  assert(rounddown(new_excel_number(1.56),new_excel_number(1)).number == 1.5);
1868
1950
  assert(rounddown(new_excel_number(-1.56),new_excel_number(1)).number == -1.5);
1869
1951
 
1952
+ // Test int
1953
+ assert(excel_int(new_excel_number(8.9)).number == 8.0);
1954
+ assert(excel_int(new_excel_number(-8.9)).number == -9.0);
1955
+
1870
1956
  // Test roundup
1871
1957
  assert(roundup(new_excel_number(1.1), new_excel_number(0)).number == 2.0);
1872
1958
  assert(roundup(new_excel_number(1.5), new_excel_number(0)).number == 2.0);
@@ -41,6 +41,8 @@ class MapFormulaeToC < MapValuesToC
41
41
  'IFERROR' => 'iferror',
42
42
  'INDEX2' => 'excel_index_2',
43
43
  'INDEX3' => 'excel_index',
44
+ 'INT' => 'excel_int',
45
+ 'LARGE' => 'large',
44
46
  'LEFT1' => 'left_1',
45
47
  'LEFT2' => 'left',
46
48
  'MATCH2' => 'excel_match_2',
@@ -27,6 +27,8 @@ class MapFormulaeToRuby < MapValuesToRuby
27
27
  'IF' => 'excel_if',
28
28
  'IFERROR' => 'iferror',
29
29
  'INDEX' => 'index',
30
+ 'INT' => 'int',
31
+ 'LARGE' => 'large',
30
32
  'LEFT' => 'left',
31
33
  'MATCH' => 'excel_match',
32
34
  'MAX' => 'max',
@@ -24,6 +24,7 @@ require_relative 'excel_functions/subtract'
24
24
  require_relative 'excel_functions/multiply'
25
25
  require_relative 'excel_functions/divide'
26
26
  require_relative 'excel_functions/power'
27
+ require_relative 'excel_functions/negative'
27
28
 
28
29
  # More advanced arithmetic
29
30
  require_relative 'excel_functions/abs'
@@ -35,50 +36,36 @@ require_relative 'excel_functions/sum'
35
36
  require_relative 'excel_functions/average'
36
37
  require_relative 'excel_functions/max'
37
38
  require_relative 'excel_functions/min'
38
-
39
- # Financial functions
40
- require_relative 'excel_functions/pmt'
41
-
42
- # Geometry functions
43
- require_relative 'excel_functions/cosh'
44
-
45
- # String functions
46
- require_relative 'excel_functions/string_join'
47
- require_relative 'excel_functions/excel_match'
48
- require_relative 'excel_functions/find'
49
-
50
- # Search functions
51
- require_relative 'excel_functions/choose'
52
-
53
- # Other functions
54
-
55
-
56
-
57
-
58
-
59
-
60
- require_relative 'excel_functions/round'
61
-
62
39
  require_relative 'excel_functions/subtotal'
63
-
64
40
  require_relative 'excel_functions/count'
65
-
66
41
  require_relative 'excel_functions/counta'
67
-
68
42
  require_relative 'excel_functions/sumif'
69
-
70
43
  require_relative 'excel_functions/sumifs'
71
-
72
44
  require_relative 'excel_functions/sumproduct'
73
45
 
46
+ # Lookup functions
47
+ require_relative 'excel_functions/index'
48
+ require_relative 'excel_functions/excel_match'
74
49
  require_relative 'excel_functions/vlookup'
50
+ require_relative 'excel_functions/choose'
51
+ require_relative 'excel_functions/large'
75
52
 
76
- require_relative 'excel_functions/index'
53
+ # Financial functions
54
+ require_relative 'excel_functions/pmt'
77
55
 
56
+ # Geometry functions
57
+ require_relative 'excel_functions/cosh'
58
+
59
+ # String functions
60
+ require_relative 'excel_functions/string_join'
61
+ require_relative 'excel_functions/find'
78
62
  require_relative 'excel_functions/left'
79
63
 
64
+ # Rounding functions
65
+ require_relative 'excel_functions/round'
80
66
  require_relative 'excel_functions/roundup'
81
-
82
67
  require_relative 'excel_functions/rounddown'
68
+ require_relative 'excel_functions/int'
69
+
70
+ # Other functions
83
71
 
84
- require_relative 'excel_functions/negative'
@@ -0,0 +1,10 @@
1
+ module ExcelFunctions
2
+
3
+ def int(number)
4
+ number = number_argument(number)
5
+ return number if number.is_a?(Symbol)
6
+ number.floor.to_f
7
+ end
8
+
9
+
10
+ end
@@ -0,0 +1,17 @@
1
+ module ExcelFunctions
2
+
3
+ def large(range, k)
4
+ range = [range] unless range.is_a?(Array)
5
+ range = range.flatten
6
+ error = range.find {|a| a.is_a?(Symbol)}
7
+ error ||= k if k.is_a?(Symbol)
8
+ return error if error
9
+ range.delete_if { |v| !v.is_a?(Numeric) }
10
+ return :value unless k.is_a?(Numeric)
11
+ return :num unless k>0 && k<=range.size
12
+ range.sort!
13
+ range.reverse!
14
+ range[k-1]
15
+ end
16
+
17
+ end
@@ -16,7 +16,7 @@ class ExtractArrayFormulae < ExtractFormulae
16
16
  output.write "\t"
17
17
  output.write @array_range
18
18
  output.write "\t"
19
- output.write @formula.join
19
+ output.write @formula.join.gsub(/[\n\r]+/,'')
20
20
  output.write "\n"
21
21
  end
22
22
 
@@ -16,7 +16,7 @@ class ExtractSharedFormulae < ExtractFormulae
16
16
  output.write "\t"
17
17
  output.write @shared_range
18
18
  output.write "\t"
19
- output.write @formula.join
19
+ output.write @formula.join.gsub(/[\n\r]+/,'')
20
20
  output.write "\n"
21
21
  end
22
22
 
@@ -11,7 +11,7 @@ class ExtractSimpleFormulae < ExtractFormulae
11
11
  return if @formula.empty?
12
12
  output.write @ref
13
13
  output.write "\t"
14
- output.write @formula.join
14
+ output.write @formula.join.gsub(/[\r\n]+/,'')
15
15
  output.write "\n"
16
16
  end
17
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excel_to_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Counsell, Green on Black Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-03 00:00:00.000000000 Z
11
+ date: 2013-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubypeg
@@ -134,6 +134,8 @@ files:
134
134
  - src/excel/excel_functions/find.rb
135
135
  - src/excel/excel_functions/iferror.rb
136
136
  - src/excel/excel_functions/index.rb
137
+ - src/excel/excel_functions/int.rb
138
+ - src/excel/excel_functions/large.rb
137
139
  - src/excel/excel_functions/left.rb
138
140
  - src/excel/excel_functions/less_than.rb
139
141
  - src/excel/excel_functions/less_than_or_equal.rb
@@ -238,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
240
  version: '0'
239
241
  requirements: []
240
242
  rubyforge_project:
241
- rubygems_version: 2.0.0
243
+ rubygems_version: 2.0.3
242
244
  signing_key:
243
245
  specification_version: 4
244
246
  summary: Converts .xlxs files into pure ruby 1.9 code or pure C code so that they