简体   繁体   中英

Change charts title by PhpOffice\PhpSpreadsheet in template

I try to change chart title in.xlsx template

I open template, change cels,write to output.

Q: How to change chart title....

$reader = IOFactory::createReader( 'Xlsx' );
$reader->setIncludeCharts( true );
$spreadsheet = $reader->load( storage_path( 'app/template.xlsx' ) );
$spreadsheet->getActiveSheet()->setCellValue( 'B3', 'Blabla' );

//create new from template
$writer = new Xlsx( $spreadsheet );
$writer->setPreCalculateFormulas( true );
$writer->setIncludeCharts( true );

##how to change title in all/first charts in template?

I finally found the method, just understand that the coded version of TITLE is backwards from what you visually see in the excel file. TITLE is like a hidden chart name, kind of like a named index (but not a real index), and the index is (seemingly) permanently assigned when its added to the spreadsheet the first time.

Meaning if you add three charts, in order they would be TITLE'd: chart1, chart2, chart3. If you came back and added a new chart between chart2 and chart3, that would be chart4 - the existing ones don't get renumbered/reordered.

Like wise, if you have five charts: char1, chart2, chart3, chart4, chart5 - and then delete chart3, the remaining don't get reordered/renumbered, they are now: chart1, chart2, chart4, chart5 . And if you added a new chart between 4 & 5, in this example, it would be: chart6. IE: chart1,chart2,chart4,chart6,chart5

But visually in the Excel file what is seen as the TITLE is the name you assign it IE: "Annual Growth Chart". But in the code, this visual name is represented as captionText . captionText "Annual Growth Chart" is a subset of Title "chart3".

So in order to find the chart you are looking to modify, you either must already know the chart index (ie: title) OR the chart visual title (ie: captionText).

Because of adding deleting moving charts, I found it easier, in the excel template, to add visual titles to your charts then search for those titles/captionText in your code. This way you don't have to have worry about tracking which chart is which index. Use the title/captionText to get the index number.

  foreach ($spreadsheet->getSheetByName("SheetName")->getChartCollection() as $chart) {
    // if you know TITLE of the chart
    // TITLE is the visible title in the chart
    // Easy to know if you set the titles yourself in the template chart
    // A template chart TITLE cannot be a cell reference, will cause an error on file import
    if ($chart->getTitle()->getCaptionText() == "Chart_12") {
      $curIndex = $chart->getTitle() ; // = "chart3"
      $curTitle = $chart->getTitle()->getCaptionText() ;  // "Chart_12"
      $chart->setTitle('chart10') ; // resets the index/object name
      $chart->setTitle()->setCaptionText("Quarterly Revenue Chart") ;
      break ;
    }

    // if you know the NAME (chart id) the chart
    // NAME is like the hidden chart index
    if ($chart->getName() == "chart12") {
      $chart->setName('chart100') ;  // rename the hidden name
      $chart->setTitle()->setCaptionText("New Chart Title") ;
      break ;
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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