OpenXML - refresh Excel sheet after cell update - excel

I have a class that reads & writes Excel (xlsx) cells using OpenXML SDK. The class is based of the most voted answer from here: Open XML SDK 2.0 - how to update a cell in a spreadsheet?
I need to update a cell and then get a value of another cell, which contains a calculation formula. The update works fine, but when I read the formula cell after the update I get the old value, that existed in the doc before editing. However, when I open my xlsx manually after running the program I can see the correct value.
So it's seems like the old value for cell is cached somewhere... Which is weird, because I open/close my doc each time before I read/write cells.
EDIT:
Vincent's answer made me update my sample code. I added a Refresh method that opens, saves and closes the document in the Excel application ran in background. This recalculates my formulas.
For more details and C# code sample see: http://fczaja.blogspot.com/2013/05/how-to-read-and-write-excel-cells-with.html

Open XML SDK doesn't refresh the calculation results of formulas. Only Excel does that (or other spreadsheet software). So even if you set ForceFullCalculation and FullCalculationOnLoad to true, you only tell Excel to force a full calculation and do it when loading the spreadsheet file.
This is why you always get the "old" value. Because there's no "new" value to speak of.
To test this, you can set the CellValue (of the Cell class) to say "3.14159" with the CellFormula intact. Excel will calculate and display the correct value of your formula, even though you specifically set the cell value as "3.14159". [Unless of course, your formula evaluates to PI...]
To solve this, you either have a calculation engine to read in the formula and calculate the result for you. Or save the spreadsheet and run Excel (in Interop mode) to calculate all the formulas, but that kind of defeat the purpose of using Open XML SDK in the first place.

You can make use of the Workbook.CalculationProperties Property, something like:
document.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
You could also force the calculation of the formulas (if needed):
document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;

Related

SSIS: can I write formulas to a destination spreadsheet?

I am using SSIS to write data from a SQL source to an Excel destination. Plugging in hardcoded values is fine. However, I've been requested to put in SUM() formulas in the last column. They want the actual formula, not the summed value, which I could do on the SSIS side.
I wrote a formula that worked and added it as an additional column in my data transform:
"=SUM(OFFSET($A$1,ROW()-1,3,1,9))"
HOWEVER, when I open my target spreadsheet, this formula is just plugged in as text. It will not evaluate unless I expressly edit the cell (just click in and out). F9, Shift-F9, etc do not work.
I'm pretty sure there's no way to overcome this, but I thought I'd check. I am open to other solutions. I cannot pre-fill the formulas, since the amount of data I'm adding is variable.
I should also mention that I do not want to use any solution that requires manipulating Excel via the Interop library. The entire point of this exercise is to stop using Excel on the server machine, so I can only install the relevant driver, not Excel itself.

Downloading File with importrange function failing - think it's a bug

