I'm using OfficeOpenXML in Asp.NET. When trying to create a table in Excel, the subtotal columns return blank as opposed to summing the values in the columns. Ironically, when I say "Enable Editing" in Excel, the formulas work, and the cells are populated with the subtotal values. Does anyone know how to get the formulas to work upon opening?
try this:
SpreadsheetDocument.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = True
Related
Summary of the problem
How to read the output value of a formula in a cell, using .NET and Openxml.
Goal
Using .NET open-xml, to read a value of a cell that contains a formula and that is not calculated at compile-time, but at run time.
For example if we have a cell with the formula: =SUM(O32:O80). I need to read the output value after evaluating this formula, using .NET.
In my case the formulas is getting calculated only when you open the excel file and press enable editing button, only then, the formulas get calculated.
So in other words, is there a way to force calculating the formulas of the cells to then read the output cell values from .NET.
Not relevant answers
As the value of the formula is not getting calculated at compile-time, I would say a not relevant answer would be in the form of:
Getting the Cell
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(tmpTempletePath, true)){
...
Cell cell = row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).FirstOrDefault();
Getting the cellValue using the normal way or getting it as a SharedString for example as follows:
string value = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(Convert.ToInt32(cell.CellValue.Text)).InnerText;
Force calculation by setting calculation properties
I tried:
Report.Workbook.CalculationProperties.CalculationOnSave = true;
Report.Workbook.CalculationProperties.ForceFullCalculation = true;
Report.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Why the answers above are not relevant
Because those answers give the value of the cell, but not the calculated output value of the formula included in the cell. In my case, the excel document that I'm working on is a very complex price calculation document with more than 70 defined names that point to ranges and cells, and a large number of formulas that include these defined names in their calculations to calculate a total price value. Meaning that to make the calculation in C# based on the data I have, is a big project compared with just reading the value after it gets calculated by excel at run-time.
Idea
I got an idea to include a visual-basic based button in a sheet that will read the value after calculation and persist it to a file that I read from .NET? Any ideas?
Many thanks in advance
Best Regards
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.
I am using PHPExcel_v1_8, I have implemented formula to some cells like following.
$objPHPExcel->getActiveSheet()->SetCellValue('G4','=SUBTOTAL(2,B6:B'.$row.')');
$objPHPExcel->getActiveSheet()->SetCellValue('H4','=ROUND(SUBTOTAL(9,Q6:Q'.$row.'),2)');
I also tried like
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6,4,'=SUBTOTAL(2,B6:B'.$row.')');
Here $row means total number of rows.
But when I filter any column then it append/override value of formula applied cell. Please see following filtered total row.
I want only latest value should in filtered total row means want to replace existing value. Right now, I am getting correct value but why it is overrided? Any suggestions what can be the solution?
You've actually discovered a genuine bug here.
I wasn't aware when I implemented the SUBTOTAL logic in PHPExcel that it only worked with visible rows, and ignored hidden rows. Can you please raise an issue on the github repo
However, reading through the MS Excel docs for SUBTOTAL, a function of 2 or 9 will return the result for all rows in the range (hidden or otherwise) while 102 or 109 will return the result only for visible rows
I am faced with rewriting an Excel project in R. I see a table in which a cell {= TABLE (F2, C2)} is shown. I understand how to create a Table like this (What-If Analysis, Data Table...).
As I have to understand this to rewrite in R, how can I find the original formula which stands behind that cell?
EXAMPLE: I have created a Data Table as shown here and the sheet looks like this:
In my case, I don't know how the sheet was created, and I want to know the initial formula. Now this is shown as {=TABLE(,C4)}.
(In the example I know the answer, it is in the cell (D10), but where is reference for this cell in Data Table?)
I'm using Excel 2007 but have no reason to believe things differ in other versions.
#Stanislav was right to reject my comment suggestion that TABLE was a name; it is an EXCEL function. But it is a very strange function :-}
There isn't any help on the TABLE function in the local help, it isn't listed in "List of worksheet functions (alphabetical)".
You can't manually enter or edit the TABLE function; error "That function is not valid".
Copy/Pasting cells containing the TABLE function pastes their values, not their formulae, even when you specify Paste Special > Formulas
You can't insert rows/columns immediately above/left of cells containing the TABLE function; error "Cannot change part of a data table".
Pace #pnuts using Formulas > Formula Auditing cells containing the TABLE function shows no precedents and no cells show them as dependents. Although in a VBA sheet auditing tool which I use the Range.DirectDependents Property finds the "formula range" dependent on the "margin" cells containing the formulas, but not those containg the values (see below for explanation of those terms).
I haven't been able to find anything I regard as decent documentation of TABLE(). I have found lots of illustrations of how to produce and use that function, but nothing clearly specifying the arguments and result. The best I've found is https://support.office.com/en-us/article/Calculate-multiple-results-by-using-a-data-table-e95e2487-6ca6-4413-ad12-77542a5ea50b. I'd be pleased if anyone can point me to better documentation.
I deduce the bahaviour as described here:
TABLE(Rowinp,Colinp) is an array formula in a contiguous array of cells. I'll refer to that contiguous array as the "formula range" of the data table.
The cells immediately above/left of the formula range are also part of the data table, even though they do not contain a TABLE() function and can be edited; I'll refer to those cells as the "margins" of the data table.
Rowinp and Colinp must be blank or references to single cells.
Rowinp and Colinp must be different (or error "Input cell reference is not valid"), they must not both be blank.
The values in the formula range are calculated by taking formula(s) from the margin(s) and substituting references to Rowinp and/or Colinp with values from the margin(s).
There are three mutually exclusive possibilities, corresponding to Rowinp blank or not.
TABLE(Rowinp, ) Colinp blank. The formula is that in the left margin of the same row with instances of Rowinp replaced by values from the upper margin of the same column.
TABLE( , Colinp) Rowinp blank. The formula is that in the top margin of the same column with instances of Colinp replaced by values from the the left margin of the same row.
TABLE(Rowinp, Colinp) Neither blank. The formula is that in the cell at the intersection of the left and top margins with instances of Rowinp replaced by values from the upper margin of the same column and instances of Colinp replaced by values from the the left margin of the same row.
I think that should let you work out what the effective formula is in each cell of the formula range.
But I wouldn't be surprised to learn that any of the above is wrong :-0
I welcome pointers to anything more authoritative.
I think in your example the F2 and C2 are effectively only the addresses of parameters for a function (TABLE) where that may be located anywhere, with the associated formula in the table's top left cell.
So I suggest go to C2, FORMULAS > Formula Auditing and click Trace Dependents, repeat for F2 and see where the arrows converge.
We get an excel worksheet from a customer and there is a data discrepancy with one of the col. the item description they send. We want to programmatically open the document lookup the table using item column, and then write a new column with the item description from the table (on SQLSever) that part of getting the data is fine but how to create such a new column is where I am stumped. writing in asp.net but open to however it can be done
I am getting this error when trying to insert a column:
To prevent possible loss of data, Excel cannot shift nonblank cells
off of the worksheet. Select another location in which to insert new
cells, or delete data from the end of your worksheet.
If you do not have data in cells that can be shifted off of the
worksheet, you can reset which cells Excel considers nonblank. To do
this, press CTRL+End to locate the last nonblank cell on the
worksheet. Delete this cell and all cells between it and the last row
and column of your data then save.
This problem is not linked to your code, it happens when you have data on the last column (and so Excel do not want you to loose it).
Yet, the trick is Excel considers that any style is the same as data. So, if you have ever defined a style on the whole row, inserting a column may cause this error.
Let me give an example to explain it more:
Create a new Excel workbook
Select the whole first row
Paint it in Yellow (or whatever color)
Select cells from A1 to E1
Paint them in white
Try to insert a new column before B
You get the error because Excel does consider that you wanted to define a specific style to the last cell so you would loose this information if you inserted a new column
Have a look at your file to solve this issue (IMHO, defining style on a whole row is a bad practice because it could cause this kind of error).
Yeah I often get this annoying bug bothering me.
To avoid this behavior in Excel 2007, what you do is you go to the end of your data with CTRL+end.
Then you go one cell down (or one cell right if you are at the bottom), and copy this cell ( CTRL+c), that should be blank, since your outside your data.
Then go back to the place where you (not that stupid Excel) consider your data should stop, while holding the shift key, and paste it (CTRL+v).
After this you should be able to insert that column.