SpreadsheetTranslator.js
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
150
151
152
153
154
// Copyright (C) 2004 by Alain Bienvenue. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
function SpreadsheetTranslator()
{
this.currentLine;
this.rows;
this.fitNesseTables;
this.isImplicitTableStart = function(index)
{
if (this.rows[index][0].match("^[A-Za-z][0-9A-Za-z]*\\.[A-Za-z][0-9A-Za-z]*"))
{
return true;
}
var i;
for (i = index; i < this.rows.length; i++)
{
if ((this.rows[i][0] == '!') || this.lineSize(this.rows[i]) == 0)
{
return false;
}
if (this.lineSize(this.rows[i]) > 1)
{
return true;
}
}
return false;
}
this.isExplicitTableStart = function(rows, index)
{
return (rows[index][0] == "!");
}
this.isNotTableLine = function(currentLine, columnsToSkip)
{
var row = this.rows[currentLine];
if (this.lineSize(row) == 0)
{
return true;
}
var i;
for (i = 0; i < columnsToSkip; i++)
{
if (row[i] != '')
{
return true;
}
}
return false;
}
this.lineSize = function(row)
{
var x;
for (x = row.length - 1; x >= 0; x--)
{
if (row[x] != '') return x + 1;
}
return 0;
}
this.parseExcelTable = function(excelTable)
{
var table = this.removeCarriageReturns(excelTable);
var lines = table.split("\n");
this.rows = new Array;
for (i = 0; i < lines.length; i++)
{
this.rows[i] = lines[i].split("\t");
}
}
this.removeCarriageReturns = function(str)
{
return str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
}
this.getFitNesseTables = function()
{
this.currentLine = 0;
this.fitNesseTables = new String();
while (this.currentLine < this.rows.length)
{
if (this.isExplicitTableStart(this.rows, this.currentLine))
{
this.processTable(1);
}
else if (this.isImplicitTableStart(this.currentLine))
{
if (this.rows[this.currentLine][0] == '')
{
this.processTable(1);
}
else
{
this.processTable(0);
}
}
else
{
this.fitNesseTables += "\n" + this.rows[this.currentLine][0];
}
this.currentLine++;
}
return this.fitNesseTables.substring(1);
}
this.processTable = function(columnsToSkip)
{
var tableFirstLine = this.currentLine;
var tableSize;
while (this.currentLine < this.rows.length)
{
var row = this.rows[this.currentLine];
if (this.currentLine > tableFirstLine && this.isNotTableLine(this.currentLine, columnsToSkip))
{
this.currentLine--;
return;
}
this.fitNesseTables += "\n";
var lineSize = 0;
if (this.currentLine == tableFirstLine)
{
this.fitNesseTables += "!";
lineSize = this.lineSize(row);
}
else if (this.currentLine == tableFirstLine + 1)
{
lineSize = this.lineSize(row);
tableSize = lineSize;
}
else
{
lineSize = Math.max(tableSize, this.lineSize(row));
lineSize = Math.min(lineSize, row.length);
}
this.fitNesseTables += "|";
var j;
for (j = columnsToSkip; j < lineSize; j++)
{
this.fitNesseTables += row[j] + "|";
}
this.currentLine++;
}
}
}