Skip to content

Empty string cells produce invalid <v>-1</v> reference, breaking sharedStrings lookup #6

@jasper-hsia

Description

@jasper-hsia

Bug Description

When a cell is set with an empty string via AsString := '', the generated
sheet1.xml contains <v>-1</v>, which is an invalid sharedStrings index.
Excel reports "已移除的記錄: /xl/worksheets/sheet1.xml 部分的 儲存格資訊"
(Removed records: cell information from sheet1.xml) and silently drops the cells.

Root Cause

In Office4D.Excel.Workbook.pas:

BuildSharedStrings (line 742) intentionally excludes empty strings:

if (Cell.IsString) and (Cell.GetAsString <> '') then
  if not Result.Contains(Cell.GetAsString) then
    Result.Add(Cell.GetAsString);

But GenerateSheet (line 951) still calls GetSharedStringIndex for empty
strings without checking, so IndexOf returns -1:

const StrIdx = GetSharedStringIndex(SharedStrings, Cell.GetAsString);
SB.Append('<c r="' + CellPair.Key + '"' + StyleAttr +
          ' t="s"><v>' + IntToStr(StrIdx) + '</v></c>');

Attached File

I've attached C:\Temp\TfrmRpcReqM\xl\worksheets\sheet1.xml (extracted from the broken .xlsx).
sheet1.xml
In this file, find -1 to see:

<c r="E10" t="s"><v>-1</v></c>
<c r="F10" t="s"><v>-1</v></c>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions