unit SheetSelectFrm;

interface

uses
  SMXLS, ZjCells, SMCells,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CheckLst, ZJGrid;

type
  TSheetSelectForm = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    zgExcelSheets: TZJGrid;
    procedure zgExcelSheetsCellCanEdit(Sender: TObject;
      const ACoord: TPoint; var Allow: Boolean);
    procedure zgExcelSheetsSetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String; DisplayText: Boolean);
    procedure zgExcelSheetsGetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  private
    FExcel: TMSExcel;
    FIsMulit: Boolean;
    FSelectRow: Integer;
    
    procedure InitGrid(AExcel: TMSExcel);
    procedure SetIsMulit(const Value: Boolean);
  public
    constructor Create(AExcel: TMSExcel);

    procedure AssignResult(ASelects: TList);
    function SelectResult: TSpreadSheet;

    // 可否多选
    property IsMulti: Boolean read FIsMulit write SetIsMulit;
  end;

function SelectSheets(AExcel: TMSExcel; ASelects: TList): Boolean;
function SelectSheet(AExcel: TMSExcel; var ASheet: TSpreadSheet): Boolean;

implementation

{$R *.dfm}

function SelectSheets(AExcel: TMSExcel; ASelects: TList): Boolean;
var
  SelectForm: TSheetSelectForm;
begin
  SelectForm := TSheetSelectForm.Create(AExcel);
  try
    SelectForm.IsMulti := True;
    Result := SelectForm.ShowModal = mrOk;
    if Result then
      SelectForm.AssignResult(ASelects);
  finally
    SelectForm.Free;
  end;
end;

function SelectSheet(AExcel: TMSExcel; var ASheet: TSpreadSheet): Boolean;
var
  SelectForm: TSheetSelectForm;
begin
  SelectForm := TSheetSelectForm.Create(AExcel);
  try
    SelectForm.IsMulti := False;
    Result := SelectForm.ShowModal = mrOk;
    if Result then
      ASheet := SelectForm.SelectResult;
  finally
    SelectForm.Free;
  end;
end;

{ TSheetSelectForm }

procedure TSheetSelectForm.AssignResult(ASelects: TList);
var
  i: Integer;
begin
  ASelects.Clear;
  for i := 1 to zgExcelSheets.RowCount - 1 do
    if zgExcelSheets.Cells[1, i].Text = 'True' then
      ASelects.Add(zgExcelSheets.Rows[i].Data);
end;

constructor TSheetSelectForm.Create(AExcel: TMSExcel);
begin
  inherited Create(nil);
  InitGrid(AExcel);
  FSelectRow := 1;
end;

procedure TSheetSelectForm.InitGrid(AExcel: TMSExcel);
var
  i: Integer;
begin
  zgExcelSheets.RowCount := 1;
  zgExcelSheets.Cells[1, 0].Text := '选择';
  zgExcelSheets.CellClass.Cols[1] := TZjCheckBoxCell;
  zgExcelSheets.Cells[2, 0].Text := '工作表';
  zgExcelSheets.ColWidths[2] := 200;

  zgExcelSheets.RowCount := AExcel.Sheets.Count + 1;
  for i := 0 to AExcel.SheetNames.Count - 1 do
  begin
    zgExcelSheets.Cells[2, i+1].Text := AExcel.SheetNames.Strings[i];
    zgExcelSheets.Rows[i+1].Data := AExcel.Sheets.Spreadsheet(i);
  end;
end;

function TSheetSelectForm.SelectResult: TSpreadSheet;
var
  i: Integer;
begin
  for i := 1 to zgExcelSheets.RowCount - 1 do
    if zgExcelSheets.Cells[1, i].Text = 'True' then
    begin
      Result := TSpreadSheet(zgExcelSheets.Rows[i].Data);
      Break;
    end;
end;

procedure TSheetSelectForm.zgExcelSheetsCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
begin
  Allow := (ACoord.X = 1) and (ACoord.Y > 0);
end;

procedure TSheetSelectForm.zgExcelSheetsSetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
begin
  if Value = 'True' then
    FSelectRow := ACoord.Y
  else
    FSelectRow := -1;
  zgExcelSheets.Invalidate;
end;

procedure TSheetSelectForm.zgExcelSheetsGetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
begin
  if (ACoord.X = 1) and (ACoord.Y > 0) then
  begin
    if FSelectRow = ACoord.Y then
      Value := 'True'
    else
      Value := 'False';
  end;
end;

procedure TSheetSelectForm.SetIsMulit(const Value: Boolean);
begin
  FIsMulit := Value;
  if not FIsMulit then
  begin
    zgExcelSheets.OnGetCellText := zgExcelSheetsGetCellText;
    zgExcelSheets.OnSetCellText := zgExcelSheetsSetCellText;
  end;
end;

end.
