1

As you know in Excel column names are letters. When it reaches Z it continues with AA-AB-AC. Is it possible to make a similar function in Delphi XE7 + for loop?

I've tried:

var
i:integer;
str:string;
begin
str:='a';
for i := 0 to 26-1 do
begin
inc (str,1);
memo1.Lines.Add(str);
end;

but it returned:

[dcc32 Error] FBarkodsuzIndesignVerisiOlustur.pas(249): E2064 Left side cannot be assigned to

I assume that's because str is not an integer.

I can convert numbers to letters with this function:

function numberToString(number: Integer): String;
begin
    Result := '';
    if (number < 1) or (number > 26) then
        Exit;

    Result := 'abcdefghijklmnopqrstuvwxyz'[number];
end;

But I have no idea how we can create letters like AA when it exceeds 26.

Also with below approach, it creates 26 letters just fine but when it exceeds 26 it starts to use characters like brackets:

  for i := 0 to 27-1 do
  begin
   memo1.Lines.Add(Char(Ord('a') + i));
  end;

Output of it:

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{

when it reach to Z it'll continue as "AA" "BB" "CC" and so on like Excel creates column names.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
Sheshman
  • 88
  • 3
  • 10
  • Use div and mod. It's just base 26 counting. Think about how you would implement IntToStr, which is base 10, and use the same algo but with a 26 instead. Oh, and make life easier by using zero based indices. – David Heffernan Oct 07 '20 at 07:02
  • @DavidHeffernan ok should i create a loop like; for i := 0 to 52-1 do begin if (I mod 26 = 0) and (I > 0) then begin memo1.Lines.Add(Char(Ord('a') + i)); end; end; – Sheshman Oct 07 '20 at 07:22
  • Stop thinking about this as being about loops. You know how to write a for loop. You don't know how to convert an index into a string column name. Write a function to do that. Once you have done that, call it from a loop. Programming 101 here, break tasks into smaller fundamental tasks that can be easily composed. – David Heffernan Oct 07 '20 at 07:29
  • @DavidHeffernan yeah you are right about functions, well i'm a rookie and working on it. – Sheshman Oct 07 '20 at 08:04

1 Answers1

3

This is the function that I use for the task.

function SpreadSheetColName(const Col: Integer): string;
var
  c: Char;
begin
  Assert(Col >= 0);
  if Col<26 then begin
    c := 'A';
    Inc(c, Col);
    Result := c;
  end else begin
    Result := SpreadSheetColName(Col div 26 - 1) + SpreadSheetColName(Col mod 26);
  end;
end;

Note that it uses zero based indices. I would suggest that you also use zero based indices as a general rule throughout your programming.

If you can't bring yourself to do that, then a one based version would look like this:

function SpreadSheetColName(const Col: Integer): string;

  function SpreadSheetColNameZeroBased(const Col: Integer): string;
  var
    c: Char;
  begin
    Assert(Col >= 0);
    if Col<26 then begin
      c := 'A';
      Inc(c, Col);
      Result := c;
    end else begin
      Result := SpreadSheetColNameZeroBased(Col div 26 - 1) + SpreadSheetColNameZeroBased(Col mod 26);
    end;
  end;

begin
  Result := SpreadSheetColNameZeroBased(Col - 1);
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • just simply amazing, thanks for your help, one last question tho, in php we are using % to define multiples, what is the equal for delphi, i mean imagine you are counting from 0 to 50 and you want to show message on every 2-4-6-8-10...., i thought i can use ODD or EVEN like if odd(i) then do something, is that the right approach? – Sheshman Oct 07 '20 at 08:48
  • % maps to mod in Delphi. It's the very first thing I said to you in my first comment. Please don't take the functions in my answer and use them without fully understanding them. Use the Delphi documentation to learn how they work. – David Heffernan Oct 07 '20 at 08:49
  • yes i get that and believe me i'll not use your function in any of my projects because i'm not able to create an project by myself yet, i'm good at php but learning Delphi and i:'m at the very beginning of it, i'm re-creating some of my php project in Delphi to learn logic and methods, so when i read your comment about mod and div, quickly searched delphi how to use mod, delphi how to use div and started to read examples and articles, on the previous comment i was wondering what would be the different/alternate approach to mod, that's why i asked. – Sheshman Oct 07 '20 at 08:53
  • I don't think that there is an alternative to `mod` to perform the same task. For sure you can use the `Odd` function in the Delphi RTL, but that is just a shorthand for `mod 2`. And `%` does not "define multiples". It's the modulus operator, it computes the remainder of a division. – David Heffernan Oct 07 '20 at 09:02
  • 1
    oh i see, i said multiples because of my poor english :) correct term was modulus you are right :) Well, you thought me good subjects to study today if i ever met you one day i owe you a cold beer, for i :=0 to 999-1 then begin memo1.lines.add(Thank you kind sir); end; – Sheshman Oct 07 '20 at 09:07
  • 1
    also thanks to mod "Andreas Rejbrand" who edited and corrected my poor English writing skills :) – Sheshman Oct 07 '20 at 09:09