Skip to content

Commit

Permalink
MDL-51603 dataformat: Fixed worksheet title bug
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood authored and David Monllao committed Apr 21, 2016
1 parent 6ab41ab commit 5b95105
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions dataformat/excel/classes/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

8 changes: 4 additions & 4 deletions lib/classes/dataformat/spout_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function send_http_headers() {
$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);
}
}

/**
Expand All @@ -64,11 +68,7 @@ public function set_sheettitle($title) {
if (!$title) {
return;
}
$title = preg_replace('/[\\\\\/\\?\\*\\[\\]]/', '', $title);
$title = substr($title, 0, 31);
$this->sheettitle = $title;
$sheet = $this->writer->getCurrentSheet();
$sheet->setName($title);
}

/**
Expand Down

0 comments on commit 5b95105

Please sign in to comment.