-->

OpenTBS/PHP - Merge content into embedded workshee

2019-08-29 01:58发布

问题:

How do I properly fill an embedded worksheet of a PowerPoint template with data using OpenTBS, where the worksheet is embedded inside of a PowerPoint presentation?

I used sub files but the embedded worksheet retains the input tags and no replaced values.

Excel Template that merges properly as a standalone xls file, but not when embedded in a presentation (PPTX).

+------------------------------+---------------------------------+
|                              |    [c.key;block=tbs:cell]       |
+------------------------------+---------------------------------+
|       [r.#;block=tbs:row]    |    [cell.val;block=tbs:cell]    |
+------------------------------+---------------------------------+

PHP Merge Code

$template = 'riskwaterfalltemplate.pptm';
$TBS->LoadTemplate($template.'#ppt/embeddings/Microsoft_Excel_Worksheet2.xlsx',  OPENTBS_ALREADY_UTF8);


// -----------------
// Output the result
// -----------------
$nbr_row = 5;
$nbr_col = 5;
// List of column's names
$columns = array();
for ($col=1; $col <= $nbr_col; $col++)
{
    $columns[$col]['key'] = $col;
}





$data = array();
$record = array();

for ($col=1; $col <= $nbr_col; $col++)
{
    $record[$col]['val'] = 1;
}
for ($row=0; $row < $nbr_row; $row++)
{

    $data[$row] = $record;
}

// Expanding columns
$TBS->MergeBlock('c',$columns);

//Expanding Cells
$TBS->MergeBlock('cell', $record);

// Merging rows
$TBS->MergeBlock('r',$data);

$TBS->Show();

Debug Output

* OPENTBS DEBUG MODE: if the star, (*) on the left before the word OPENTBS, is not the very first character of this page, then your
merged Document will be corrupted when you use the OPENTBS_DOWNLOAD option. If there is a PHP error message, then you have to fix it.
If they are blank spaces, line beaks, or other unexpected characters, then you have to check your code in order to avoid them.

------------------------------
INFORMATION
------------------------------
* Debug command: OPENTBS_DEBUG_XML_CURRENT
* OpenTBS version: 1.9.9
* TinyButStrong version: 3.10.1
* PHP version: 5.6.25YES
* Opened document: riskwaterfalltemplate.pptm
* Activated features for document type: openxml/pptx
* Deleted files in the archive: none
* Added files in the archive: none
* Modified files in the archive:
  - ppt/embeddings/Microsoft_Excel_Worksheet2.xlsx

------------------------------
File merged with OpenTBS: ppt/embeddings/Microsoft_Excel_Worksheet2.xlsx

回答1:

If you are merging the embedded XLSX in order to have a chart modified in the PPTX Presentation, this will not work.

In both Ms Word ans Ms PowerPoint, embedded XLSX attached to chart does not contains the true data of the chart. It is a simple copy that helps the user for editing the data using Ms Excel. The true data are stored in the XML sub-file related to the chart.

If you want to edit the chart, use command OPENTBS_CHART.

If you actually want to merge the embedded XLSX file, your code is not working because the sub-file ppt/embeddings/Microsoft_Excel_Worksheet2.xlsx is not an XML file, it is a binary file. So TBS cannot merge anything in it directly.

The solution is to:

  1. load the PPTX template using $TBS->LoadTemplate('my_presentation.pptx'),
  2. the load the ppt/embeddings/Microsoft_Excel_Worksheet2.xlsx sub-file as the current sub-file using $TBS->PlugIn(OPENTBS_SELECT_FILE, $SubFile)
  3. save the binary of the XLSX sub-file in a temp file using $handle = tmpfile(); fwrite($handle, $TBS->Source);
  4. open a new TBS instance for this temp file using $TBS2->LoadTemplate($handle);
  5. do your data merging in the XLSX with $TBS2,
  6. finalize the XLSX using $TBS2->Show(OPENTBS_STRING);
  7. save the result in the PPTX using $TBS->Source = $TBS2->Source;
  8. finalize the PPTX using $TBS->Show(...);