Skip to content

Commit a3a01c6

Browse files
Jan WielemakerJan Wielemaker
Jan Wielemaker
authored and
Jan Wielemaker
committed
Make tables module-aware; prepare for adding a database
1 parent 43697e4 commit a3a01c6

File tree

7 files changed

+84
-38
lines changed

7 files changed

+84
-38
lines changed

README.TXT

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ unoconv -f ods *.xlsx
4545

4646
---+ Approach
4747

48-
---++ Dependency discovery
48+
1. Find clear tables: a square of equally typed objects with
49+
aligned strings rows and columns.
4950

50-
Dependency of a cell is a cell or datasource.
51+
2. Delete tables that are completely inside other tables. These are
52+
typically the header of another table.
5153

52-
---++ Identify Datasources
54+
3. Find conflicts. These are tables that intersect.
5355

54-
A datasource is a block of cells (cell_range(Sheet,SX,SY,EX,EY)).
55-
56-
- Identify as dependency from formulas
56+
- Assume this is the result of header rows/columns that are shared.
57+
-

datasource.pl

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
:- module(datasource,
22
[ ds_sheet/2, % +DS, -Sheet
33
ds_size/3, % +DS, -Columns, -Rows
4+
ds_id/2, % ?DS, ?Id
45

56
ds_inside/3, % +DS, ?X, ?Y
67

@@ -14,6 +15,8 @@
1415
ds_column_slice/3, % +DS1, ?Offset, ?Slice
1516
ds_unbounded_column_slice/3 % +DS1, +Offset, ?Slice
1617
]).
18+
:- use_module(ods_table).
19+
1720

