S
S
Stanislav2019-04-22 11:22:17
PHP
Stanislav, 2019-04-22 11:22:17

PhpSpreadsheet: How can I format a cell so that it accepts text values ​​without truncating the "+" sign in them?

There is something like this code:

$sheet
  ->getStyleByColumnAndRow($column, $row)
  ->getNumberFormat()
  ->setFormatCode('@')
;
$sheet->setCellValueByColumnAndRow(
  $position,
  $row,
  $value
);

In theory, it should set the text format in the cell, and after that, if we set a value like "+5" in it, it should remain so. In practice, the plus sign is cut off. How to win it? I tried the General format, but with this format (General), the plus is cut off even if you check it manually in MS Excel itself. But the text format with such a check saves the plus, but not when we do it through the code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
U
Urvin, 2019-04-22
@lamo4ok

Looks like we'll have to fiddle with the DefaultValueBinder heir and assign it to cells.
Look - on the 63rd line of this, the initial "+" is directly cut off, no matter how you deal with cell types.

K
Kripto77, 2021-02-11
@Kripto77

Faced the same problem - the answer was in the
docs
, maybe it will be useful to someone given by code like this

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',  // координаты ячейки
    "01513789642",   // нужное значение строки
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);

A
Alexander Lykasov, 2019-04-22
@lykasov-aleksandr

As an option - to transfer values ​​prefixed with an apostrophe, i.e. instead "+5"of doing"'+5"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question