fpspreadsheet: Fix writing of defective UTF8 strings to xlsx. Related to issue #39045.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8569 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-10-19 14:08:53 +00:00
parent df2c182ee2
commit ab310b1165
2 changed files with 19 additions and 21 deletions

View File

@ -62,6 +62,7 @@ procedure DestroyTempStream(AStream: TStream);
implementation implementation
uses uses
LazUTF8,
(* (*
{$IF FPC_FULLVERSION >= 20701} {$IF FPC_FULLVERSION >= 20701}
zipper, zipper,
@ -162,7 +163,7 @@ begin
(Pos(''', AppoSt) = 1) or (Pos(''', AppoSt) = 1) or
(Pos('%', AppoSt) = 1) // % (Pos('%', AppoSt) = 1) // %
then begin then begin
//'&' is the first char of a special chat, it must not be converted //'&' is the first char of a special char, it must not be converted
Result := Result + AText[Idx]; Result := Result + AText[Idx];
end else begin end else begin
Result := Result + '&'; Result := Result + '&';
@ -174,21 +175,13 @@ begin
'''':Result := Result + '''; '''':Result := Result + ''';
'%': Result := Result + '%'; '%': Result := Result + '%';
#10: if ProcessLineEndings then #10: if ProcessLineEndings then
Result := Result + '
' else Result := Result + '
'
else
Result := Result + #10; Result := Result + #10;
#13: if ProcessLineEndings then #13: if ProcessLineEndings then
Result := Result + '
' else Result := Result + '
'
else
Result := Result + #13; Result := Result + #13;
{ this breaks multi-line labels in xlsx
#10: begin
Result := Result + '<br />';
if (idx < Length(AText)) and (AText[idx+1] = #13) then inc(idx);
end;
#13: begin
Result := Result + '<br />';
if (idx < Length(AText)) and (AText[idx+1] = #10) then inc(idx);
end;
}
else else
Result := Result + AText[Idx]; Result := Result + AText[Idx];
end; end;
@ -218,15 +211,21 @@ const
BOX = #$E2#$8E#$95; BOX = #$E2#$8E#$95;
var var
i: Integer; i: Integer;
P: PChar;
begin begin
Result := true; Result := true;
for i := Length(AText) downto 1 do
if (AText[i] < #32) and not (AText[i] in [#9, #10, #13]) then begin repeat
// Replace invalid character by box symbol P := PChar(AText);
Delete(AText, i, 1); i := FindInvalidUTF8CodePoint(P, Length(AText), true);
Insert(BOX, AText, i); if i >= 0 then
begin
Delete(AText, i+1, 1);
Insert(BOX, AText, i+1);
Result := false; Result := false;
end; end;
until (i < 0);
if ReplaceSpecialChars then if ReplaceSpecialChars then
AText := UTF8TextToXMLText(AText, ProcessLineEndings); AText := UTF8TextToXMLText(AText, ProcessLineEndings);
end; end;

View File

@ -7947,8 +7947,7 @@ begin
ResultingValue := AValue; ResultingValue := AValue;
{ Check for invalid characters } { Check for invalid characters }
txt := ResultingValue; if not ValidXMLText(ResultingValue) then
if not ValidXMLText(txt) then
Workbook.AddErrorMsg( Workbook.AddErrorMsg(
rsInvalidCharacterInCell, [ rsInvalidCharacterInCell, [
GetCellString(ARow, ACol) GetCellString(ARow, ACol)
@ -7959,7 +7958,7 @@ begin
// unformatted string // unformatted string
AppendToStream(FSSharedStrings, AppendToStream(FSSharedStrings,
'<si>' + '<si>' +
'<t xml:space="preserve">' + txt + '</t>' + '<t xml:space="preserve">' + ResultingValue + '</t>' +
'</si>') '</si>')
else else
begin begin