Skip to content

Instantly share code, notes, and snippets.

@SmiSoft
Created April 27, 2021 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmiSoft/a3ec08887b23f95afcddeea4b296d789 to your computer and use it in GitHub Desktop.
Save SmiSoft/a3ec08887b23f95afcddeea4b296d789 to your computer and use it in GitHub Desktop.
ODT/ODS import/export for Lazarus (no LibreOffice installed required)
unit OdsImport;
interface
uses
Windows, Classes, SysUtils, OdtFilter, Hash, laz2_Dom;
Type
{ TOdsImport }
TOdsImport=class(TOdtProcess)
protected
fMap,fNotes:THash;
procedure ProcessContent(Doc:TXMLDocument);override;
public
class procedure ImportFromOds(const InputFile:string; Data, Notes:THash);
end;
implementation
function GetNodeTextp(Node:TDOMNode):string;
var
Child:TDOMNode;
begin
if Node.NodeName='text:p' then begin
Result:=Node.TextContent;
exit;
end;
For Child in Node do begin
Result:=GetNodeTextp(Child);
if Result<>'' then exit;
end;
Result:='';
end;
{ TOdsImport }
procedure TOdsImport.ProcessContent(Doc: TXMLDocument);
var
Roots:TDOMNodeList;
Table,Row,Cell:TDOMNode;
RowNo:integer;
BlockId:string;
I:integer;
begin
Roots:=Doc.getElementsByTagName('table:table');
For I:=0 to Roots.Count-1 do begin
Table:=Roots.Item[I];
For Row in Table do begin
if Row.NodeName<>'table:table-row' then continue;
RowNo:=0;
For Cell in Row do begin
if Cell.NodeName<>'table:table-cell' then continue;
if RowNo=0 then
BlockId:=GetNodeTextp(Cell)
else if RowNo=5 then
fMap[BlockId]:=GetNodeTextp(Cell)
else if RowNo=7 then
fNotes[BlockId]:=GetNodeTextp(Cell);
Inc(RowNo);
end;
end;
end;
end;
class procedure TOdsImport.ImportFromOds(const InputFile: string; Data, Notes:THash);
var
Temp:TOdsImport;
begin
Temp:=TOdsImport.Create;
try
Data.Clear;
Temp.fMap:=Data;
Temp.fNotes:=Notes;
Temp.Process(InputFile);
finally
Temp.Free;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment