简体   繁体   English

phpspreadsheet如何更改分配给模板文件中图表的数据集

[英]phpspreadsheet how to change the data set assigned to a chart in a template file

I am reading in a template file that has a series of charts.我正在阅读一个包含一系列图表的模板文件。 The charts on sheet "Charts" are based on data sets from a second sheet "RawData".工作表“Charts”上的图表基于第二个工作表“RawData”中的数据集。 I am trying to change the data set/range that feeds the chart - my assumption is that simply changing the data set will auto expand/shrink the chart.我正在尝试更改为图表提供数据的数据集/范围 - 我的假设是简单地更改数据集将自动扩展/缩小图表。

In excel在 excel

From:
   headings: =RawData!$A$1:$H$1
   data      =RawData!$A$2:$H$2

To:
   headings: =RawData!$A$1:$M$1
   data      =RawData!$A$2:$M$2

I can find the chart I need to change, but I can't figure out how to get/set the dataSource of the chart.我可以找到我需要更改的图表,但我不知道如何获取/设置图表的数据源。 I think I should be able to extract it with getDataSource() but I can't figure out how to properly use the method:我想我应该可以用getDataSource()提取它,但我不知道如何正确使用该方法:

  foreach ($spreadsheet->getSheetByName("Chart Data")->getChartCollection() as $chart) {
    if ($chart->getTitle()->getCaptionText() == "Chart_12") {
      // "Chart_12" is a manually assigned chart title in the template file
      $chart->getDataSource() ;  <--- error: undefined method
      break ;
    }
  }

getDataSource() is a listed method in the documentation: getDataSource - hopefully someone can assist as this is driving me nuts. getDataSource()是文档中列出的方法: getDataSource - 希望有人可以提供帮助,因为这让我发疯。

I think there is no method to alter the existing charts in phpspreadsheet and I think it can not be feasible because new chart can be smaller then existing chart and currently it does not provide any functionality to remove charts.我认为没有方法可以更改phpspreadsheet中的现有图表,我认为这是不可行的,因为新图表可能比现有图表小,而且目前它不提供任何删除图表的功能。

So what you can do is you can remove the worksheet of charts dynamically so all charts will be removed and after that you can add new worksheet and add new charts on that new sheet.因此,您可以做的是动态删除图表工作表,以便删除所有图表,然后您可以添加新工作表并在该新工作表上添加新图表。

I have provided sample code below to remove sheet from the excel file and also add new sheet into that file:我在下面提供了示例代码以从 excel 文件中删除工作表并将新工作表添加到该文件中:

<?php

require_once(__DIR__ . '/vendor/autoload.php');

$mySpreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
    
// delete the default active sheet
$mySpreadsheet->removeSheetByIndex(0);
    
// Create "Sheet 1" as the first worksheet.
// https://phpspreadsheet.readthedocs.io/en/latest/topics/worksheets/adding-a-new-worksheet
$worksheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($mySpreadsheet, "Sheet 1");
$mySpreadsheet->addSheet($worksheet, 0); // add new sheet and then create new charts on it.

In addition to above code you can also use sample chart code provided officially by phpspreadsheet library itself: Sample Chart code by phpspreadsheet除了上述代码,您还可以使用 phpspreadsheet 库本身官方提供的示例图表代码: Sample Chart code by phpspreadsheet

You can make your solution by using both code the code of phpspreadsheet library sample chart and the sample code of add, remove sheet which I have given above.您可以通过使用phpspreadsheet库示例图表的代码和我上面给出的添加、删除工作表的示例代码来制作您的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM