Giter Club home page Giter Club logo

Comments (5)

xaviermdq avatar xaviermdq commented on June 2, 2024 2

Without a modification or extension of the class, no. You have 2 approaches:

  • make $sheets property public and modify the sheet directly (by cell or adding an entire row also)
  • make a setter method
    I'm not an expert in OOP but I think the "correct" way is using a setter but I always have the temptation to directly modify the property.

If you prefer the first option, you must know that $sheets[S]['rows'] stores a bidimensional array with the data of the sheet S. S is an integer defining the sheet number (first sheet has S = 0). You can't reference sheet by name unless you make a method (or property) to convert from sheet name to sheet number. Also, as S, you can use curSheet property who holds the last sheet number added.

And, if you prefer a setter, the following code extends the class to allow cell modification and row adding. Also, an using example:

require_once 'simplexlsxgen.php';

class cfrrSimpleXLSXGen extends Shuchkin\SimpleXLSXGen
{
	protected $name2sheet;
	public function __construct()
	{
		parent::__construct();
		$name2sheet = [];
	}

	public function addSheet(array $rows, $name = null)
	{
		parent::addSheet($rows, $name);
		$this->name2sheet[$this->sheets[$this->curSheet]['name']] = $this->curSheet;
	}

	/*
	$cell : a string referencing the cell to modify in A1 notation (column letter / row number)
	$data : the cell data to modify
	$sheet : if type
		= null : the current (last) sheet
		= integer : the sheet number (X)
 		= string : the sheet name
	*/
	public function modifyCell($cell, $data, $sheet = null)
	{
		$x = $y = 0;
		$this->cell2coord($cell, $x, $y);
		if ($sheet === null) {
			$sheet = $this->curSheet;
		} elseif (is_string($sheet)) {
			if (!isset($this->name2sheet[$sheet]))
				return;
			$sheet = $this->name2sheet[$sheet];
		}
		if (!isset($this->sheets[$sheet]))
			return;
		$this->sheets[$sheet]['rows'][$y][$x] = $data;
	}

	/*
	$row : unidimensional array representing a row to add to the end of $sheet
	$sheet : if type
		= null : the current (last) sheet
		= integer : the sheet number (X)
		= string : the sheet name
	*/
	public function addRow($row, $sheet = null)
	{
		if ($sheet === null) {
			$sheet = $this->curSheet;
		} elseif (is_string($sheet)) {
			if (!isset($this->name2sheet[$sheet]))
				return;
			$sheet = $this->name2sheet[$sheet];
		}
		if (!isset($this->sheets[$sheet]))
			return;
		$this->sheets[$sheet]['rows'][] = $row;
	}
}

$xlsx = new cfrrSimpleXLSXGen();
//generate random data
$data = [];
for ($i=0; $i<10; $i++) {
	$data[] = [rand(0, 10), rand(0, 10), rand(0, 10), rand(0, 10)];
}
//add to ne sheet
$xlsx->addSheet($data, 'Test Sheet');
$xlsx->modifyCell('B4', 'Hello World!', 'Test Sheet');
$xlsx->saveAs('Test.xlsx');

NOTE:
You can modify $sheets property (with either approach) after calling addSheet and before calling __toString/saveAs/downloadAs methods.

from simplexlsxgen.

CFRR avatar CFRR commented on June 2, 2024

thanks for answering.
and if i want to open a existing excel file, change de value of the cell and save?

from simplexlsxgen.

xaviermdq avatar xaviermdq commented on June 2, 2024

This class generates xlsx files. You first need to parse the existing xlsx file with another library (for example: https://github.com/shuchkin/simplexlsx), modify data and generate xlsx again with this class. But, in this scenario, you don't need the modifications from my previous post because you modify the data before calling addSheet method.

from simplexlsxgen.

AP-JC avatar AP-JC commented on June 2, 2024

this would be a fantastic addition to the core code ... any plans to integrate? Would be great to be able to read a cell as well

from simplexlsxgen.

shuchkin avatar shuchkin commented on June 2, 2024

this would be a fantastic addition to the core code ... any plans to integrate? Would be great to be able to read a cell as well

no

from simplexlsxgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.