[英]Sorting in OpenOffice Calc via Delphi
I've spent several hours trying to sort data in OpenOffice Calc in Delphi.我花了几个小时试图在 Delphi 的 OpenOffice Calc 中对数据进行排序。 I tried to follow the examples in basic (from "OpenOffice.org Macros Explained"*) or C#, but still no result.
我尝试遵循基本(来自“OpenOffice.org Macros Explained”*)或 C# 中的示例,但仍然没有结果。 Data wont change at all.
数据根本不会改变。 What am i doing wrong?
我究竟做错了什么? Maybe the problem is with data types?
也许问题出在数据类型上?
* https://www.pitonyak.org/OOME_3_0.pdf , page 488. * https://www.pitonyak.org/OOME_3_0.pdf ,第 488 页。
Minimal reproducible example:最小可重现示例:
program OpenOfficeCalcSortingIssue;
{$APPTYPE CONSOLE}
uses
System.SysUtils, Variants, ComObj, ActiveX;
var
StarOffice: OleVariant;
SODesktop: OleVariant;
SOCalc: OleVariant;
CalcSheets: OleVariant;
CalcSheet: OleVariant;
SortFields: OleVariant;
SortDescriptor: OleVariant;
begin
CoInitialize(nil);
try
try
StarOffice:=CreateOleObject('com.sun.star.ServiceManager');
SODesktop:=StarOffice.CreateInstance('com.sun.star.frame.Desktop');
SOCalc:=SODesktop.LoadComponentFromURL('private:factory/scalc', '_blank', 0, VarArrayCreate([0, -1], varVariant));
CalcSheets:=SOCalc.GetSheets;
CalcSheet:=CalcSheets.GetByIndex(0);
CalcSheet.GetCellByPosition(0, 0).SetValue(2);
CalcSheet.GetCellByPosition(0, 1).SetValue(1);
CalcSheet.GetCellByPosition(0, 2).SetValue(4);
CalcSheet.GetCellByPosition(0, 3).SetValue(3);
SortFields:=VarArrayCreate([0, 0], varVariant);
SortFields[0]:=StarOffice.Bridge_GetStruct('com.sun.star.util.SortField');
SortFields[0].Field:=0;
SortFields[0].SortAscending:=True;
SortDescriptor:=VarArrayCreate([0, 0], varVariant);
SortDescriptor[0]:=StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
SortDescriptor[0].Name:='SortFields';
SortDescriptor[0].Value:=SortFields;
CalcSheet.GetCellRangeByName('A1:A4').Sort(SortDescriptor);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
finally
CoUninitialize;
end;
end.
As usual, solution came shortly after exposing the problem to the world :-) I've found an example from the depths of Internet.像往常一样,解决方案在向世界公开问题后不久就出现了:-) 我从互联网的深处找到了一个例子。 Hope this topic on SO will be googled a way more easily by people who stumbled into this issue.
希望偶然发现这个问题的人可以更轻松地在 google 上搜索这个关于 SO 的主题。 The answer is: SortFields is not just a variants array.
答案是:SortFields 不仅仅是一个变体数组。 It is a "Value Object" and must be set like this:
它是一个“值对象”,必须像这样设置:
ValueObject:=StarOffice.Bridge_GetValueObject;
ValueObject.Set('[]com.sun.star.table.TableSortField', SortFields);
In example that i found it is commented "you must specify which type of sequence is transmitted to SortFields property".在我发现的示例中,它被注释为“您必须指定将哪种类型的序列传输到 SortFields 属性”。
So the whole code should be:所以整个代码应该是:
StarOffice:=CreateOleObject('com.sun.star.ServiceManager');
SODesktop:=StarOffice.CreateInstance('com.sun.star.frame.Desktop');
SOCalc:=SODesktop.LoadComponentFromURL('private:factory/scalc', '_blank', 0, VarArrayCreate([0, -1], varVariant));
CalcSheets:=SOCalc.GetSheets;
CalcSheet:=CalcSheets.GetByIndex(0);
CalcSheet.GetCellByPosition(0, 0).SetValue(2);
CalcSheet.GetCellByPosition(0, 1).SetValue(1);
CalcSheet.GetCellByPosition(0, 2).SetValue(4);
CalcSheet.GetCellByPosition(0, 3).SetValue(3);
SortFields:=VarArrayCreate([0, 0], varVariant);
SortFields[0]:=StarOffice.Bridge_GetStruct('com.sun.star.table.TableSortField');
SortFields[0].Field:=0;
SortFields[0].IsAscending:=True;
ValueObject:=StarOffice.Bridge_GetValueObject;
ValueObject.Set('[]com.sun.star.table.TableSortField', SortFields);
SortDescriptor:=VarArrayCreate([0, 0], varVariant);
SortDescriptor[0]:=StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
SortDescriptor[0].Name:='SortFields';
SortDescriptor[0].Value:=ValueObject;
CalcSheet.GetCellRangeByName('A1:A4').Sort(SortDescriptor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.