Cannot export formula to .xlsx spreadsheet using c# - c#

So I'm trying to use COUNTIF function to reference several different worksheets and count all occurrences of a keyword. I'm using the C# NuGet Package ClosedXML to export the data and the formula, but every time I do so, I get the following error in Numbers:
The formula couldn’t be imported and was replaced by the last calculated value. Original formula: =COUNTIF(2 WHATUP::Table 1::A2:A85,A6) + COUNTIF(3 Movies::Table 1::A2:A28,A6)
Excel gives me something similar. The only way to get it to accept it is to copy the formula, clear the cell, and paste the formula back. Here is the formula exactly as it is exported within my C# code (disregard the test worksheet names):
COUNTIF(2 WHATUP::Table 1::A2:A85,A4) + COUNTIF(3 Movies::Table 1::A2:A28,A4)
I've also tried the ! notation:
COUNTIF(2 WHATUP!A2:A85,A2) + COUNTIF(3 Movies!A2:A28,A2)
I've also tried using SUM to add them both together instead of +:
=SUM(COUNTIF(2WHATUP::Table 1::A2:A85,A2),COUNTIF(3Movies::Table 1::A2:A28,A2))
Yes, I've even tried adding more arguments within a single COUNTIF. This returns the same error as well.
=COUNTIF(1 Jello::Table 1::A2:A328, 3 Movies::Table 1::A2:A28, A2)
The only time I can get it to work is when I only use one COUNTIF to calculate a single range (without adding the results of another COUNTIF). But I need to add together the occurrences of a keyword throughout several worksheets, hence the use of several COUNTIFS/several arguments within a COUNTIF.
Please help! I have tried everything I can think of.
Thank you so much.

Looks like you have some space characters in the worksheet names, which then require beeing enclosed by single quotes.
Adopting your second example, following should work just fine:
=COUNTIF('2 WHATUP'!A2:A85,A2) + COUNTIF('3 Movies'!A2:A28,A2)

Related

EPPlus changes capitals to lowercase on formulas accessing different sheets

I'm having an issue with EPPlus in C#. I created a document and it works fine except for one part. I'm trying to create a formula that accesses data from a different sheet.
summarySheet.Cells["A2"].Formula = "$Details.B19";
I've also tried
summarySheet.Cells["A2"].Formula = "=$Details.B19";
The cell showed #Name? for the value. When I checked the formula, the capital letters had been reduced to lowercase letters:
=$details.b19
So the formula doesn't work. On every formula that I've tried using a formula that access a different sheet, I get the same result. However, the formulas stay capitalized for the cell info from the same sheet.
I tried subtracting data from one sheet from data from the same sheet:
summarySheet.Cells["A2"].Formula = "=$Details.B19 - B20";
And I get:
=$details.b19 - B20
So the capitalization error only occurs when accessing cells from a different sheet.
I'm guessing that it's something I'm doing wrong. My experience with C# is limited to personal projects and I've only started messing with EPPlus.
Thanks for looking and thanks in advance for any help that's given.
Use the following
summarySheet.Cells["A2"].Formula = string.Format("'{0}'!{1}", sourceSheet.Name, sourceSheet.Cells["B19"].Address);

Get around involuntary typing of columns in CSV file when using for unit test

I am using a CSV file to inject data into my test.
[TestMethod]
[DataSource( CsvData, CsvDir + "TC177023.csv", "TC177023#csv", SEQ )]
The file looks somethings like this: (other strings removed)
something,something,Value,something,something,something
,,0xDEADBEEF,,
,,-12,,
,,0,,
,,0,,
,,0,,
I have one column that I have integers in and I wanted to extend this to hexadecimals. So I took the value from the column "Value" and instead of casting it to an int
int value = (int)TestContext["Value"];
I tried to access the string representing the integer
string text = TestContext.DataRow["Value"].ToString();
But all I got was a empty string. I wont bother you with all I tried to find out what the problem was. I could at least see from inspecting the TestContext object while debugging that the type of the column indeed vas Int32.
In the end I tried to replace the integer values in the column with strings and, lo and behold, I got my hexadecimal value. My conclusion is that, when loading the values, the test framework is "helping" me and sets the type of the column by look on values.
now to my question:
Can I set something somewhere to inhibit this behavour so the MS unit test framework does not help me with this ?
I cannot change the framework, I cannot change the int values in that column to something recognised as strings. I am not the end user of the solution so any workarounds would probably make things worse. I just want to get the "raw value" of the "cell" for further processing.
I have investigated it further and for whose who would have the same question, I write it down here.
And the short answer to my question here is no, I cannot within my limitations.
The long answer is: The test framework is using oledb connection to read from excel and csv files. There is a way to make oledb to accept "mixed values", sending the string "IMEX = 1" as Extended Properties in the connection string.
Source:
https://yoursandmyideas.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/
Unfortunatly the connection string is set inside the framework code, at least in the open source version, that is accessable on githug and I haven't found a way smuggle in the string.

C# - Set excel cell with a formula containing double quotes

I'm trying to set via c# code the formula of an excel cell.
I use Microsoft.Office.Interop.Excel, version 14.
I always get an excel error 0x800A03EC, which is a kind of generic error.
String formula = "=IF(A2=\"BLANCO\";\"\";C1+1)"
Range cell = (Microsoft.Office.Interop.Excel.Range)ws.Cells[2, 3];
cell.Formula = formula;
I also tried to escape the double quotes with an #
String formula = #"=IF(A2=""BLANCO"";"""";C1+1)"
Same error, same problem.
When I try to set a simple formula, where no quotes are involved it is working fine.
Anybody has a solution?
Don't have much context for your code, but slashes should work when used appropriately. I am not sure about the use of semicolons here.
This code is an example pulled straight from one of my Interop programs and works fine:
thisExcel.xlWorksheet.Range["AD44", Type.Missing].Value = "=IF(BULK!L7=\"#N/A Field Not Applicable\",\"( \"&'Title Look Up'!C3&\" )\",\"( \"&'Title Look Up'!C3&\" )\")";
If all you want is: 'if a2 reads blanco then nothing else increment value of c1 by 1', then you just need:
=IF(A2=\"BLANCO\",\"\",C1+1)
I would also try using .Value rather than .Formula.
Hope that help
Building upon #getglad's answer - Using a slash does work for double quotes.
My recent solution (as of 2020):
worksheet.Cells[row,col].Value = "=IFERROR(VLOOKUP(etc),\"\")";

Working with a multi-line Excel Cell into a C# Program

My problem seems to be a simple case, but I can't seem to find any answers on it. I'm trying to write a program that will allow me to read Excel files using C# and the Interop method. These Excel files can contain information that has been entered with the use of "Alt-Enter" to create multiple lines within the cell, each line denoting a different value.
Currently my program spits out the contents of the whole cell, but how do I separate out the multiple values in the single cell, so that I am able to work on the individual values? Sorry if this has a simple answer, I'm a noob at this!
You can use String.Split function:
string[] values = ((string) multiLineCell.Value).Split('\n');

How to show long numbers in Excel?

I have to build a C# program that makes CSV files and puts long numbers (as string in my program). The problem is, when I open this CSV file in Excel the numbers appear like this:
1234E+ or 1234560000000 (the end of the number is 0)
How I retain the formatting of the numbers? If I open the file as a text file, the numbers are formatted correctly.
Thanks in advance.
As others have mentioned, you can force the data to be a string. The best way for that was ="1234567890123". The = makes the cell a formula, and the quotation marks make the enclosed value an Excel string literal. This will display all the digits, even beyond Excel's numeric precision limit, but the cell (generally) won't be able to be used directly in numeric calculations.
If you need the data to remain numeric, the best way is probably to create a native Excel file (.xls or .xlsx). Various approaches for that can be found in the solutions to this related Stack Overflow question.
If you don't mind having thousands separators, there is one other trick you can use, which is to make your C# program insert the thousands separators and surround the value in quotes: "1,234,567,890,123". Do not include a leading = (as that will force it to be a string). Note that in this case, the quotation marks are for protecting the commas in the CSV, not for specifying an Excel string literal.
Format those long numbers as strings by putting a ' (apostrophe) in front or making a formula out of it: ="1234567890123"
You can't. Excel stores numbers with fifteen digits of precision. If you don't mind not having the ability to perform calculations on the numbers from within Excel, you can store them as Text, and all of the digits will display.
When I generate data to imported into Excel, I do not generate a CSV file if I want control over how the data are displayed. Instead, I write out an Excel file where the properties of the cells are set appropriately. I do not know if there is a library out there that would do that for you in C# without requiring Excel to be installed on the machine generating the files, but it is something to look into.
My two cents:
I think it's important to realize there is a difference between "Data" and "Formatting". In this example you are kind of trying to store both in a data-only file. This will, as you can tell from other answers, change the nature of the data. (In other words cause it to be converted to a string. A CSV file is a data only file. You can do some tricks here and there to merge formatting in with data, but to my way of thinking this essentially corrupts the data by merging it with non-data values: ie: "Formatting".
If you really need to be able to store formatting information I suggest that, if you have time to develop it out, you switch to a file type capable of storing formatting info separately from the data. It sounds like this problem would be a good candidate for a XML Spreadsheet solution. In this way you can not only specify your data, but also it's type and any formatting you choose to use.

Categories