1821
/*******************************
1922
* SIMPLE PROPERTIES *
@@ -33,6 +36,23 @@
3336
Columns is EX-SX+1,
3437
Rows is EY-SY+1.
3538

39+
%% ds_id(+DS, -ID) is det.
40+
%
41+
% True when ID is an identifier for DS
42+
43+
ds_id(DS, Id) :-
44+
ground(DS), !,
45+
DS = cell_range(Sheet, SX,SY, EX,EY),
46+
column_name(SX, SC),
47+
column_name(EX, EC),
48+
( sheet_name_need_quotes(Sheet)
49+
-> format(atom(Id), '[\'~w\'.~w~w:~w~w]', [Sheet,SC,SY,EC,EY])
50+
; format(atom(Id), '[~w.~w~w:~w~w]', [Sheet,SC,SY,EC,EY])
51+
).
52+
ds_id(DS, Id) :-
53+
atom_codes(Id, Codes),
54+
phrase(ods_reference(DS, ''), Codes).
55+
3656

3757
/*******************************
3858
* COORDINATES *

ods_table.pl

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
cell/8, % :Sheet, ?X, ?Y, ?V, ?T, ?F, ?S, ?A
1616

1717
column_name/2, % ?Index, ?Name
18-
ods_DOM/3 % :Source, -DOM, +Options
18+
ods_DOM/3, % :Source, -DOM, +Options
19+
20+
sheet_name_need_quotes/1, % +SheetName
21+
ods_reference//2 % -Expr, +Table
1922
]).
2023
:- use_module(library(xpath)).
2124
:- use_module(library(sgml)).
@@ -831,6 +834,15 @@
831834
not_in_sheet_name(0'#).
832835
not_in_sheet_name(0'$).
833836

837+
%% sheet_name_need_quotes(+Name) is semidet.
838+
%
839+
% True when Name is a sheet name that needs (single) quotes.
840+
841+
sheet_name_need_quotes(Name) :-
842+
atom_codes(Name, Codes),
843+
member(Code, Codes),
844+
not_in_sheet_name(Code), !.
845+
834846

835847
/*******************************
836848
* CELL PROPERTIES *

recognise.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
cell_class(M:Sheet, SX,AY, TAbove), TAbove \== Type
6767
).
6868

69-
%% block(?DataSource, ?Type) is nondet.
69+
%% block(:DataSource, ?Type) is nondet.
7070

7171
block(M:cell_range(Sheet, SX,SY, EX,EY), Type) :-
7272
block(M:Sheet, SX,SY, EX,EY, Type).

table.pl

+37-28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
:- use_module(datasource).
88
:- use_module(library(lists)).
99

10+
:- meta_predicate
11+
tables(:, ?, -).
12+
1013
%% tables(?Sheet, +Type, -Tables) is det.
1114
%
1215
% Make an initial guess at all tables. Table is a list of
@@ -22,13 +25,14 @@
2225
NestedTables),
2326
append(NestedTables, Tables).
2427

25-
table_in_sheet(Sheet, Type, table(DS,Headers,Union)) :-
28+
table_in_sheet(M:Sheet, Type, table(Id,Type,DS,Headers,Union)) :-
2629
ds_sheet(DS, Sheet),
2730
cell_class(Type),
2831
anchor(DS, Type),
29-
once((block(DS, Type),
30-
table(DS, Headers))),
31-
ds_union([DS|Headers], Union).
32+
once((block(M:DS, Type),
33+
table(M:DS, Headers))),
34+
ds_union([DS|Headers], Union),
35+
ds_id(DS, Id).
3236

3337
%% remove_inside(+Tables0, -Tables) is det.
3438
%
@@ -67,50 +71,55 @@
6771
Cells).
6872

6973

70-
%% table(+DataDS, ?SupportDS) is nondet.
74+
%% table(:DataDS, ?SupportDS) is nondet.
7175
%
7276
% True when there is a table with DataDS and a list of support
7377
% datasources.
7478

75-
table(DataDS, TitleDS) :-
79+
table(QDataDS, TitleDS) :-
80+
QDataDS = _:DataDS,
7681
ds_size(DataDS, Cols, Rows),
77-
top_rows(DataDS, -1, TitleDS, Left),
78-
left_columns(DataDS, -1, Left, Right),
79-
right_columns(DataDS, Cols, Right, Bottom),
80-
bottom_rows(DataDS, Rows, Bottom, []).
81-
82-
%% top_rows(+DS, +StartIndex, -Rows, ?Tail) is nondet.
83-
%% bottom_rows(+DS, +StartIndex, -Rows, ?Tail) is nondet.
84-
%% left_columns(+DS, +StartIndex, -Rows, ?Tail) is nondet.
85-
%% right_columns(+DS, +StartIndex, -Rows, ?Tail) is nondet.
86-
87-
top_rows(DataDS, Index, [Row|Rows], Tail) :-
82+
top_rows(QDataDS, -1, TitleDS, Left),
83+
left_columns(QDataDS, -1, Left, Right),
84+
right_columns(QDataDS, Cols, Right, Bottom),
85+
bottom_rows(QDataDS, Rows, Bottom, []).
86+
87+
%% top_rows(:DS, +StartIndex, -Rows, ?Tail) is nondet.
88+
%% bottom_rows(:DS, +StartIndex, -Rows, ?Tail) is nondet.
89+
%% left_columns(:DS, +StartIndex, -Rows, ?Tail) is nondet.
90+
%% right_columns(:DS, +StartIndex, -Rows, ?Tail) is nondet.
91+
92+
top_rows(QDataDS, Index, [Row|Rows], Tail) :-
93+
QDataDS = M:DataDS,
8894
ds_unbounded_row_slice(DataDS, Index, Row),
89-
row(Row, string),
95+
row(M:Row, string),
9096
Up is Index - 1,
91-
top_rows(DataDS, Up, Rows, Tail).
97+
top_rows(QDataDS, Up, Rows, Tail).
9298
top_rows(_, _, Tail, Tail).
9399

94100

95-
bottom_rows(DataDS, Index, [Row|Rows], Tail) :-
101+
bottom_rows(QDataDS, Index, [Row|Rows], Tail) :-
102+
QDataDS = M:DataDS,
96103
ds_unbounded_row_slice(DataDS, Index, Row),
97-
row(Row, string),
104+
row(M:Row, string),
98105
Down is Index + 1,
99-
bottom_rows(DataDS, Down, Rows, Tail).
106+
bottom_rows(QDataDS, Down, Rows, Tail).
100107
bottom_rows(_, _, Tail, Tail).
101108

102109

103-
left_columns(DataDS, Index, [Col|Cols], Tail) :-
110+
left_columns(QDataDS, Index, [Col|Cols], Tail) :-
111+
QDataDS = M:DataDS,
104112
ds_unbounded_column_slice(DataDS, Index, Col),
105-
col(Col, string),
113+
col(M:Col, string),
106114
Up is Index - 1,
107-
left_columns(DataDS, Up, Cols, Tail).
115+
left_columns(QDataDS, Up, Cols, Tail).
108116
left_columns(_, _, Tail, Tail).
109117

110-
right_columns(DataDS, Index, [Col|Cols], Tail) :-
118+
right_columns(QDataDS, Index, [Col|Cols], Tail) :-
119+
QDataDS = M:DataDS,
111120
ds_unbounded_column_slice(DataDS, Index, Col),
112-
col(Col, string),
121+
col(M:Col, string),
113122
Right is Index + 1,
114-
right_columns(DataDS, Right, Cols, Tail).
123+
right_columns(QDataDS, Right, Cols, Tail).
115124
right_columns(_, _, Tail, Tail).
116125

test.pl

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@
144144
integer(SX), integer(EX),
145145
column_name(SX, CS),
146146
column_name(EX, CE),
147-
format('[~q.~w~w:~w~w]', [Sheet, CS,SY,CE,EY]).
147+
( atom(Sheet),
148+
\+ sheet_name_need_quotes(Sheet)
149+
-> format('[~w.~w~w:~w~w]', [Sheet, CS,SY,CE,EY])
150+
; format('[\'~w\'.~w~w:~w~w]', [Sheet, CS,SY,CE,EY])
151+
).
148152

149153
user:message_property(debug(ods(test(ok))), color(fg(green))).
150154
user:message_property(debug(ods(test(error))), color(fg(red))).

webui.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
\table_rows(Sheet, SX,SY, EX,EY))), !.
4545
web_portray(cell(Sheet,X,Y)) -->
4646
web_portray(cell_range(Sheet, X,Y, X,Y)).
47-
web_portray(table(_,_,Union)) -->
47+
web_portray(table(_Id,_Type,_DS,_Headers,Union)) -->
4848
web_portray(Union).
4949
web_portray(List) -->
5050
{ is_list(List), !,

0 commit comments

Comments
 (0)