I've been working with PHPExcel over the last few days and so far it's been perfect for what I need. However, today I've been trying to use mergeCells for "colspan" functionality but when I do it causes unreadable content errors and MS Excel prompts me to repair the file.
The code I'm using is:
$first = PHPExcel_Cell::stringFromColumnIndex($xCount).$yCount;
$second = PHPExcel_Cell::stringFromColumnIndex( $xCount + 2).$yCount;
$cell_merge = $first.":".$second;
$this->excelDoc->getActiveSheet()->mergeCells( $cell_merge );
I've output the result of $first, $second and $cell_merge to ensure I'm going about it the right way and the content is correct. Furthermore, when I try a static merge of cells (by entering mergeCells('A1:C1'); it works perfectly.
Would it be possible that trying to write to a merged cell would cause problems?
EDIT: just a note: I also tried to cast the variable as string just in case like this:
$this->excelDoc->getActiveSheet()->mergeCells( (string)$cell_merge );
... but no luck.
Just for future reference. The problem here existed between the keyboard and chair.
I was attempting to change the colspan during a loop and in this loop I had a call to mergeCells with 'A0:C0'.
When passing row "0" I wasn't getting any errors, just a bad excel sheet. Something to bear in mind.
Related
I'm a newbie starting to learn from source code. I bought a source code on the internet with full source code switching but it turns out there is a part that is hidden. How to do decrypt/decode for lines like this:
<?php
$keystroke1 = base64_decode("d2RyMTU5c3E0YXllejd4Y2duZl90djhubHVrNmpoYmlvMzJtcA==");
eval(gzinflate(base64_decode('hY5NCsIwEIWv8ixdZDCKWZcuPUfRdqrBmsBkAkrp3aVIi3Tj9v1+vje7PodWfQwNv3zSZAqJyqGNHRdE4+JiVU2ZVHy42fLyjDkoYUT54DdqpHxNKmsAJwtHFXxvksrAYXGort1cE9YsAe1dTJTOzCuEPZbhChN4SPw/iePMd/7ybSmcxeb+4Mj+vkzTBw==')));
$O0O0O0O0O0O0=$keystroke1[2].$keystroke1[32].$keystroke1[20].$keystroke1[11].$keystroke1[23].$keystroke1[15].$keystroke1[32].$keystroke1[1].$keystroke1[11];
$keystroke2 = $O0O0O0O0O0O0("xes26:tr5bzf{8ydhog`uw9omvl7kicjp43nq", -1);
$OO000OO000OO=$keystroke2[16].$keystroke2[12].$keystroke2[31].$keystroke2[23].$keystroke2[18].$keystroke2[24].$keystroke2[9].$keystroke2[20].$keystroke2[11];
$O0000000000O=$keystroke1[30].$keystroke1[9].$keystroke1[6].$keystroke1[11].$keystroke1[27].$keystroke1[8].$keystroke1[19].$keystroke1[1].$keystroke1[11].$keystroke1[15].$keystroke1[32].$keystroke1[1].$keystroke1[11];
eval($OO000OO000OO(base64_decode('LcTLsm
tKAADQn7lVZ+8yoBtB3ZH3OyEEMbnl0SLxTJrQvv
5M7hos9C36n38uF4Zh/u+nLDA6cf/VqJpq9PPHq2
IHD+dQlrVwpIa3BPicV2atbjLVsx+to7il1297dn
c+9PeDJGOoGn0MJUJnSqiJwrGcK5/bG2iiJtUoOk
3GKbHYjjzd5yLu3q2dPpWSFjDVTKWSS6MFsF6MU5
dsbJn7qHRxhGo0MNuluk29F3iwyAx/cYO+OfPWi1
ECDkWG1NsMLuAcM3F98vtMsubbvQjf1ZpVMUP5Eh
puFNzCi/CYkoM1VgsAetzjpvEe1M2AlX4YFjQZF0
A0VBRQKS0B5mcI7na2N/nER993+qocgmh9WawUrU
YhBMUiPNpuXNQy2o7VxHvhyO3nZkcWTmQu5kV1C2
ECbZiH8XsL4QuYbf7lI4SF1gDM/vVqRz4qyj7a8b
qS1nXP79731t4O0qcDaqN97BHDzlPwTEF6H7p9a3
Zu1Ut6X5GNTgZhWe3dHa+6yzJ58MX1Pc8mwAWK4v
EVLjGolQQLieOvkn4jD4d0FMQuLYvXhaxbzJyLR2
OHDKhMu2EwHthDt+I7YwOvVUydwEnCigk/n4iQei
SzwWNKicdunzmrVoOWl9gt8lhK+WzNpbPqkHEK7i
xBHT84UAbkHpity8i9eLUUulASI5d7cfpGWF6I4l
7tYBeJmYzXycA3FbbrSb+yNgd8XM5u7wU0mL8tVP
hJ2J/nu2QLr/OgzZrmp7xvKmpZCgHU7w0RlS1PT9
4JvxXtekif9dDGvBxSQjcwj2i32C7Abbcosvey5I
iq2hW7mjn/lUS6OUQ64Kw/v7+///4F')));
?>
is code like this dangerous?
You are looking at a piece of obfuscated code. I will explain it line by line, but first let's go over the functions that are used:
base64_decode()
This function decodes a base64 encoded string. It's used here to unscramble intentionally scrambled code.
gzinflate()
This function decompresses a compressed string. It's used the same way as base64_decode().
eval()
This function executes a string as code. Its use is discouraged and is in itself a bit of a red flag, though it has legitimate uses.
$keystroke1 = base64_decode("d2RyMTU5c3E0YXllejd4Y2duZl90djhubHVrNmpoYmlvMzJtcA==");
This line creates an apparently random string of characters: wdr159sq4ayez7xcgnf_tv8nluk6jhbio32mp
This string is saved to a variable, $keystroke1. The string itself is not important, other than that it contains some letters that are used later.
eval(gzinflate(base64_decode('hY5NCsIwEIWv8ixdZDCKWZcuPUfRdqrBmsBkAkrp3aVIi3Tj9v1+vje7PodWfQwNv3zSZAqJyqGNHRdE4+JiVU2ZVHy42fLyjDkoYUT54DdqpHxNKmsAJwtHFXxvksrAYXGort1cE9YsAe1dTJTOzCuEPZbhChN4SPw/iePMd/7ybSmcxeb+4Mj+vkzTBw==')));
This line unscrambles a doubly scrambled string and then runs this resulting code:
if(!function_exists("rotencode")){function rotencode($string,$amount) { $key = substr($string, 0, 1); if(strlen($string)==1) { return chr(ord($key) + $amount); } else { return chr(ord($key) + $amount) . rotEncode(substr($string, 1, strlen($string)-1), $amount); }}}
This creates a new function called rotencode(), which is yet another way of unscrambling strings.
$O0O0O0O0O0O0=$keystroke1[2].$keystroke1[32].$keystroke1[20].$keystroke1[11].$keystroke1[23].$keystroke1[15].$keystroke1[32].$keystroke1[1].$keystroke1[11];
This line takes specific characters from that random string from earlier to create the word "rotencode" as a string, stored in the variable named $O0O0O0O0O0O0.
$keystroke2 = $O0O0O0O0O0O0("xes26:tr5bzf{8ydhog`uw9omvl7kicjp43nq", -1);
This line uses the rotencode() function to unscramble yet another string (actually exactly the same string as before, for some reason).
$OO000OO000OO=$keystroke2[16].$keystroke2[12].$keystroke2[31].$keystroke2[23].$keystroke2[18].$keystroke2[24].$keystroke2[9].$keystroke2[20].$keystroke2[11];
$O0000000000O=$keystroke1[30].$keystroke1[9].$keystroke1[6].$keystroke1[11].$keystroke1[27].$keystroke1[8].$keystroke1[19].$keystroke1[1].$keystroke1[11].$keystroke1[15].$keystroke1[32].$keystroke1[1].$keystroke1[11];
On these lines the two (identical but separate) random strings are used to create the words gzinflate and base64_decode. This is done so the coder can use these functions without it being apparent that that's what is happening. However, base64_decode() is never used this way in the snippet you posted. That might suggest that it is used later in the code in places you haven't seen or recognized yet. Searching your code for "$O0000000000O" might yield other uses.
eval($OO000OO000OO(base64_decode('LcTLsmtKAADQn7lVZ+8yoBtB3ZH3OyEEMbnl0SLxTJrQvv5M7hos9C36n38uF4Zh/u+nLDA6cf/VqJpq9PPHq2IHD+dQlrVwpIa3BPicV2atbjLVsx+to7il1297dnc+9PeDJGOoGn0MJUJnSqiJwrGcK5/bG2iiJtUoOk3GKbHYjjzd5yLu3q2dPpWSFjDVTKWSS6MFsF6MU5dsbJn7qHRxhGo0MNuluk29F3iwyAx/cYO+OfPWi1ECDkWG1NsMLuAcM3F98vtMsubbvQjf1ZpVMUP5EhpuFNzCi/CYkoM1VgsAetzjpvEe1M2AlX4YFjQZF0A0VBRQKS0B5mcI7na2N/nER993+qocgmh9WawUrUYhBMUiPNpuXNQy2o7VxHvhyO3nZkcWTmQu5kV1C2ECbZiH8XsL4QuYbf7lI4SF1gDM/vVqRz4qyj7a8bqS1nXP79731t4O0qcDaqN97BHDzlPwTEF6H7p9a3Zu1Ut6X5GNTgZhWe3dHa+6yzJ58MX1Pc8mwAWK4vEVLjGolQQLieOvkn4jD4d0FMQuLYvXhaxbzJyLR2OHDKhMu2EwHthDt+I7YwOvVUydwEnCigk/n4iQeiSzwWNKicdunzmrVoOWl9gt8lhK+WzNpbPqkHEK7ixBHT84UAbkHpity8i9eLUUulASI5d7cfpGWF6I4l7tYBeJmYzXycA3FbbrSb+yNgd8XM5u7wU0mL8tVPhJ2J/nu2QLr/OgzZrmp7xvKmpZCgHU7w0RlS1PT94JvxXtekif9dDGvBxSQjcwj2i32C7Abbcosvey5Iiq2hW7mjn/lUS6OUQ64Kw/v7+///4F')));
This is where it all comes together. This line unscrambles a line of code which has been compressed and encoded 10 times over. The final result is this:
$cnk = array('localhost');
That's it. It sets the string "localhost" as the sole element of an array and saves it in a variable named $cnk.
In and of itself, there's nothing hazardous about running this code, but noting the lengths that the coder went to in order to hide this line, it's probably a safe bet that it wasn't placed there to help you - the buyer - in any way. Search your code for the $cnk variable if you want to know exactly what's being done. Or better yet, chalk this experience down to a loss and find a better way to learn coding. There are plenty of books, video tutorials and free resources online. Do not place your trust in whoever sold you this code. While they may not have been malicious (people suggested in comments that this might be part of a license check), anyone who includes something like this in their code is not someone you should be learning from.
Good luck on your coding journey!
Does somebody know how set conditional format with phpspreadsheet that each second row is shadowed like in the picture below?
sorry, I had to delete all the data, but I think its clear what I've meant
As it looks like there is no "function" inside phpspreadsheet to color the background of each 2nd row(?). If somebody knows such an easy way I'm still interested. In the meantime see my own amateurish way:
As long as you have the variables $col and $rowNumber available somewhere inside your script you can loop thrue all lines and set a background color for all lines with an odd number. Even lines you could address with the "else part".
if ($rowNumber%2) {
$spreadsheet->getActiveSheet()->getStyle($col.$rowNumber)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('e2efda'); //e2efda
};
The title might be a little confusing, but I've got a spreadsheet generator, and in this case it's used to generate a spreadsheet to upload to Amazon, and I'm working on converting the price of the items from USD to CAD.
In my file, I store the price of my items into $price_amazon, then I multiply it by whatever the user enters and gets stored into itself. Like so;
$conversion = $row['rate'];
$price_amazon = $row['price'];
$price_amazon = $price_amazon*$conversion
#Evaluates to:
#19.99*1.24
Here's the issue, once the spreadsheet loads up, my price fields will have the correct value, in this case 24.7876 but with an asterisk at the end, so 24.7876*. All of the cells for prices have an asterisk at the end, and I'm not sure on what is going on exactly. The image below shows exactly what I'm talking about, taken directly from the spreadsheet.
Then just remove the asterix at the end of the string and convert the value to a float:
$price_amazon = (float) substr($row["price"], 0, -1);
or even just a type conversation to a float removes the asterix from the end:
$price_amazon = (float) $row["price"];
Figured out the problem. User error! Further down my script, where I'm echoing all the variables into their necessary cells, I fat-fingered the asterisk key attempting to do God-knows-what. Here's what it looked like
echo "<table>\n";
echo "<tr>
<td>$price_amazon*</td>
As you can see, such a very easy mistake to make and hard to find, but this was definitely a case of user error. I appreciate everyone taking the time to help resolve my issue when in the end, the issue was me!
I've searched everywhere for an answer to this, and I truly do hate asking what is probably a really simple question, but I am lost.
This is the code I'm using to display a pie cart ...
$s1 = array(
array('Carpets',235000),
array('Vinyl',35069),
array('LVT',36911),
array('Laminate',5243.97)
);
$pc = new C_PhpChartX(array($s1),'chart1');
$pc->set_grid(array('drawBorder'=>true,
'drawGridlines'=>false,
'background'=>'#ffffff',
'shadow'=>false));
$pc->set_axes_default(array());
$pc->set_series_default(array(
'renderer'=>'plugin::PieRenderer',
'rendererOptions'=>array('showDataLabels'=>true)));
$pc->set_legend(array('show'=>true,
'rendererOptions'=> array('numberRows'=> 1),
'location'=> 's'));
$pc->draw(400,400);
The chart works fine like this for testing purposes, but when I try and replace the data inside the data array with variables, no chart appears. I think it may be to do with the way the data in the array is being passed to the JavaScript, but I am not skilled enough to fix.
The data is in variables ... $cTotal, $vTotal, $lTotal & $laTotal
I have echoed these and can confirm they are just producing an integer.
I have tried ...
$s1 = array(
array('Carpets',$cTotal),
array('Vinyl',$vTotal),
array('LVT',$lTotal),
array('Laminate',$laTotal)
);
As well as using "" and "{}", but to no avail.
OK, so I figured it out. For some reason when I used ...
$cTotal= $dataLabel['carpet'];
It echoed 236987. But didn't work. However, had a last minute thought and physically forced it to be an integer with ..
$cTotal= (int)$dataLabel['carpet'];
... and it worked. I thought PHP was clever enough to figure that out for itself!
Using PHP, I'm trying to grab data from the Goodreads API, which returns XML. I've been having a hell of a time trying to figure out how to pull data out of it.
At some point in the adventure, someone suggested I do a json decode( json encode ($blah)) of the whole thing and use JSON instead of XML.
That brings me to my current situation. Everything works as it should, up to the point where I'm pulling data out of the returned array. I probably should have spent more time reading and learning about arrays, but after more than two days of doing every Google search I could think of, I came here.
Here's the entirety of what gets returned by Goodreads:
a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
What I want from this array is the "id" variable appears under "best_book". For that, I'm using these lines of code:
$goodreads_results = json_decode(json_encode((array) simplexml_load_string($goodreads_data_a)), 1);
$goodreads_id = $goodreads_results->search->results->work->best_book->id;
I should point out that the array I posted (that began "a:2:{s:7") is what's contained in $goodreads_results after the above two lines of code. So I know everything UP TO that point works as it should.
For whatever reason, I'm not getting the ID. The $goodreads_id variable is empty.
Can somebody help me figure out why? Even though I know it's likely something basic, I'm lost, and everything is starting to look the same to me.
Try:
<?php
$goodreads_data_a = 'a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
';
$goodreads_results = unserialize($goodreads_data_a);
echo $goodreads_id = $goodreads_results['search']['results']['work']['best_book']['id'];
?>