Comments (5)
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.
thanks for answering.
and if i want to open a existing excel file, change de value of the cell and save?
from simplexlsxgen.
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.
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.
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)
- Thisis excellent
- Cell text gets trimmed HOT 4
- 1.3.16 seems to have broken compatibility with Libreoffice Calc HOT 3
- _x002d_ is replaced by - HOT 7
- String is passed to date2excel HOT 1
- Can not get formats to work HOT 1
- I should make a drop-down list with Data Validation in Excel.
- Adding Line Break in Cell HOT 7
- Adding a picture HOT 1
- downloadAs or saveAs -> corrupted file HOT 5
- The autoresize of columns doesn't work properly when the first row has a different (bigger) font size than the rest of the rows HOT 4
- Feature Request: Append to Excel File HOT 1
- Hyperlink to internal file HOT 2
- xlsx defect HOT 4
- Replacement in cell2coord will not work HOT 4
- Applying style to a raw value HOT 1
- http2 HOT 3
- SimpleXLSXGen generated spreadsheet can not be parsed by SimpleXLSX HOT 1
- Backspace character corrupts output file. HOT 1
- The presence of a <b></b> tag makes the all entire text bold, not just the text inside this tag. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simplexlsxgen.