PHPExcel - column ranges stripped from formula - php

I'm trying to insert a "COUNTIFS" formula and I know it's not a supported function, but I have the calculation setting turned off on the writer. It's not throwing any errors. However,
$active_sheet->setCellValue('C3', "=COUNTIFS('INVENTORY'!$H:$H,1,'INVENTORY'!$C:$C,\"ADMIN\",'INVENTORY'!$N:$N,1)");
Is getting written to the file as
COUNTIFS('INVENTORY'!:,1,'INVENTORY'!:,"ADMIN",'INVENTORY'!:,1)
I read on another page somewhere that referencing columns like this wasn't supported either, but I also tried putting them in like "$C2:$C3000" and it didn't help.

The issue would be that you're using double quotes around your second parameter. PHP is trying to replace $H, $C and $N with actual variables.
Try using single quotes and escaping the existing single quotes within the string.
Here are the docs on how PHP parses double quoted strings which might help.

So the solution was to remove or escape the dollar signs ($). I removed them and it worked fine, and then I escaped them and it also worked!!
Resulting example line:
$active_sheet->setCellValue('C3', "=COUNTIFS('INVENTORY'!\$H:\$H,1,'INVENTORY'!\$C:\$C,\"ADMIN\",'INVENTORY'!\$N:\$N,1)");
edit: wow, noob mistake. Credit goes to Ian Belcher.

Column and Row ranges aren't supported by PHPExcel at this point: you need to supply an actual range (e.g. 'INVENTORY'!$H1:$H500 rather than simply 'INVENTORY'!$H:$H.
There are also a couple of known issues when inserting or deleting new rows or columns into a worksheet when it doesn't correctly adjust an existing formula to reflect that change. Those issues have been fixed in the latest develop branch code on github, so you may want to try again using that.

Related

PHP curly braces in database value

so I have an issue. We have some values in our DB stored like the following:
{yoyoyo}
This is great until we pull the value out of the DB using PHP. It appears to break PHP when trying to access the variable like so:
$result['curly'];
For some reason PHP is interpreting that as if its a variable.
Anyone know how to escape the result from the DB so PHP interprets it as a string and not a variable?
Thanks!
You could use htmlentities - http://php.net/manual/en/function.htmlentities.php
This will convert all characters to html entities. You can also in the future store things that don't need to be used as variables to html entities before putting them in the database to avoid this problem.
http://www.freeformatter.com/html-entities.html
Yeah, after further research it appears that it's pulling the string value out of the DB just fine. However, later on in our code we were doing other, lets just say, inefficient stuff. :)

Displaying Code Snippets Properly Without Escape Characters

I have a PHP script that stores my code snippets.
To insert, I use:
$snippet_code = mysqli_real_escape_string($conn,trim($_POST['snippet_code']));
To display, I use the following which is wrapped in a pre tag:
$snippet_code = htmlentities($row['SnippetText']);
I notice that sometimes I get a lot of escape characters like \\\\ when the snippet is displayed on the page. The escape characters are present wherever single or double quotes appear in the code. The problem seems to be more severe in non-English language browsers.
How can I properly do this? How can I properly store and display code on a page?
Assuming you mean slash escape sequences like \", and not HTML escape sequences like & try this:
$snippet_code = htmlentities(stripslashes($row['SnippetText']));
If it is actually HTML escapes causing you trouble, just omit the htmlentities call.
If you are getting ' converted to \', your server is probably configured with a legacy option called Magic Quotes. You can read about it in the PHP manual. My advice is to disable them if possible.
Also, check your database. It's possible that your current data is corrupted. If so, you can write a small script thay uses stripslashes() to fix it.
From your comments, it seems that you are in fact talking about slashes found before quotes.
It's not clear from the limited information you've given us why non-English browsers would show more of these.
However, it is likely that these slashes should not be present in the first place. Perhaps you are running mysql_real_escape_string several times, instead of just once... but, again, nothing you've shown us indicates that.
Either way, you should fix the data in the database and not just hack around the issue on display.

