Add validation to entire column - aspose-cells

In my current project I need to create an excel file with a list validation on an entire column. Googling turned up with the following two results:
http://www.aspose.com/docs/display/cellsnet/Working+with+Validations+in+Columns
This refers to aspose.cells.griddesktop which actually has the worksheet.Columns[n].Validations property. Aspose.Cells doesn't.
http://www.aspose.com/docs/display/cellsjava/Data+Filtering+and+Validation
All the examples use a CellArea which requires a start- and end row.
Anything I missed?

There are two types of excel formats. One is older XLS format and other is newer XLSX format. The number of rows inside the column in XLS format is 65536 and in XLSX format is 1048576. So you can use the above two values to cover your entire column in the CellArea.
You can also use CellArea.CreateCellArea() static method to create cell area object easily
For XLS format, the following CellArea code covers entire column A
CellArea ca = CellArea.CreateCellArea("A1", "A65536");
For XLSX format, the following CellArea code covers entire column A
CellArea ca = CellArea.CreateCellArea("A1", "A1048576");
Note: I am working as Developer Evangelist at Aspose

Here is another way to cover entire column.
// Cover entire column A
CellArea ca = CellArea.CreateCellArea("A", "A");
This will work with both XLS and XLSX format.
Please see the following sample code, execute it at your end and also read its comments. You will get two output Excel files. One in XLS format and other in XLSX format.
Now enter 200 (or any value greater than 100) in these cells and you will get validation error.
A65536
A1048576
C#
// Create workbook
Workbook workbook = new Workbook();
// Accessing the Validations collection of the worksheet
ValidationCollection validations = workbook.Worksheets[0].Validations;
// Cover entire column A
CellArea ca = CellArea.CreateCellArea("A", "A");
// Creating a Validation object
Validation validation = validations[validations.Add(ca)];
// Setting the validation type to whole number
validation.Type = ValidationType.WholeNumber;
// Setting the operator for validation to Between
validation.Operator = OperatorType.Between;
// Setting the minimum value for the validation
validation.Formula1 = "10";
// Setting the maximum value for the validation
validation.Formula2 = "100";
// Save in XLS format
workbook.Save("output.xls", SaveFormat.Excel97To2003);
// Remove the area of validation and add it again
validation.RemoveArea(ca);
validation.AddArea(ca);
// Save in XLSX format
workbook.Save("output.xlsx");
Note: I am working as Developer Advocate at Aspose

Related

how can i write a condition in ssis to verify if the column is a string doesn't contain numbers

i have an excel source to a data base but before i transmit the information in the column product-name i have to verify if the information is a string without numbers in it(example:lion is correct but lion124 is wrong) , using conditional split
and after the verification i have to send a message in an excel file telling the user that the column that he wrote is not correct and if it is correct i will send it to the data base
how could i check for the column and how can i send a excel file ?
I would run it through a script transformation and use c#.
Add a column (boolean) to your data and use this:
Row.NewColForIntTest = Row.YourStringColumn.Any(char.IsDigit);
Then conditionally split off the new column.

Closed XML data validation for allowing any numbers

