PHPSpreadsheet Convert Cell Formula to plain text - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, September 12, 2023

PHPSpreadsheet Convert Cell Formula to plain text

I'm using PHPSpreadSheet to read some cells in a range and saving it to a CSV. Everything is great except when my script encounters a formula it renders the cells formula like so in the CSV:

=CONCAT(B27," RNA QC")

instead of:

ONDr641 RNA QC

There are only a few rows where there are formulas. Everything else is text.

I have read that the 3rd argument in the rangeToArray function allows formulas be calculated (the equivalent of getCalculatedValue() for each cell). If I set that to true it stills renders it wrong. I've tried setting the explicit value and that did not work either.

How can I fix this? Any advice - I would most appreciate it!!

while ($data_detected) {
        $start_cell = "B" .
        strval(($start_row + ($chunk_size * ($pass - 1))));
        $end_cell = "AA" .
        strval(($start_row - 1) + ($chunk_size * $pass));
        
        $cell_range = $start_cell . ":" . $end_cell;


        $sheet_data = $spreadsheet->getActiveSheet()->rangeToArray(
                        $cell_range, null, false, false, true);

        $chunk_has_data = False;

        foreach ($sheet_data as $row_number => $row) {
            // Filter out rows that are entirely blank. If even a single
            // element in a row has data, then the row will be included...
            if (array_filter($row)) {
                // Set a flag that we have detected data in this pass.
                $chunk_has_data = true;

                foreach ($headers as $column_letter => $column_name) {
                    $row[$column_name] = $row[$column_letter];

                    unset($row[$column_letter]);
                }

                // Save the row number so that we can refer to it later.
                $row['row_number'] = $row_number;
                array_push($rows_with_data, $row);

            } else {
                // Row was empty
                $chunk_has_data = False;
            }
        }



source https://stackoverflow.com/questions/77083951/phpspreadsheet-convert-cell-formula-to-plain-text

No comments:

Post a Comment