Fast Report - Get CSV Data from Memo Field

From eStream Software
Revision as of 02:08, 20 April 2018 by Twfaung (talk | contribs) (→‎Steps)

Introduction

Below is example on how to get CSV data from Maintain Agent UDF_AList field

ProjectCode;Remark
P12W5;Rmakr 1
P13W4;Note 1234

Steps

01. Enter below script
Get CSV Data Script
function SetStrictDelimiter(const AStr:String):string;
var i, x, ALength  : integer;
    s, s1 : string;
    OrigList, MasterList, Row : TStringList;                              
begin
  OrigList   := TStringList.Create;      
  MasterList := TStringList.Create;
  Row        := TStringList.Create;
  Row.Delimiter := ';';
  try
    OrigList.Text := RichTextToPlainText(AStr);                                    
    for I := 0 to OrigList.Count - 1 do
      begin
        Row.Clear;
        s1 := '';
        x := 1;
        s := OrigList[I];
        ALength := Length(s);
        while (x <= ALength) do begin
          if x = ALength then begin
            s1 := s1 + s[x];
            Row.Add(Trim(s1));
          end;
          if (s[x] = ';') then begin
              Row.Add(Trim(s1));
              s1 := '';
            end else
             s1 := s1 + s[x];           
           inc(x);
        end;
      MasterList.Add(Row.DelimitedText);
    end;
    Result := MasterList.Text;                                          
  finally
    OrigList.Free;                  
    MasterList.Free;
    Row.Free;  
  end;            
end;          
  
function ValueOfSemiColonStrEx(const AStr: string; const AIndex: integer): string;
var S: TStringList;
begin
  S := TStringList.Create;
  try
    S.Delimiter       := ';';
    S.DelimitedText   := AStr;
    if AIndex < S.Count then Result := S[AIndex]
    else Result := '';
  finally
    S.Free;
  end;
end;          

function GetAgentData(const AProject:String):String;
var S: TStringList;
    i: integer;                         
begin
  S := TStringList.Create;
  try        
    S.Text := SetStrictDelimiter(<Document_Agent."UDF_AList">);    
    for I := 0 to S.Count - 1 do begin
      if ValueOfSemiColonStrEx(S[i], 0) = AProject then
        Result := ValueOfSemiColonStrEx(S[i], 1);          
    end;    
  finally
    s.Free;              
  end;            
end;
02.