MDL-36845 excel export: don't output invalid sheet names.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 27 Nov 2012 16:36:35 +0000 (16:36 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 30 Nov 2012 15:52:54 +0000 (15:52 +0000)
We should clean any proposed worksheet name, to remove the characters
that Excel does not allow.

lib/excellib.class.php
lib/tablelib.php

index 44315ca..69205d9 100644 (file)
@@ -74,8 +74,8 @@ class MoodleExcelWorkbook {
      * @param string $name Name of the sheet
      * @return object MoodleExcelWorksheet
      */
-    function &add_worksheet($name = '') {
-    /// Create the Moodle Worksheet. Returns one pointer to it
+    function add_worksheet($name = '') {
+        // Create the Moodle Worksheet. Returns one pointer to it
         $ws = new MoodleExcelWorksheet ($name, $this->pear_excel_workbook, $this->latin_output);
         return $ws;
     }
@@ -138,6 +138,9 @@ class MoodleExcelWorksheet {
      */
     function MoodleExcelWorksheet($name, &$workbook, $latin_output=false) {
 
+        // Replace any characters in the name that Excel cannot cope with.
+        $name = strtr($name, '[]*/\?:', '       ');
+
         if (strlen($name) > 31) {
             // Excel does not seem able to cope with sheet names > 31 chars.
             // With $latin_output = false, it does not cope at all.
index 2a08616..e1a17cb 100644 (file)
@@ -1487,7 +1487,7 @@ class table_spreadsheet_export_format_parent extends table_default_export_format
     }
 
     function start_table($sheettitle) {
-        $this->worksheet =& $this->workbook->add_worksheet($sheettitle);
+        $this->worksheet = $this->workbook->add_worksheet($sheettitle);
         $this->rownum=0;
     }