I've been saving Google Sheets to Excel without any problems for a while. These sheets have always successfully saved and opened in Excel with the importrange function. However, recently it hasn't been successfully saving correctly.
It used to just have the static value (e.g, 40). There used to be an IFERROR in the first cell in the header row but now it exists in every single cell.
E.g, each cell would have something like this:
=IFERROR(__xludf.DUMMYFUNCTION(importrange(blahblah)),"40").
DUMMYFUNCTION throws an error and "40" is returned as a result. but "40" is a string, not an integer which messes up all my formulas.
I also know this isn't an Excel issue because OpenOffice is doing the same thing with the file.
I'm pretty sure this would be a bug because why would it be working for months and then suddenly stop working?
What should I do?
I'm thinking it's a bug too.
Workarounds
On Excel
Copy and paste as values only the ranges with IFERROR(__xludf.DUMMYFUNCTION(..., then use Excel's UI tools to convert numbers shown as text to numbers.
Selectively remove quotes on the IFERROR second argument of the cells causing problems
Remove =IFERROR(__xludf.DUMMYFUNCTION(),"value") except value (we could use Excel's built-in FIND & REPLACE for this)
On Google Sheets
Use Copy > Paste as values only on the range areas having formulas with non-compatible functions like IMPORTRANGE, QUERY, FILTER, etc.
If you only need the values, download it as CSV instead of XLSX
IMPORTANT
In order to help to prioritize this issue, send feedback to Google. To do this open a Google Sheets spreadsheet, click on Help > Report a problem, then fill the feedback form and submit it.
Related stuff
I posted 5 small articles about this in Spanish. You could find them listed on https://www.rubenrivera.mx/p/descargar-hcg-excel.html.
We accidentally created a workaround for this bug with a different sheet that was just set up like this.
This works when you IMPORTRANGE into another Google Sheet. We are doing it into a Google Sheet with a single worksheet - haven't tried it with multiple.
It's going to sound a little nuts but it works for us.
In the first cell of your import range put a hyperlink in the original document you are importing from. This is in the first cell of the import range. We linked it to a worksheet in the original document. It has worked and failed with an external link. With an external link it worked when I linked it to an internal link, then changed it. But when I deleted the cell and just straight linked it to an external URL it didn't work.
Then #timbo was right - put data validation in. This can be in part of the document that isn't being imported into the second sheet. I put it in the first line of the import range but outside what I was importing. It might have to be the first line. I just put a date in one cell, then in the next cell data > data validation > then choose that one date as the data range.
For aesthetics I have hidden the first row in one Google Sheet I am importing into. In another I made the first cell link the title of the sheet and put the data validation outside the import range. Both of these work.
Let me know if this works for you.
Until this bug is fixed, a workaround is to put a data validation (Data > Data Validation) on the imported data (Any kind of data validation will do).

Locating Lost Excel Macro Formula From Old Data Sheet

I'm resurrecting some old scientific data from the early 2000s.
I need to locate the custom functions that allowed the data to be shown. The spreadsheet that I have is full of #REF! cells, as they are supposed to be calculated based on a custom-defined formula (here, called 'RESECTION').
How do I find this formula? If I can see the math it was performing, I will be able to use this old data, and extend our timeseries significantly.
The spreadsheet is an ".xlsm" document. There is an associated file that is ".XLM"; it provides some GUI-like functionality that is now broken, and I do not see how to access the commands (?) or other VBA that is inside.
I have not had success with this solution.
File with the VBA can be found here; SURVEY.XLM.
Problem is seen here; calling function from SURVEY.XLM. How do I access the formula within here?
I can see that the formula is in there; how do I see the calculation it performs?
RESECTION is a named range refering to cell A4 on the hidden Survey sheet.
In the VBE immediate window type thisworkbook.Sheets(2).visible = true and then thisworkbook.Sheets(2).select.
Cell Survey!A4 contains the value =RESULT(64).
The rest of the sheet contains the macros - first time I've seen or tried to use a filled in macro sheet.
I tried Ctrl+Fto find the definition of RESULT but it comes up with Macro error at cell [SURVEY.XLM]SURVEY!A364.

Groovy POI created cells considered blank by other programs until ENTER is manually pressed in cells

Ok this is kind of hard to explain.
I create a cell in my worksheet using JAVA POI
newCell= row.getCell(index)
if (containerCell == null) {
containerCell = row.createCell(index)
}
newCell.setCellType(HSSFCell.CELL_TYPE_STRING)
newCell.setCellValue(strVar)
If i then open the worksheet in EXCEL, I see that the cell value is indeed set. However if I load this into another external program that reads EXCEL sheets, it claims the cell I just set is blank.
NOW, if I go back into excel and do a simple "Hit return" on the cell in question, in the formula bar (even though its not a formula) and try to reload it into the external program, it works fine. Do I need to evaluate a formula on a string?
Thanks
I have no experience with Java, but having to hit enter in a cell is usually indicative of calculation being set to manual. Since you are working with API it may be treating your input from Java as a formula (?).
Open the offending workbook in its broken state, and on the sheet you are having problems with choose [Formulas] tab at top, then [Calculation]>Calculate Sheet.
If this updates the value then I see two options...
See if there is an option to manually calculate the sheet with POI, after the value has been entered.
Alternatively, you can write just a tiny bit of VBA to force calculation on that sheet when workbook is opened. MSDN offers a simple example.
http://msdn.microsoft.com/en-us/library/office/aa223802%28v=office.11%29.aspx
I have had to do this for Excel apps where calculation had to be set to manual to avoid excessive overhead. Just a guess though..
I am not sure why this was happening.. and evaluating the individual cells did not solve it however running evaluteAll() on the entire workbook made it work!

Trying to maintain a record of data in an excel cell

I am working with an excel sheet and wondering is there any way you can enter a currency value into a cell without completely removing the previous amount. I am trying to keep a record of numerous previous entries put into the excel sheet. It needs to be enabled so it is just a case of adding the new value and the previous values would be stored in the same cell. I know its a long shot but any help would be seriously appreciated. Would look something like below with the €1000 being the last entry and the €3000 being the first.
€1000
€1300
€1250
€3000
You cannot squeeze more than one value into a cell.
You could write VBA code that could, for example, use the Change event of the worksheet to add a comment to the cell and append the previous value to this comment. Or use this event to copy the previous value to a, perhaps hidden, worksheet.
For completeness I should mention that there is a Track Changes feature in Excel but it requires the workbook to be shared - which I do not recommend. Excel is not designed to work with multiple-users.

Resources