OpenOffice Calc automation how alter a chart label of a scatter diagram - delphi

Hello could you please help me with the following. I have created a scattered chart and draw a chart from data of a column. The used data is not just after the cell which determines the label:
Column O:
Pwm1 <-- This is the cell I want to see as the label
27114 <-- not used data for graph
27055 <-- etc
27092
27070 <-- data for graph starts here
27105
27024
27092 <-- data for graph ends here
I would like the LABEL cell to appear as the Y column label name (Is now 'Column O'), but how?
This as far as I got (code is Delphi but if someone could help me with a basic example that's ok too):
(* Turn the symbol of the data points off *)
oChart.Diagram.SymbolType := _chartChartSymbolTypeNONE;
oDataSeries := oChart.getUsedData;
oDataSequences := oDataSeries.getDataSequences;
ShowMessage(oDataSequences[1].Label.SourceRangeRepresentation);
SourceRangeRepresentation returns the current label, but how to change?
Thanks Ad

This did it:
(*
creat new DataSequence from range representaion
that provides real data and its role in the series
oDataProvider: com.sun.star.chart2.data.XDataProvider
sRangeRepresentation: range address e.g. Sheet1.A1:B2
sRole: role is defined in com.sun.star.chart2.data.DataSequenceRole
*)
Function CreateDataSequence( oDataProvider : Variant; sRangeRepresentation : String; sRole :String ) : Variant;
Var
oDataSequence : Variant;
Begin
(* create .chart2.data.DataSequence from range representation *)
oDataSequence := oDataProvider.createDataSequenceByRangeRepresentation(sRangeRepresentation);
If NOT VarIsEmpty(oDataSequence) Then
oDataSequence.Role := sRole;
Result := oDataSequence;
End;
oNewLabel := CreateDataSequence(oChart.getDataProvider, '$Sheet1.$O$7', 'label');
oDataSequences[1].setLabel(oNewLabel);

Related

What is the correct way to copy the dictionary?

I need to check if there has been a change in a certain part of the application and therefore I make "copies" of the data after loading them and then compare them. One part of the comparison function involves checking keys in dictionaries like lDict1.Keys.EqualsTo(lDict2.Keys).
Although the dictionaries do not rely on the order of the elements, I didn't realize that even if I fill two dictionaries with the same data, they won't be created the same and the order of elements may change, so the previous function does not work properly because it relies on the elements order that may not match when using any of the following methods. (I'm not sure why)
var
lDict1, lDict2 : IDictionary<Integer, TObject>;
lKey : Integer;
begin
lDict1 := TCollections.CreateDictionary<Integer, TObject>;
lDict1.Add(5, nil); // Keys.First = 5, Keys.Last = 5
lDict1.Add(6, nil); // Keys.First = 5, Keys.Last = 6
lDict2 := TCollections.CreateDictionary<Integer, TObject>;
lDict2.AddRange(lDict1); // Keys.First = 6, Keys.Last = 5
lDict2.Clear;
for lKey in lDict1.Keys do // Keys.First = 6, Keys.Last = 5
lDict2.Add(lKey, nil);
end;
Is there any way to make an exact copy of the dictionary so I can compare them? One way to work around this problem is to create my own comparison function, but I'd like to avoid that.
function ContainsSameValues<T>(AEnumerable1, AEnumerable2: IEnumerable<T>): Boolean;
var
lValue : T;
begin
Result := AEnumerable1.Count = AEnumerable2.Count;
if Result then
begin
for lValue in AEnumerable1 do
begin
Result := AEnumerable2.Contains(lValue);
if not Result then
Exit;
end;
end;
end;
usage
ContainsSameValues<Integer>(lDict1.Keys, lDict2.Keys);
Checking for equality of a unordered dictionaries is a relatively simple algorithm. I will outline it here. Suppose we have two dictionaries, A and B.
Compare the number of elements of A and B. If this differs, the dictionaries are not equal.
Enumerate each key/value pair k,v in A. If k is not in B, or B[k] is not equal to v, then the dictionaries are not equal.
If you reach the end of the enumeration, then you know that the dictionaries are equal.

How to set the value of an enum type?

I have the following:
TDirection = (dirNorth, dirEast, dirSouth, dirWest);
TDirections = set of TDirection;
In a seperate class I have it declared as a property:
property Directions: TDirections read FDirections write FDirections;
What I want is to be able to treat them as if they were Booleans, so for example if dirNorth was True then it would be 1, if False it would be 0.
I am trying to imagine it as (1,0,0,0)
I think to check if a direction is True, I could use:
var
IsTrue: Boolean;
begin
IsTrue := (DirNorth in Directions);
Not sure if the above is correct or not, but then my other problem is how to change one of the directions to True or False?
I have now reached one of my confusion states :(
This is the last thing I tried to set the value but I am getting Illegal Expression (in Lazarus).
Directions(TDirection(DirNorth)) := True;
Directions is a set of elements of type TDirection.
To see if it contains dirNorth, do dirNorth in Directions. The result of using the in operator is a boolean; dirNorth in Directions is true iff the set Directions contains the element dirNorth.
To make sure dirNorth is included in Directions, do Directions := Directions + [dirNorth].
To make sure dirNorth is not included in Directions, do Directions := Directions - [dirNorth].
To set Directions to a particular value, simply assign: Directions := [dirNorth, dirSouth].
Formally, + computes the union of two sets; - computes the set difference of two sets. * computes the intersection of the two operands.
You also have the nice Include and Exclude functions: Include(Directions, dirNorth) does the same thing as Directions := Directions + [dirNorth]; Exclude(Directions, dirNorth) does the same thing as Directions := Directions - [dirNorth].
For example, if
type
TAnimal = (aDog, aCat, aRat, aRabbit);
TAnimalSet = set of TAnimal;
const
MyAnimals = [aDog, aRat, aRabbit];
YourAnimals = [aDog, aCat];
then
aDog in MyAnimals = true;
aCat in MyAnimals = false;
aRat in YourAnimals = false;
aCat in YourAnimals = true;
MyAnimals + YourAnimals = [aDog, aRat, aRabbit, aCat];
MyAnimals - YourAnimals = [aRat, aRabbit];
MyAnimals * YourAnimals = [aDog];
Implicit in my answer is the fact that the Delphi set type is modelled after the mathematical set. For more information about the Delphi set type, please refer to the official documentation.
You may add an item to a set by doing like this:
Include(Directions, dirNorth);
To remove it from the set:
Exclude(Diretions, dirNorth);
The help states that the result is the same as using the plus operator, but the code is more efficient.
Based on this helper, which doesn't work for properties, I created this one (requires XE6) - it can be used for variables and properties:
TGridOptionsHelper = record helper for TGridOptions
public
/// <summary>Sets a set element based on a Boolean value</summary>
/// <example>
/// with MyGrid do Options:= Options.SetOption(goEditing, False);
/// MyVariable.SetOption(goEditing, True);
/// </example>
function SetOption(GridOption: TGridOption; const Value: Boolean): TGridOptions;
end;
function TGridOptionsHelper.SetOption(
GridOption: TGridOption; const Value: Boolean): TGridOptions;
begin
if Value then Include(Self, GridOption) else Exclude(Self, GridOption);
Result:= Self;
end;

WinAPI: GetFontUnicodeRanges - I do not understand the result

I am trying to get Unicode font glyph ranges (Delphi 6):
var GS:PGlyphSet;
GSSize:LongWord;
rng:TWCRange;
begin
GSSize := GetFontUnicodeRanges(Canvas.Handle, nil);
GetMem(Pointer(GS), GSSize);
try
GS.cbThis:=GSSize;
GS.flAccel:=0;
GS.cGlyphsSupported:=0;
GS.cRanges:=0;
if GetFontUnicodeRanges(Canvas.Handle, GS)<>0 then begin
for i:=0 to GS.cRanges-1 do begin
rng := GS.ranges[i];
The strange thing is that Length(GS.ranges) is 1, but GS.cRanges is 309 and when I try to access the second range GS.ranges[1] I get, of course, a range check error. Before I turned range checking on it has worked in some magical way.
Types for reference (from Windows module):
PWCRange = ^TWCRange;
{$EXTERNALSYM tagWCRANGE}
tagWCRANGE = packed record
wcLow: WCHAR;
cGlyphs: SHORT;
end;
TWCRange = tagWCRANGE;
PGlyphSet = ^TGlyphSet;
{$EXTERNALSYM tagGLYPHSET}
tagGLYPHSET = packed record
cbThis: DWORD;
flAccel: DWORD;
cGlyphsSupported: DWORD;
cRanges: DWORD;
ranges: array[0..0] of TWCRange;
end;
TGlyphSet = tagGLYPHSET;
This struct makes use of the so-called struct hack:
http://c-faq.com/struct/structhack.html
http://tonywearme.wordpress.com/2011/07/26/c-struct-hack/
The ranges member is a variable length array, placed inline in the struct. But you cannot actually encode that in a static C type. That's why you call the function to find out how much memory to allocate, and then heap allocate the struct. If you allocated it on the stack, or using SizeOf(...) then the struct would be too small.
The simplest thing to do is to disable range checking for the code that accesses ranges. Although the type declaration says that only 0 is a valid index for ranges, in fact 0..cRanges-1 are valid.
If you don't want to disable range checking for the relevant code, then take a pointer the element 0, and then use pointer arithmetic in your loop.
var
rng: PWCRange;
....
rng := #GS.ranges[0];
for i:=0 to GS.cRanges-1 do begin
// use rng^
inc(rng);
end;
This is, in my view, the cleanest way to write code for sequential access. For random access, and with range checking in force, you'd be compelled to declare some extra types to defeat range checking:
type
TWCRangeArray = array [0..(MaxInt div SizeOf(TWCRange))-1] of TWCRange;
PWCRangeArray = ^TWCRangeArray;
And then use type casting to access individual elements:
rng := PWCRangeArray(#GS.ranges)[i];

Delphi : copy column from one excel sheet to another

I need to find column (on each sheet excluding ProfitSheet) which first cell has specified value (it always exists and only one per sheet) and then copy column to another sheet. i.e. something like that:
for i := 2 to MsExcel.Sheets.Count do // sheets loop. Sheets[1] — ProfitSheet
begin
MsExcel.Sheets[i].Select;
for j := 1 to 15 do // columns loop to find "Profit column"
if MsExcel.Cells[1, j].Text = 'Profit' then
// copy j column, usually I use something like
// VariantArr := MsExcel.Range[MsExcel.Cells[1, j], MsExcel.Cells[299, j]].Value
// and then
// Range := MsExcel.Range[MsExcel.Cells[1,i], MsExcel.Cells[299, i]];
// Range.Value := VariantArr;
// but I don't know how to find last column's cell (where 299)
MsExcel.Sheets['ProfitSheet'].Select;
// copy col to the i column of ProfitSheet
end;

An array of pointers that point to the same array

I've read a piece of Delphi code like this :
sample1 = ARRAY[1..80] OF INTEGER;
psample =^sample1;
VAR
function :ARRAY[1..70] OF psample;
From my understanding, the programmer is trying to declare an array that contains 70 pointers and each pointer points to a sample1 array.
So when I write :
function[1]^[1] := 5;
function[1]^[2] := 10;
then :
function[n]^[1] := 5
function[n]^[2] := 10; ( n = 2 to 70)
Is that correct ?
Your code sample is lacking some information since you do not say how function is defined. This means that you cannot draw the conclusions that you attempt to draw.
Of course, since function is a reserved word in Pascal, that code could never even compile. I will assume now that the variable is called f.
Consider the following definitions:
type
sample1 = array [1..80] of integer;
psample = ^sample1;
var
f : array [1..70] of psample;
Here, sample1 and psample are types. sample1 is type describing an array of 80 integers. psample is a pointer to a sample1.
Next a variable named f is defined. It is an array of 70 psamples.
Now, before you can even consider what happens when you write f[1]^[1], we need to assign some values to the elements of f.
Suppose we did it like this:
var
sample: sample1;
...
for i := 1 to 70 do
f[i] := #sample;
Now it would be true that f[i]^[k] refers to the same integer as f[j]^[k] for all valid i and j. So when you write f[1]^[1] := 42 you are also assigning that value to f[2]^[1], f[3]^[1] and so on.
On the other hand you could do it like this:
var
samples: array [1..70] of sample1;
...
for i := 1 to 70 do
f[i] := #samples[i];
Now each f[i] pointer points to a distinct array in memory. In this case assigning f[1]^[1] := 42 does not modify the value of f[2]^[1] or any of the other values.
That is correct. You have 70 pointers, each pointing to an array of 80 integers.

Resources