2015-03-15 11:02:15 +00:00
|
|
|
{ This program seeks all spreadsheet hyperlinks in the file "source.xls" and
|
|
|
|
adds the linked worksheet to a new workbook. }
|
|
|
|
|
|
|
|
program collectlinks;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
|
|
|
cthreads,
|
|
|
|
{$ENDIF}{$ENDIF}
|
|
|
|
Classes, SysUtils, uriParser,
|
|
|
|
fpstypes, fpspreadsheet, fpsUtils, fpsAllFormats
|
|
|
|
{ you can add units after this };
|
|
|
|
|
|
|
|
const
|
|
|
|
srcFile = 'source.ods';
|
|
|
|
destFile = 'result';
|
|
|
|
|
|
|
|
var
|
|
|
|
srcWorkbook, destWorkbook, linkedWorkbook: TsWorkbook;
|
|
|
|
sheet, linkedSheet, destSheet: TsWorksheet;
|
|
|
|
cell: PCell;
|
|
|
|
hyperlink: PsHyperlink;
|
|
|
|
u: TURI;
|
|
|
|
fn: String; // Name of linked file
|
|
|
|
bookmark: String; // Bookmark of hyperlink
|
|
|
|
sheetFormat: TsSpreadsheetFormat;
|
|
|
|
sheetName: String;
|
|
|
|
r, c: Cardinal;
|
2021-04-15 12:58:58 +00:00
|
|
|
dir: String;
|
2015-03-15 11:02:15 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
// Just for the demo: create the file "source.xls". It contains hyperlinks to
|
2015-04-18 17:37:20 +00:00
|
|
|
// some of the "test" files created in the XXXXdemo projects
|
2015-03-15 11:02:15 +00:00
|
|
|
Write('Creating source workbook...');
|
2021-04-15 12:58:58 +00:00
|
|
|
dir := ExtractFilePath(ParamStr(0));
|
|
|
|
|
2015-03-15 11:02:15 +00:00
|
|
|
srcWorkbook := TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
sheet := srcWorkbook.AddWorksheet('Sheet');
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(0, 0, 'Link to biff8 test file');
|
2021-04-15 12:58:58 +00:00
|
|
|
sheet.WriteHyperlink(0, 0, '../../read_write/excel8demo/test.xls#''My Worksheet 2''!A1');
|
2015-03-15 11:02:15 +00:00
|
|
|
//sheet.WriteHyperlink(0, 0, '../excel8demo/test.xls#''Meu Relatório''!A1');
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(1, 0, 'Link to ods test file');
|
2021-04-15 12:58:58 +00:00
|
|
|
sheet.WriteHyperlink(1, 0, '../../read_write/opendocdemo/test.ods');
|
2015-03-15 11:02:15 +00:00
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(2, 0, 'E-Mail Link');
|
2015-03-15 11:02:15 +00:00
|
|
|
sheet.WriteHyperlink(2, 0, 'mailto:someone@mail.com;someoneelse@mail.com?Subject=This is a test');
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(3, 0, 'Web-Hyperlink');
|
2015-03-15 11:02:15 +00:00
|
|
|
sheet.WriteHyperlink(3, 0, 'http://www.lazarus-ide.org/');
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(4, 0, 'File-Link (absolute path)');
|
2021-04-15 12:58:58 +00:00
|
|
|
sheet.WriteHyperlink(4, 0, 'file:///'+ExpandFilename('../../../tests/testooxml_1899.xlsx'));
|
2015-03-15 11:02:15 +00:00
|
|
|
// This creates the URI such as "file:///D:\Prog_Lazarus\svn\lazarus-ccr\components\fpspreadsheet\tests\testooxml_1899.xlsx"
|
|
|
|
// but makes sure that the file exists on your system.
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteText(5, 0, 'Jump to A10');
|
2015-03-15 11:02:15 +00:00
|
|
|
sheet.WriteHyperlink(5, 0, '#A10');
|
|
|
|
|
2016-03-18 19:50:40 +00:00
|
|
|
sheet.WriteColWidth(0, 40, suChars);
|
2015-03-15 11:02:15 +00:00
|
|
|
|
2021-04-15 12:58:58 +00:00
|
|
|
srcWorkbook.WriteToFile(dir + srcFile, true);
|
2015-03-15 11:02:15 +00:00
|
|
|
finally
|
|
|
|
srcWorkbook.Free;
|
|
|
|
end;
|
|
|
|
WriteLn('Done.');
|
|
|
|
|
|
|
|
// Prepare destination workbook
|
|
|
|
destWorkbook := nil;
|
|
|
|
|
|
|
|
// Now open the source file and seek hyperlinks
|
|
|
|
Write('Reading source workbook, sheet ');
|
|
|
|
srcWorkbook := TsWorkbook.Create;
|
|
|
|
try
|
2021-04-15 12:58:58 +00:00
|
|
|
srcWorkbook.ReadFromFile(dir + srcFile);
|
2015-03-15 11:02:15 +00:00
|
|
|
sheet := srcWorkbook.GetWorksheetByIndex(0);
|
|
|
|
WriteLn(sheet.Name, '...');
|
|
|
|
|
|
|
|
for cell in sheet.Cells do
|
|
|
|
begin
|
|
|
|
hyperlink := sheet.FindHyperlink(cell);
|
|
|
|
if (hyperlink <> nil) then // Ignore cells without hyperlink
|
|
|
|
begin
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('Cell ', GetCellString(cell^.Row, cell^.Col), ':');
|
|
|
|
WriteLn(' Hyperlink "', hyperlink^.Target, '"');
|
|
|
|
if (hyperlink^.Target[1] = '#') then
|
|
|
|
WriteLn(' Ignoring internal hyperlink')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
u := ParseURI(hyperlink^.Target);
|
|
|
|
if u.Protocol = '' then begin
|
|
|
|
Write(' Local file (relative path)');
|
|
|
|
SplitHyperlink(hyperlink^.Target, fn, bookmark)
|
|
|
|
end else
|
|
|
|
if URIToFileName(hyperlink^.Target, fn) then
|
|
|
|
Write(' File (absolute path)')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
WriteLn(' Ignoring protocol "', u.Protocol, '"');
|
|
|
|
continue; // Ignore http, mailto etc.
|
|
|
|
end;
|
|
|
|
|
|
|
|
if not FileExists(fn) then
|
|
|
|
WriteLn(' does not exist.')
|
|
|
|
else
|
|
|
|
if GetFormatFromFileName(fn, sheetFormat) then
|
|
|
|
begin
|
|
|
|
Write(' supported. ');
|
|
|
|
// Create destination workbook if not yet done so far...
|
|
|
|
if destWorkbook = nil then
|
|
|
|
destWorkbook := TsWorkbook.Create;
|
|
|
|
// Open linked workbook
|
|
|
|
linkedworkbook := TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
linkedworkbook.ReadFromFile(fn, sheetFormat);
|
|
|
|
// Get linked worksheet
|
|
|
|
if bookmark = '' then
|
|
|
|
linkedSheet := linkedWorkbook.GetWorksheetByIndex(0)
|
|
|
|
else
|
|
|
|
if not linkedWorkbook.TryStrToCell(bookmark, linkedsheet, r, c)
|
|
|
|
then begin
|
|
|
|
WriteLn('Failure finding linked worksheet.');
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
// Copy linked worksheet to new sheet in destination workbook
|
2021-04-15 12:58:58 +00:00
|
|
|
destSheet := destWorkbook.CopyWorksheetFrom(linkedSheet, true);
|
2015-03-15 11:02:15 +00:00
|
|
|
// Create sheet name
|
|
|
|
sheetName := ExtractFileName(fn) + '#' +linkedSheet.Name;
|
|
|
|
destWorkbook.ValidWorksheetName(sheetName, true);
|
|
|
|
destSheet.Name := sheetName;
|
|
|
|
// Done
|
|
|
|
WriteLn(' Copied.');
|
|
|
|
finally
|
|
|
|
linkedworkbook.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Save destination workbook
|
|
|
|
WriteLn;
|
|
|
|
if destWorkbook <> nil then
|
|
|
|
begin
|
2021-04-15 12:58:58 +00:00
|
|
|
destworkbook.WriteToFile(dir + destFile+'.xls', true);
|
|
|
|
destworkbook.WriteToFile(dir + destFile+'.xlsx', true);
|
|
|
|
destworkbook.WriteToFile(dir + destFile+'.ods', true);
|
2015-03-15 11:02:15 +00:00
|
|
|
WriteLn('All hyperlinks to spreadsheets are collected in files ' + destFile + '.*');
|
|
|
|
end else
|
|
|
|
WriteLn('No hyperlinks found.');
|
|
|
|
|
|
|
|
finally
|
|
|
|
// Clean up
|
|
|
|
srcWorkbook.Free;
|
|
|
|
if destWorkbook <> nil then destWorkbook.Free;
|
|
|
|
end;
|
|
|
|
|
2021-04-15 12:58:58 +00:00
|
|
|
if ParamCount = 0 then
|
|
|
|
begin
|
|
|
|
{$IFDEF MSWindows}
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('Press ENTER to close...');
|
|
|
|
ReadLn;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
2015-03-15 11:02:15 +00:00
|
|
|
end.
|
|
|
|
|