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

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)

Related

Importing multiple 1D JSON arrays in Excel

I'm trying to import a JSON file containing multiple unrelated 1D arrays with variable amount of elements into Excel.
The JSON I wrote is :
{
"table":[1,2,3],
"table2":["A","B","C"],
"table3":["a","b","c"]
}
When I import the file using Power Query and expand the columns, it multiplies the previous entries each time I expand a new column.
enter image description here
I there a way to solve this, shows the elements of each array below each other and each array as a new column?
One method would be to transform each Record into a List and then create a table using Table.FromColumns method.
This needs to be done from the Advanced Editor:
Read the code comments and explore the Applied Steps to better understand.
Also HELP topics for the various functions will be useful
let
//Change following line to reflect your actual data source
Source = Json.Document(File.Contents("C:\Users\ron\Desktop\New Text Document.txt")),
//Get Field Names (= table names)
fieldNames = Record.FieldNames(Source),
//Create a list of lists whereby each sublist is derived from the original record
jsonLists = List.Accumulate(fieldNames,{}, (state, current)=> state & {Record.Field(Source,current)}),
//Convert the lists into columns of a new table
myTable = Table.FromColumns(
jsonLists,
fieldNames
)
in
myTable
Results

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.

Format an excell column as link (ClosedXML generated file)

i generated an excel file with ClosedXML library
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dt,"aaaaa");
wb.SaveAs("aaaa.xlsx");
one column of the datatable contains a link (a string with a valid url), the generated excel file doesn't show the columns as link but as a normal string.
How may i format this column in order to make possible click on it?

Adwords csv file in attachment is not parsing properly

I am trying to use google apps script to extract data from an email attachment which is basically an Adwords report as csv file.
Here is the gist of the code
var dataTest3 = Utilities.parseCsv(msg.getAttachments()[0].getDataAsString());
SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange(1, 1, dataTest3.length, dataTest3[0].length).setValues(dataTest3);
msg is the GmailMessage object.
The result that i am getting is an array with strange format
The data shows ok but its value is strange
Any idea how can i make it parse into the spreadsheet like a normal csv. It opens up like a normal csv when downloaded.
Thanks
The description basically an Adwords report as csv file needs to be investigated... what exactly is the file format? With only pictures of your problem, the best I can do is guess that your file is using some custom delimiter, not commas.
"CSV" stands for Comma Separated Values, but in practice it applies to text files with a number of different field delimiters - call them Delimiter-separated values. Common delimiters include commas (,), tabs (\t), colons (:), v-bars (|), and sometimes just spaces (usually between quote-enclosed text fields).
Instead of using the version of Utilities.parseCsv(csv) that assumes a comma delimiter, you can use Utilities.parseCsv(csv, delimiter) to specify a custom delimiter. You should be able to determine what the delimiter is by reviewing the attachment in the debugger.
You could also try adapting importFromCSV() from How to Import tab-delimited "CSV", which automatically detects tab or comma delimiters.

Add validation to entire column

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