PHP 5.3 fgetcsv \"

I'm importing a CSV from DB2 into MySQL, all goes well until half a million rows in I encounter \" from a column with encrypted data.
Here is an example:
100,"foo","bar","µ┬;¬µ┬;→ºµ┬;Öì\"
101,"foo","bar","$⌠ù¶∙$∙µ┬µ┬;→ºµ┬;Öì"
When fgetcsv parses this, it escapes the last double quote and includes the next line as if it is part of that field.
I see a few bug reports and in PHP 5.3 they added an escape param for fgetcsv.
What does DB2 use as an escape? Just "?
From the comments on the fgetcsv manual page it looks like this is a fairly common issue with no real good workaround. There are however some alternate functions which people have been kind enough to post on the page which might do what you need.
Here is a link to one of them: http://us3.php.net/manual/en/function.fgetcsv.php#98800

How to show a comma within a comma separated file

I'm generating a csv file using php, now some columns contain a paragraph with commas, now when I open the file , every comma within the file counts as a new column, is it maybe possible to escape these commas on a way?
Depends, what, your, CSV, reader, is, "but, quoting, should, work"
Many CSV readers will allow commas within a single column by surrounding the column with double quotes. In that case, double quotes can be represented by double double quotes:
column 1,"column 2, with comma","column 3 with ""quote chars"", and comma"
That's the BIG problem with using a , in a CSV file. I would recommend using a different separator like | (it's less likely to appear on a text) or using a different more robust file format like XML for generating your file.
You're using a comma because it's a delimiter. That is, the comma has special meaning no matter when its used. By that very definition, it becomes hard to treat it as context sensitive. It can be done though, considering symbols like '\n.
You can try a new delimiter, such as ,\n, though that might not be an option for you.
Looks to me that the best solution would be to use a different persistence mechanism. Things will get sticky otherwise.

Best Way to parse an iCalendar string in php

I am trying to write a class that can parse an iCalendar file and am hitting some brick walls. Each line can be in the format:
PARAMETER[;PARAM_PROPERTY..]:VALUE[,VALUE2..]
It's pretty easy to parse with either a bunch of splits or regex's until you find out that values can have backticked commas, also they can be double quote marked which makes life hard. for example:
PARAMETER:"my , cool, value",value\,2,value3
In this example you are meant to pull out the three values:
my , cool value
value,2
value3
Which makes it a little more difficult.
Suggestions?
Go through the file char by char and split the values manually, whenever you have a quotation mark you enter "quotation mode" where you won't split at commas and when the closing quotation mark comes you leave it.
For the backticked commas: If you read in a backslash you also read the next character and decide what to do with it then.
Of course that's not extremely efficient, but you can't use regular expressions for this. I mean you can, but since I believe that there also can be escaped quotation marks this is going to be really messy.
If you want to give it a try though:
let's start by matching a quotation mark followed by characters that are not: "[^"]*"
to overcome the problem of escaped characters you can use lookaheads (?<!\\)"[^"]*(?<!\\)"
now it will break if escaped quotation marks are in the value, maybe this works?(haven't tested it) (?<!\\)"[^"|(?<=\\)"]*(?<!\\)"
So you see it very fast get's messy, so I would suggest to you to read it in characterwise.
I had the same problems. I found it a bit hard to turn 'any' iCalendar file into a usable PHP object/array structure, so instead I've been trying to convert iCalendar to xCal.
This is my implementation:
http://code.google.com/p/sabredav/source/browse/branches/caldav/lib/Sabre/CalDAV/ICalendarToXML.php
I must say that this script is not fully tested, but it might be enough to get your started.
Have you tried pulling something out of http://phpicalendar.net/ ?
Is this the project you're thinking of? I'm the auther :) The first usable version (v0.1.0) should be ready in about a month. It is capable of working with about 85% of the iCalendar spec right now, but recurring events are really tough. I'm working on them right now. Once those are complete, the library will be fully capable of doing anything in the spec.
qCal Google Code Homepage
Enjoy!

Categories