Recent

Author Topic: Can copy area ?  (Read 504 times)

dodgex

  • Newbie
  • Posts: 3
Can copy area ?
« on: May 12, 2025, 12:08:26 pm »
Does it support copy an area and insert it on another row?
like this by ole:

    ExcelApp.ActiveSheet.Rows['1:10' ].Select;
    ExcelApp.Selection.Copy;
    ExcelApp.ActiveSheet.Rows['11:11'].Select;
    ExcelApp.Selection.Insert;
   

wp

  • Hero Member
  • *****
  • Posts: 12871
Re: Can copy area ?
« Reply #1 on: May 12, 2025, 05:58:31 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   fpSpreadsheet, xlsxOOXML;
  4. var
  5.   wBook: TsWorkbook;
  6.   wSheet: TsWorksheet;
  7.   r, c: Cardinal;
  8. begin
  9.   wBook := TsWorkbook.Create;
  10.   wSheet := wBook.AddWorksheet('Sheet 1');
  11.  
  12.   // Populate work sheet
  13.   for r := 0 to 10 do
  14.     for c := 0 to 5 do
  15.       wSheet.WriteNumber(r, c, 100*r + c);
  16.  
  17.   // Copy range C3:D6 to M1:N5
  18.   for r := 2 to 5 do
  19.     for c := 2 to 3 do
  20.       wSheet.CopyCell(r, c, r-2, c+10);
  21.  
  22.   wBook.WriteToFile('test.xlsx', true);
  23.   wBook.Free;
  24. end.

dodgex

  • Newbie
  • Posts: 3
Re: Can copy area ?
« Reply #2 on: May 13, 2025, 11:49:54 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   fpSpreadsheet, xlsxOOXML;
  4. var
  5.   wBook: TsWorkbook;
  6.   wSheet: TsWorksheet;
  7.   r, c: Cardinal;
  8. begin
  9.   wBook := TsWorkbook.Create;
  10.   wSheet := wBook.AddWorksheet('Sheet 1');
  11.  
  12.   // Populate work sheet
  13.   for r := 0 to 10 do
  14.     for c := 0 to 5 do
  15.       wSheet.WriteNumber(r, c, 100*r + c);
  16.  
  17.   // Copy range C3:D6 to M1:N5
  18.   for r := 2 to 5 do
  19.     for c := 2 to 3 do
  20.       wSheet.CopyCell(r, c, r-2, c+10);
  21.  
  22.   wBook.WriteToFile('test.xlsx', true);
  23.   wBook.Free;
  24. end.

Thank you for your help,
But this is just copying the values of cells. Can we copy the values and styles within one range and copy them in another row?

wp

  • Hero Member
  • *****
  • Posts: 12871
Re: Can copy area ?
« Reply #3 on: May 13, 2025, 01:06:25 pm »
But this is just copying the values of cells. Can we copy the values and styles within one range and copy them in another row?
No, it copies also formulas and cell formatting:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   Sysutils, fpSpreadsheet, fpsTypes, xlsxOOXML;
  4.  
  5. function RandomBrightColor: DWord;
  6. var
  7.   R, G, B: Byte;
  8. begin
  9.   R := $FF - Random($33);
  10.   G := $FF - Random($33);
  11.   B := $FF - Random($33);
  12.   Result := R + G shl 8 + b shl 16;
  13. end;
  14.  
  15. var
  16.   wBook: TsWorkbook;
  17.   wSheet: TsWorksheet;
  18.   cell: PCell;
  19.   r, c: Cardinal;
  20. begin
  21.   RandSeed := 1;
  22.  
  23.   wBook := TsWorkbook.Create;
  24.   wSheet := wBook.AddWorksheet('Sheet 1');
  25.  
  26.   // Populate work sheet
  27.   for r := 0 to 10 do
  28.     for c := 0 to 5 do
  29.     begin
  30.       if c = 3 then
  31.         // Write a formula
  32.         cell := wSheet.WriteFormula(r, c, '=Row(A' + IntToStr(r+1)+ ')')
  33.       else
  34.         // Write constant values
  35.         cell := wSheet.WriteNumber(r, c, 100*r + c);
  36.       // Write some random formatting
  37.       wSheet.WriteBackgroundColor(cell, RandomBrightColor);
  38.       if Random(2) =0 then
  39.         wSheet.WriteFontStyle(cell, [fssBold, fssItalic]);
  40.     end;
  41.  
  42.   // Copy range C3:D6 to M1:N5
  43.   for r := 2 to 5 do
  44.     for c := 2 to 3 do
  45.       wSheet.CopyCell(r, c, r-2, c+10);
  46.  
  47.   wBook.WriteToFile('test.xlsx', true);
  48.   wBook.Free;
  49. end.

dodgex

  • Newbie
  • Posts: 3
Re: Can copy area ?
« Reply #4 on: May 14, 2025, 05:19:05 am »
But this is just copying the values of cells. Can we copy the values and styles within one range and copy them in another row?
No, it copies also formulas and cell formatting:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   Sysutils, fpSpreadsheet, fpsTypes, xlsxOOXML;
  4.  
  5. function RandomBrightColor: DWord;
  6. var
  7.   R, G, B: Byte;
  8. begin
  9.   R := $FF - Random($33);
  10.   G := $FF - Random($33);
  11.   B := $FF - Random($33);
  12.   Result := R + G shl 8 + b shl 16;
  13. end;
  14.  
  15. var
  16.   wBook: TsWorkbook;
  17.   wSheet: TsWorksheet;
  18.   cell: PCell;
  19.   r, c: Cardinal;
  20. begin
  21.   RandSeed := 1;
  22.  
  23.   wBook := TsWorkbook.Create;
  24.   wSheet := wBook.AddWorksheet('Sheet 1');
  25.  
  26.   // Populate work sheet
  27.   for r := 0 to 10 do
  28.     for c := 0 to 5 do
  29.     begin
  30.       if c = 3 then
  31.         // Write a formula
  32.         cell := wSheet.WriteFormula(r, c, '=Row(A' + IntToStr(r+1)+ ')')
  33.       else
  34.         // Write constant values
  35.         cell := wSheet.WriteNumber(r, c, 100*r + c);
  36.       // Write some random formatting
  37.       wSheet.WriteBackgroundColor(cell, RandomBrightColor);
  38.       if Random(2) =0 then
  39.         wSheet.WriteFontStyle(cell, [fssBold, fssItalic]);
  40.     end;
  41.  
  42.   // Copy range C3:D6 to M1:N5
  43.   for r := 2 to 5 do
  44.     for c := 2 to 3 do
  45.       wSheet.CopyCell(r, c, r-2, c+10);
  46.  
  47.   wBook.WriteToFile('test.xlsx', true);
  48.   wBook.Free;
  49. end.

Thank you, I'll give it a try

 

TinyPortal © 2005-2018