MDL-51603 dataformat: Fixed worksheet title bug

This commit is contained in:
Brendan Heywood 2016-04-21 09:25:28 +10:00 committed by David Monllao
parent 6ab41abd3e
commit 5b95105600
2 changed files with 25 additions and 4 deletions

View File

@ -46,5 +46,26 @@ class writer extends \core\dataformat\spout_base {
/** @var $spouttype */
protected $spouttype = \Box\Spout\Common\Type::XLSX;
/**
* Set the title of the worksheet inside a spreadsheet
*
* For some formats this will be ignored.
*
* @param string $title
*/
public function set_sheettitle($title) {
if (!$title) {
return;
}
// Replace any characters in the name that Excel cannot cope with.
$title = strtr(trim($title, "'"), '[]*/\?:', ' ');
// Shorten the title if necessary.
$title = \core_text::substr($title, 0, 31);
// After the substr, we might now have a single quote on the end.
$title = trim($title, "'");
$this->sheettitle = $title;
}
}

View File

@ -51,6 +51,10 @@ abstract class spout_base extends \core\dataformat\base {
$this->writer = \Box\Spout\Writer\WriterFactory::create($this->spouttype);
$filename = $this->filename . $this->get_extension();
$this->writer->openToBrowser($filename);
if ($this->sheettitle) {
$sheet = $this->writer->getCurrentSheet();
$sheet->setName($this->sheettitle);
}
}
/**
@ -64,11 +68,7 @@ abstract class spout_base extends \core\dataformat\base {
if (!$title) {
return;
}
$title = preg_replace('/[\\\\\/\\?\\*\\[\\]]/', '', $title);
$title = substr($title, 0, 31);
$this->sheettitle = $title;
$sheet = $this->writer->getCurrentSheet();
$sheet->setName($title);
}
/**