SSIS: can I write formulas to a destination spreadsheet? - excel

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.

Related

Spire When Pdf-ing an excel file it leaves some formulas as System.Object[][]

I have an excel template that has preset formulas, then have my wpf application fills in data in other sheets then the preset formulas takes the data from the other sheet and shows it on the main page.
The problem is when I automatically PDF the excel most formulas go through but other's give me a System.Object[][] or some other errors. But when I access the excel file with the dataset it works.
The difference between the formulas that go through and the ones that don't are the ones that have an if() statement to remove all non zeroes in a range like this.
=TEXTJOIN("
", TRUE, TEXT(IF(Details!O:O>0,Details!O:O,""), "HH:MM"))
Functions like this works:
=TEXTJOIN("
",TRUE,Details!D:D)
How do I get Spire.xls to PDF the right format?
As you tested the Excel file and found it working fine, the problem is likely located inside the Spire rendering.
You can of course report a bug over there but that might not get resolved instantly.
You may want to try applying a number format to the cells for zero values instead of using that if part in the formula.
Something like this could do when writing with ClosedXML if implemented correctly by Spire:
worksheet.Cell(row, column).Style.NumberFormat.Format = "#,##0;[Red]-#,##0;\"\";#";
Formating possibilities are somewhat documented by Microsoft, take a look over there.

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).

Sorting In Cell List Box Using An Excel Formula

I have a spreadsheet where I am populating a cell in a worksheet with a drop down list using Data Validation, with a Named Range as the source. The Named Range is based on a worksheet called Services, where the data starts in the second row, the first row being headings.
This works fine where the name has been defined using the following formula:-
=OFFSET(Services!$A$2,0,0,COUNTA(Services!$A:$A),1)
However, the list is not alphabetically sorted, and all the formulas I have come across on the internet have not achieved this for me.
One site offers a solution which says
If you can handle extreme formulas, here is an all-in-one formula…
However, when I attempt to paste this in to the “Refers To” box when defining the name, it won’t paste (no error message). At first I thought this was because the formula was too long (about 360 characters) but I understand Excel 2003 should handle formulas that are 1024 characters. So I am a bit stuck and would appreciate any suggestions.
I am working for an organisation which is still using Excel 2003, with no immediate prospect of an upgrade.
I think you may be hitting a version limitation (works in Excel 2013) but in any case that name may not help because Data Validation requires a delimited list, or a reference to a single row or column and I think your formula does not provide that.
However, you should be able to create the sorted version with your formula and then name the range that populates - using that Name for your List Source: in Data Validation.

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!

OpenXML - refresh Excel sheet after cell update

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;

Resources