PHPExcel filling 0 values in range formulas - phpexcel

Using PHPExcel 1.7.9, I am reading an Excel template and filling values in specific cells only before sending the file to the user.
The template is full of range formulas which calculate totals on columns and rows. Trouble is, no matter what, the formula value is always set to 0 with the Writer instead of remaining blank (which is the actual Excel behaviour).
I tried using the $objWriter->setPreCalculateFormulas(false); method, to no avail.
It would certainly be possible to rewrite all the formulas and apply conditions to circumvent the issue but it's not ideal, and looking quite overkill.
Just for the sake of information, here is my Writer code:
$objWriter = new PHPExcel_Writer_Excel2007($obj);
$objWriter->setPreCalculateFormulas(false);
PHPExcel_Calculation::getInstance($obj)->clearCalculationCache();
$objWriter->save('php://output');
exit;
What am I missing?
Thanks in advance for any help!

I used $objWriter->setPreCalculateFormulas(true); and the zeroes disappeared.

Related

Is there a way to extract a substring from a cell in OpenOffice Calc?

I have tens of thousands of rows of unstructured data in csv format. I need to extract certain product attributes from a long string of text. Given a set of acceptable attributes, if there is a match, I need it to fill in the cell with the match.
Example data:
"[ROOT];Earrings;Brands;Brands>JeweleryExchange;Earrings>Gender;Earrings>Gemstone;Earrings>Metal;Earrings>Occasion;Earrings>Style;Earrings>Gender>Women's;Earrings>Gemstone>Zircon;Earrings>Metal>White Gold;Earrings>Occasion>Just to say: I Love You;Earrings>Style>Drop/Dangle;Earrings>Style>Fashion;Not Visible;Gifts;Gifts>Price>$500 - $1000;Gifts>Shop>Earrings;Gifts>Occasion;Gifts>Occasion>Christmas;Gifts>Occasion>Just to say: I Love You;Gifts>For>Her"
Look up table of values:
Zircon, Diamond, Pearl, Ruby
Output:
Zircon
I tried using the VLOOKUP() function, but it needs to match an entire cell and works better for translating acronyms. Haven't really found a built in function that accomplishes what I need. The data is totally unstructured, and changes from row to row with no consistency even within variations of the same product. Does anyone have an idea how to do this?? Or how to write an OpenOffice Calc function to accomplish this? Also open to other better methods of doing this if anyone has any experience or ideas in how to approach this...
ok so I figured out how to do this on my own... I created many different columns, each with a keyword I was looking to extract as a header.
Spreadsheet solution for structured data extraction
Then I used this formula to extract the keywords into the correct row beneath the column header. =IF(ISERROR(SEARCH(CF$1,$D769)),"",CF$1) The Search function returns a number value for the position of a search string otherwise it produces an error. I use the iserror function to determine if there is an error condition, and the if statement in such a way that if there is an error, it leaves the cell blank, else it takes the value of the header. Had over 100 columns of specific information to extract, into one final column where I join all the previous cells in the row together for the final list. Worked like a charm. Recommend this approach to anyone who has to do a similar task.

Removing columns from an index request in a data frame

Hi I currently have the code:
matrixed.data <- data.matrix(df[1:row.dim,7:col.dim])
Where the row.dim and col.dim are variables for the size of the whole frame. I would like to remove the column "df$WEATHER" that is included in the col.dim selection but don't know how to word it. I have tried adding - df$WEATHER and !df$WEATHER inside the bracket but fear I'm misinterpreting the scope of these commands.
Is it possible to do this without creating a new col.dim variable; I'm trying to keep the code as limitless as possible as the data frame may increase in size in the future.
Thank you digEmAll! I thought it would be reasonably simple I'm just a bit too green at R to think of something like that. For others what worked for me was:
(df[1:row.dim, setdiff(7:col.dim,which(names(df) == "WEATHER"))])

Custom column design

I have a raw dataset and the columns are not clearly defined at all. When I go to import the data using "Read.Table" in R, it automatically tries to approximate where the columns begin and end. But it is not correct. I know the number of characters per variable, but I am not sure how to customize them as one would in Excel(=Left(x,3) OR =MID(X,4,1)... etc.). Some variables are separated by spaces, some aren't. It is not consistent.
FYI: The document was originally ".dat", then I saved the file as a ".R" file.
Here is an example of my data
Any help is much appreciated! Let me know
You can use read_fwf from the great readr package, to specify the fix widths per variable.

phpexcel calculate formulas before saving sheet

If I save a phpexcel document in Excel5 format that contains values only, people that reference the document can open and close it without issue.
But if I put some formulas in cells, I have two undesired outcomes.
Just before saving the document I set the column widths based on the contents of the columns. Since the formulas have not been calcuated, the columns appear to be only as large as the largest single value in the column so the width is set too narrow. Once the =sum() formulas are calculated after being opened in excel, the contents overflow the cell width and display as a string of ###.
The second effect is that when the total is calculated by excel, the book is marked as modified by excel. When the user attempts to exit the book, they are prompted to see if they want to save their changes. This is disconcerting because in their mind they have not changed anything and annoying because it is an interuption that they really don't want to contend with.
I have been searching the documentation. I found a reference to $objWriter->setPreCalculateFormulas(true) but it does not help with either issue.
If a column is set to AutoSize, PHPExcel attempts to calculate the column width based on the calculated value of the column (so on the result of your SUM() formula), and any additional characters added by format masks such as thousand separators. By default, this is an estimated width: a more accurate calculation method is available, based on using GD, but this is a much bigger overhead, so it is turned off by default. You can enable the more accurate calculation using
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
If a worksheet contains formulae, then some versions of MS Excel files hold additional information detailing the calculation tree: data that is not saved by PHPExcel (because calculating the tree structure is a big overhead). You don't indicate which format you are using to save your workbooks, or which version of MS Excel you're using to open them; but this is the normal explanation for any prompting to save changes when a PHPExcel-generated file is opened in MS Excel.
It works for me if you paste it just before saving the file, like this:
$objWriter->setPreCalculateFormulas(true);
$objWriter -> save("file.xlsx");

Active Cells of Excel In RExcel

Cells in MS-Excel are always actives. Formulas update automatically when any value is modified. In R-Excel, I put data into R array/Dataframe and use it in a formula, and get the output.
When I change any data, I have to do all steps again to get the modified result. I want to do it automatically without writing any macros as excel does. I may do it to create an excel macro, but I don't want to.
Or how to keep data into R-Excel in active cell, so R may take the current value of every variable for every run/execution of R-commands.
Can anyone tell me the solution?
RApply should do what you want.

Resources