EPPLUS generated xlsx files needs recover on open - epplus

I'm using EPPLUS to generate reports and that works fine.
In case I assign valid excel formula in some cells formula property, formulas are rejected, I'm getting warnings: "We found a problem with some content in..." and "Removed Records: Formula from /xl/worksheets/sheet2.xml part".
Formula looks like:"=IF(Worksheet1!C9-Calculation!$B$1=0;24;IF(Worksheet1!C9-Calculation!$B$1<0;Worksheet1!C9-Calculation!$B$1+24;Worksheet1!C9-Calculation!$B$1))" and it works if I paste it directly to excel.
My formula related code looks like:
ExcelPackage excel = new ExcelPackage();
excel.Workbook.Worksheets.Add("Worksheet1");
excel.Workbook.Worksheets.Add("Calculation");
ExcelWorksheet worksheet = excel.Workbook.Worksheets["Worksheet1"];
ExcelWorksheet worksheet2 = excel.Workbook.Worksheets["Calculation"];
for (int c = 1; c <= monthcnt; c++)
{
NumColumns cellAddr = (NumColumns)c;
string addr1 = string.Format("Worksheet1!{0}{1}", cellAddr.ToString(), 8);
worksheet2.Cells[addr1].Formula = string.Format("={0}-Calculation!$B$1", addr1);
string addr2 = string.Format("Worksheet1!{0}{1}", cellAddr.ToString(), 9);
worksheet2.Cells[addr2].Formula = string.Format("=IF({0}-Calculation!$B$1=0;24;IF({1}-Calculation!$B$1<0;{2}-Calculation!$B$1+24;{3}-Calculation!$B$1))", addr2, addr2, addr2, addr2);
}
Does anybody knows what causes this?
Thanks in advance,
SiniĊĦa

Related

openpyxl: Is it possible to load a workbook (with data_only=False), work on it, save it and re open the saved vile with (data_only= True)?

Basically the title. The thing is that I got an Excel file already (with a lot of formulas) and I have to use it as a template, but I have to copy certain column and paste it in another column.
Since I have to make some graphs in between I need the numeric data of the excel file so my plan is the following:
1.- load the file with data_only = False.
2.- Make the for loops needed to copy and paste info from one worksheet to another.
3.- Save the copied data as another Excel file.
4.- Open the new Excel created file, this time with data_only = True, so I can work with the numeric values.
The problem is that after doing this, it's like after putting data_only on the new created file it doesn't work, because when I made a list that filters NoneType values and strings in a column that have actual numerical values it gives me an empty list.
#I made the following
wb = load_workbook('file_name.xlsx', data_only = True)
S1 = wb['Sheet 1']
S2 = wb['Sheet 2']
#Determination of min and max cols and rows
col_min = S1.min_column
col_max = S1.max_column
row_min = S1.min_row
row_max = S1.max_row
for i in range(row_min + 2, row_max + 1):
for j in range(col_min + Value, Value + 2):
S2.cell(row = i+6, column = j+10-Value).value = S1.cell(row = i, column = j).value
Transition_file = wb.save('transition.xlsx')
wb1 = load_workbook('transition.xlsx', data_only = True) #To obtain only numerical values
S2 = wb1['Sheet 2'] #Re define my Sheet 2 values

Apps script: Paste filtered range to the next empty row in specific column

I would like to create a code which copies a filtered column and pastes the whole column to the next available row in another sheet. The code below works however pastes the range in a loop until the end of the spreadsheet. How can I paste it just once?
I could not find an answer myself.
I would much appreciate your help!
function CopyFilter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("MASTER");
var values = sheet.getDataRange().getValues();
Logger.log("VALUES "+values.length);
var criteria = SpreadsheetApp.newFilterCriteria()
.whenNumberGreaterThanOrEqualTo(1)
.build();
var range = sheet.getFilter()
.setColumnFilterCriteria(52, criteria)
.getRange();
range.getValues();
var sheet2 = ss.getSheetByName("DL CSV OUTPUT");
sheet.getRange("AZ4:AZ").copyTo(sheet2.getRange(2,41,sheet2.getLastRow()+1), {contentsOnly:true});
sheet.getFilter().remove();
}
Do not use the copy method. Simply select the range you are interested in, then get a range (of same size - very important) in the target sheet and set all values at once.
EXAMPLE:
const valuesToCopy = sheet.getRange("AZ4:AZ").getValues();
sheet2.getRange(2,3,valuesToCopy.length).setValues(valuesToCopy);
And no need to worry about: {contentsOnly:true}
Since the above method does not bring over any formatting.
Reference:
setValues(values)

Both variables are the same, however calculating a=b disagrees?

On my Workbook, I have a sheet called "check"
Where the CELLS have the values:
F8 = 176129.20
H8 = 176129.20
My VBA code is a basic validation script which checks boths cells and displays messages based on the outcome.
Function plchk()
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("check")
qb = sheet.Range("F8").Value
xl = sheet.Range("H8").Value
If qb = xl Then
plchk = "They're the same"
Else
plchk = qb & " " & xl
End If
End Function
Should be pretty straight forward when I compare qb = xl the THEN should be executed, however instead VBA doesn't recognise them as the same and goes down the ELSE route and outputs both numbers with a space, and they're exactly the same number. I'm lost! Variable types?
Whomever helps me i send out good karma to you inadvance!!
try this:
Function plchk()
Dim sheet As Worksheet, qb as double, xl as double
Set sheet = ActiveWorkbook.Sheets("check")
qb = cdbl(trim(sheet.Range("F8").Value))
xl = cdbl(trim(sheet.Range("H8").Value))
If qb = xl Then
plchk = "They're the same"
Else
plchk = qb & " " & xl
End If
End Function

How to read the fill pattern in Excel using Microsoft.Office.Interop

I am trying to import a spreadsheet that has a question on each row along with 4 possible answers. I can successfully read the cell values, but the correct answer is indicated by a fill pattern (50% Gray). I am using the code below to loop through the worksheet and pick out the correct answers. However, the value of Pattern seems to be the same for all columns, even though the pattern is plainly visible on the worksheet. Am I looking in the wrong place?
The worksheet is an .xls file. I am using Excel 2010 and VS 2010.
Dim dt As New System.Data.DataTable
Dim wks As Worksheet = wkb.Worksheets(1)
Dim ur As Range = wks.UsedRange
' Load all cells into an array.
Dim SheetData(,) As Object = ur.Value(XlRangeValueDataType.xlRangeValueDefault)
' Loop through all cells.
For j As Integer = 1 To SheetData.GetUpperBound(0)
For k As Integer = 1 To (SheetData.GetUpperBound(1) - 1)
'Get the pattern for the cells in columns 7 - 10
If (k > 6) And (k < 11) Then
Dim r As Range = wks.Cells(j, k)
Dim s As Style = r.Style
If s.Interior.Pattern = XlPattern.xlPatternGray50 Then
'Convert column index to "A" - "D"
Dim key As Char = ChrW(k + 58)
'Do something with key
End If
End If
Next
Next
I looked in MSDN but they give little or no explanation of how styles are stored in the object model. The few examples that I have seen show using the Style.Interior.Pattern to set the value after selecting the cell. Do I need to select the cell to read the pattern?
Any help would be appreciated.
Interior.Pattern is accessible from a Range object. The Range interface/object contains all the style and value information for the range.
The Style property of the Range object refers to Styles that are definied at workbook level ("shared" styles).
From MSDN:
The Style object contains all style attributes (font, number format, alignment, and so on) as properties. There are several built-in styles, including Normal, Currency, and
Percent. Using the Style object is a fast and efficient way to change
several cell-formatting properties on multiple cells at the same time.
In your case (where the styles seems to be definied at cell level and not by using a "shared" style), you just need to replace:
Dim r As Range = wks.Cells(j, k)
Dim s As Style = r.Style
If s.Interior.Pattern = XlPattern.xlPatternGray50 Then
With:
Dim r As Range = wks.Cells(j, k)
'Dim s As Style = r.Style 'no need
If r.Interior.Pattern = XlPattern.xlPatternGray50 Then

Reading from login.xls file and append results in same file?

Workbook workbook = Workbook.getWorkbook(new File("C:\\Users\\Tsss-Pc1\\Desktop\\login.xls"));
Sheet sheet = workbook.getSheet(0);
String uname = sheet.getCell(0, 0).getContents();
d.findElement(By.id("inputEmail")).sendKeys(uname);
String pwd = sheet.getCell(1, 0).getContents();
d.findElement(By.id("inputPassword")).sendKeys(pwd);
d.findElement(By.xpath("//div[3]/div/button")).click();
This is my code for reading user name and password from login.xls, i want to append the results in the third column in same file.
WritableCell label = new Label(2,0, "pass");
WritableSheet sheet1 = null;
sheet1.addCell(label);
I tryed with this code, but it's not working for me..
can any one tell me, how to do this...
WritableWorkbook workbook = Workbook.createWorkbook(new File("d://output6.xls"));
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
Label label = new Label(0, 0, "pass");
sheet.addCell(label);
workbook.write();
workbook.close();
}