I want to add data validation such that it allows only numbers.CLOSED XML
I think you can do it in memory but you can also use the "Data Validation" in Excel.
But if you use the Excel's data validation you need to be sure that the data you will insert programmatically are only number.
Anyway you can create an excel's templete file with the excel's data validation and than use closedXML for getting the file's template and then insert the data.
Basically for getting the template you can use this code(C#):
var workbook = new XLWorkbook("BasicTable.xlsx");
Info at : https://github.com/ClosedXML/ClosedXML/wiki/Loading-and-Modifying-Files
For the excel's data validation here there are some example :
https://www.got-it.ai/solutions/excel-chat/excel-tutorial/data-validation/data-validation-how-to-allow-numbers-only

Google spreadsheet - #N/A error when imported range has a custom function

In my Google spreadsheet (the newest version, as of August 2014) I'm using =IMPORTRANGE(P5,"Tasks!b6:o7") where P5 cell contains another spreadhsheet's key. One of the cells in the other spreadsheet contains my custom function, that sometimes takes time to calculate (there are dozens of cells using that function). The problem is that in the first spr. that cell's value is not displayed properly, the #N/A error message is displayed instead.
The function is defined as follows:
// global variable
var ONEDAY = 24*60*60*1000; // hours*minutes*seconds*milliseconds
function _mySimpleCalculateDaysDifference(DateToBeChecked)
{
var todaysDate = new Date();
var todaysDayModifier = todaysDate.getHours()<6?-1:0;
var daysDiff = (todaysDate.getTime() - DateToBeChecked.getTime())/ONEDAY;
return Math.floor(daysDiff) + todaysDayModifier;
}
It's being called the following way:
=if((H18>0),(H18-F18),if((F18>0),_mySimpleCalculateDaysDifference(F18),""))
where F and H columns contain date values.
Is there any way to solve that problem?
Edit:
I mentioned that the main spreadhsheet is the newest Google spreadsheet, but those being pulled in were 'old'.
This answer relates to a situation when the importing spreadsheet is the 'new' one, and imported - the 'old' (as of September 2014):
Make sure that both spreadsheets are of the same version, i.e. old<-old or new<-new (although as #eddieparkinson commented, in his case similar issue existed for an old<-old situation).
In my case converting the imported spreadsheet to the new helped and I cannot observe the issue now.
PS
Probably the easiest way to convert is to save it as an Excel file, then import it back into Google Drive and convert into Google spreadsheet. I found it the easiest and most reliable way, although I lost my charts and scripts. There were also problems with validations, so I had to recreate them. If you're not using any of these - it should be OK.

Can I use SpreadsheetGear to read from a CSV file without it formatting the cells?

Given a simple CSV file that consists of a string of digit characters and a date in UK format:
"00000000","01/01/2014"
and code to get the used cells:
IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
IWorkbook workbook = workbookSet.Workbooks.Open(#"C:\file.csv");
IRange cells = workbook.Worksheets[0].UsedRange;
when I access cells[0,0].Text it gives it as 0, because it's treating it as numeric and therefore the leading 0s are meaningless. It will do the same for the date. I'm trying to manually construct a DataTable from the cells, but I need the original values in the file.
I tried:
SpreadsheetGear.Advanced.Cells.IValues cells = (SpreadsheetGear.Advanced.Cells.IValues)workbook.Worksheets[0];
var sb = new StringBuilder();
cells[0,0].GetText(sb);
but nothing is appended to the string builder.
How can I get access to the original file values?
SpreadsheetGear does not make available the original values as found in in the CSV file (such as "00000000" in your case). You would only be able to access cell data after it has been parsed and processed by SpreadsheetGear (i.e., converting the above to a double value of 0). If you need the CSV's original values, then you'll need to open up file yourself and manually process and parse it.
It sounds like you ultimately want a DataTable, but if you still require to create a workbook file from your CSV data, once you've created a routine to manually open and parse each "cell" in your CSV file, you could enter each value into a spreadsheet as Text, so that it is preserved as it is found in the CSV file. You can go about this in two ways:
1) Set IRange.NumberFormat to "#", which will treat any future input into that IRange as Text. Example:
worksheet.Cells["A1"].NumberFormat = "#";
worksheet.Cells["A1"].Value = "00000000";
2) Prepend your inputted value with a single apostrophe, which indicates that you want the input to be treated as text. Example:
worksheet.Cells["A1"].Value = "'00000000";
If you still need a DataTable at this point, you could use the IRange.GetDataTable(...) method to accomplish this. Because the cell data is stored as Text, your DataTable values should also reflect these same values Example:
DataTable dt = worksheet.Cells["A1"].GetDataTable(GetDataFlags.None);
(There is a GetDataFlags.FormattedText option, but this isn't really relevant for your case since the cell data is stored as text anyway and so won't be formatted)

Exporting BIRT report into CSV

I am Using BIRT for reporting in my project.
The report shows correct value for amount(String) as 123456789123, but when i try to export the same report into csv, the csv file shows same amount as 1.234E11.
I want to value as 123456789123 in csv too.
Please help
Thanks
I imagine this is probably an issue with viewing it in Excel. Exporting data does not export format codes. Open the csv in notepad and you will see the correct data. If you export the report to excel you can set a custom format code like #####0 in the Format Number property in the properties editor for the data item.
If the number is too large excel will display the value like that. If you expand the column and set the column to number under format cells it will display correctly. You will need to save it as an excel workbook.
We just had this problem with the our BiRT reporting tool. When we opened the exported file in a text editor the number was formatted in scientific notation. This was a bug with one of Birt's custom formatters.
We had to look at org.eclipse.birt.report.engine.dataextraction.impl.CommonDataExtractionImpl and change the line
valueFormatters[i] = new NumberFormatter( patterns[i], this.locale );
to
String pattern = patterns[i] == null ? "Unformatted" : patterns[i];
valueFormatters[i] = new NumberFormatter( pattern, this.locale );
Setting the pattern to "Unformatted" made default format stay as a normal integer rather than scientific notation (via a decimal formatter).