Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xls writer, on save, sets selected cell in sheet to last parsed cell with formula and cannot set active sheet (selects multiple sheets when available). #1250

Closed
jimmy4o4 opened this issue Nov 22, 2019 · 1 comment
Labels

Comments

@jimmy4o4
Copy link
Contributor

Xls writer, on save, sets selected cell in sheet to last cell with formula and cannot set active sheet (selects multiple sheets when available).

This is:

- [x] a bug report
- [ ] a feature request
- [x] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

  1. On save() with Xls writer, selected cell(s) should remain selected and selection should not change.
  2. On save() with Xls writer, active sheet should remain active. Only one (active), not multiple sheets should be selected in output file.

What is the current behavior?

  1. On save() with Xls writer, cell(s) selection changes to last parsed sheet cell that contain formula;

This is caused by running \PhpOffice\PhpSpreadsheet\Cell\Cell::getStyle() method (in fact \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getStyle()) in \PhpOffice\PhpSpreadsheet\Calculation\Calculation::_calculateFormulaValue() method.

  1. On save() with Xls writer, output file has selected multiple sheets and active is not the one that was made active before save. Using LibreOffice Calc 6.3.2.2 x86 to open output file.

What are the steps to reproduce?

<?php

require_once 'vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$spreadsheet->setActiveSheetIndex(0)
    ->setTitle('Test1')
    ->setCellValue('D1', 1)
    ->setCellValue('D2', 2)
    ->setCellValue('D3', 3)
    ->setCellValue('D4', 4)
    ->setCellValue('D5', '=SUM(D1:D4)')
    ->setSelectedCell('A1');

$spreadsheet->createSheet(1);

$spreadsheet->setActiveSheetIndex(1)
    ->setTitle('Test2')
    ->setCellValue('D1', 4)
    ->setCellValue('E1', 3)
    ->setCellValue('F1', 2)
    ->setCellValue('G1', 1)
    ->setCellValue('H1', '=SUM(D1:G4)')
    ->setSelectedCell('A1');

$spreadsheet->setActiveSheetIndex(0);

echo $spreadsheet->getActiveSheetIndex() . PHP_EOL; // Prints 0
foreach ($spreadsheet->getAllSheets() as $sheet) {
    echo $sheet->getTitle() . ": " . $sheet->getActiveCell() . PHP_EOL;
    // Prints:
    // Test1: A1
    // Test2: A1
}

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');

$writer->save('Test.xls');

echo $spreadsheet->getActiveSheetIndex() . PHP_EOL; // Prints 1 (should be 0).
foreach ($spreadsheet->getAllSheets() as $sheet) {
    echo $sheet->getTitle() . ": " . $sheet->getActiveCell() . PHP_EOL;
    // Prints:
    // Test1: D5
    // Test2: H1
    // Should be:
    // Test1: A1
    // Test2: A1
}

// These changes (with wrong selected cells and sheets) has been saved to output file.

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet: since 1.7.0
PHP: 7.1.26 and 7.3.11 was used with the same result

@stale
Copy link

stale bot commented Jan 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Jan 21, 2020
@stale stale bot closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant