To load formulas in a range I've used this method
ws1.Cells["B4:D4"].LoadFromText($"={con_Fu_LF},={con_Un_LF},={con_Fo_LF}");
and to evaluate I've called
ws1.Cells["B4:D4"].Calculate();
Opening the workbook I see strings in that range, for example in B4:
='Fu-LF'!F6841+'Fu-LF'!K6841
these are evaluated when I click on formula bar selecting a cell and press enter.
I want these to be evaluated automatically, how to do?
LoadFromText sets the cell .Value property. But you need to set the .Formula like this:
ws.Cells["B4"].Formula = $"={con_Fu_LF}";
It works with the spacebar trick because that is Excel itself detecting that it "looks" like a formula and automatically setting it.
There is no way to do it that doesnt require you to loop through the cells/strings manually.
Related
I'm using Excel's data validation lists, checking if the cell value is into a dynamic range (the cell values of another column).
The problem is that if I add a value into the source range (eg. "A"), I can input that value on the cell with data validation. But then, if I change the source (eg from "A" to "B") the cell with data validation is still "A".
In Excel there's a button that sets a red circle around invalid data, but client does not always check if everything is good.
Can I perform that check with EPPLUS?
The documentation doesn't suggest that this check can be performed by EPPlus:
https://www.epplussoftware.com/Developers/Features mentions "Create, read, modify, delete Data validations" and links to sample code in their wiki, which is all about designing the sheet and inspecting its design, no content validation.
I've searched the source code and looked at ExcelDataValidation.cs and its various sub-types. Their "Validate" methods only check if the validation is properly defined according to Excel's rules.
To achieve what you're asking for, you'll have to use EPPlus to get the validation from the cell, interpret it depending on its type and evaluate the cell content. There is an example that touches some of the cases and could give you an idea where this is heading.
I have several files I need to open automatically and then import via SSIS.
Problem is, the relevant range in each file is different, and does not necessarily start at cell A1.
When I open a file, press CTRL+End, it will activate the last cell in the sheet. Pressing CTRL+A will now select exactly the range that I need. So this is what I'm trying to automate.
I can implement the first part, of finding the last cell, with
worksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
But I can't figure out how to mimic the Select All command, that's selecting only the adjacent cells with data.
currentRegion did it!
https://learn.microsoft.com/en-us/office/vba/api/excel.range.currentregion
worksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).CurrentRegion;
I'm trying to fill a table using the selenium drivers and all the documentations I could find only shows how to retrieve data from the cells. I'm able to access my table cells using:
var rows = Driver.FindElement(By.Id("Products")).FindElements(By.XPath("id('Products')/tbody/tr"));
var cells = tableRows[1].FindElements(By.XPath("td"));
But I couldn't find any way to update the data that's in it. The "Text" property only has a Get method and the SendKeys() function doesn't work. How can I edit the cell's value?
As a side note, my cell contains an html "input", I've tried to access it with the FindElement function of the cell but for some reasons it cannot find it.
Generally speaking, SendKeys should work if the cell indeed contains the input element. But because you're also saying that you fail to find the input element, I suspect that the input element does not exist in each cell all the time. You should probably first click on the cell in order for the input element to appear on that cell. You should be able to verify it using the Dev Tools if you inspect the element before clicking it.
IWebElement doesn't provide a method to change text, but you could use a little bit of JS - something little like :
((IJavaScriptExecutor)Driver).ExecuteScript("document.getElementByXXXXX.innerHTML = "VALUE";");
https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IJavaScriptExecutor_ExecuteScript.htm
I'm currently trying to produce the opposite effect of the Selection.SelectCell method.
Here is my code:
public void getCellContent()
{
Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
currentSelection.SelectCell();
newKey = currentSelection.Text; //global variable
currentSelection.Collapse();
}
Collapse Method set the cursor to the start of the cell.
Even if I set Collapse direction parameter to the end, the cursor going to the next cell.
My purpose is to be able to save content of a word table cell each time I type up in it (wihtout cell changing).
best regards,
Chefty.
You won't be able to set the cursor at the end of a text inside the cell of a Word table. Not without moving from a cell to another.
The only way to do that would have been using the following code:
currentSelection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
currentSelection.MoveLeft(Word.WdUnits.wdCharacter, 1);
Don't do it. Given the fact this would get called each time you type a character, it won't be fast and efficient enough and you will end up typing characters in the next cell.
You should save the content of your cell just once, when you change the selection on your own. When you type your first character, use the following code :
currentCell = currentSelection.Range.Cells[1]; //index always starts at 1, not 0
And when you click on another cell (you will maybe have to use Win32 API to catch such an event), don't forget to do :
currentCell.Select();
string currentCellContent = currentSelection.Text;
After that, you still end up with a selected cell, and still need to use currentSelection.Collapse(), but at least you got the content of your cell, and this by performing one save per edit.
I'm currently updating values in an Excel template and I keep getting my currency fields to have the green triangle in the top corner and the "Number Stored as Text" message. How do I get Excel to recognize the cell is a number and I want it to treat it that way since I already have the cell formatted as currency? (just like what happens if I'm on the cell in Excel, hit F2, and then hit enter)
Here's a simplified version of what I'm currently doing:
UInt32Value moneyFormat = report.createCellFormat(report.Stylesheet, fontIndex, backgroundIndex, borderIndex, 168);
report.UpdateValue("Workbook", "I29", "1234.56", moneyFormat, true, String.Empty);
And here's an image of what my cells look like
You might find the answer in this post:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
Probably the DataType of the cell is not set correct.