function to copy openoffice comments to a cell - function

I'm trying to import spreadsheet data into a CMS, but there are around 100 comments on various cells throughout the 4000+ row spreadsheet that we would like to import also. Is there a function or macro that will copy the comment content into a cell? (not as a comment, just as plain text)

In Openoffice API the interface XSheetAnnotationsSupplier provides a method getAnnotations. Using this it is possible to get all sheet annotations together with their positions. The Position is the address of the cell in which the annotation is placed:
sub getAnnotations()
oThisWorkbook = ThisComponent
oActiveSheet = oThisWorkbook.CurrentController.ActiveSheet
oAnnotations = oActiveSheet.Annotations
for each oAnnotation in oAnnotations
lColumn = oAnnotation.Position.Column
lRow = oAnnotation.Position.Row
sText = oAnnotation.String
oCell = oActiveSheet.getCellByPosition(lColumn, lRow)
msgbox oCell.AbsoluteName & " has annotation: " & sText
next
end sub
So you have the annotations (comments) and their cells.
Now you have to decide what you wants to do with that. As said in my comment already, simply appending the annotations to the cell contents is not a good idea in my opinion. That will possibly make the cell content unreadable for further processing.

Related

PHPexcel save to CSV linked cells to another sheet are empty

Could someone help me?
I have xlsx file, with 2 sheets.
Second sheet contain cells linked to another(first) sheet.
When I save sheet to CSV file:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
This function doesn't save (linked) cells value...
My code looking like this:
$objPHPExcel = new PHPExcel();
// Read your Excel workbook
try
{
$inputFileType = PHPExcel_IOFactory::identify($excelFile);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly('list');
$objReader->setLoadSheetsOnly('main');
/* I also tried like this:
$worksheetList = $objReader->listWorksheetNames($excelFile);
$sheetname = $worksheetList[0];
$sheetname2 = $worksheetList[1];
$objReader->setLoadSheetsOnly($worksheetList[0]);
$objReader->setLoadSheetsOnly($sheetname);
*/
$objPHPExcel = $objReader->load($excelFile);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($filename);
I also tried to save to EXCEL files (xls and xlsx) -> the same problem, the cells (which was linked) they are empty...
My linked cells looked like this: "=list!C46"
Many hours of looking for answer, I have found not good solution:
I've removed any of these lines: $objReader->setLoadSheetsOnly('list');
and add:
$activeSheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$objPHPExcel->getActiveSheet()->fromArray($activeSheetData, false);
just after:
$objPHPExcel = $objReader->load($excelFile);
and also add: $objWriter->setSheetIndex(1);
Now it will works, but with problems...
One column in original format have linked cells like this: "=list!$AV$46"
I mean with $ symbol.
More detailed:
If I have: $objReader->setReadDataOnly(true);
and have those cells: "=list!$AV$46" then they are empty in output.
But if I remove: $objReader->setReadDataOnly(true);
then those cells: "=list!$AV$46" works good and have a value, but with format
like: 11/17/2017.
As I removed $objReader->setReadDataOnly(true);, then I can't apply
this my code:
$objPHPExcel->setActiveSheetIndex(1)->getStyle('AV1:AV'.$highestRow)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD);
Then second question, how to write new date format?
I also wanted to say, that initial date format was:
17.11.2017.
And I wanted 17-11-17 (FORMAT_DATE_YYYYMMDD).
And again, with cells which looks like "=list!AV46" all works good.
UPDATE: Solvation: $objReader->setLoadSheetsOnly(['list', 'main']); + $objPHPExcel->setActiveSheetIndex(1); before any of: $objPHPExcel->getActiveSheet()->...
Here's your first problem
$objReader->setLoadSheetsOnly('list');
$objReader->setLoadSheetsOnly('main');
You're only ever loading one sheet, the last one that you tell PHPExcel to load, which is main. Multiple calls to setLoadSheetsOnly() overwrite the setting of the previous call.
If you want to load both sheets, then you need to pass an array listing all the sheetnames that you want to load
$objReader->setLoadSheetsOnly(['list', 'main']);
This is explained in the PHPExcel Documentation
The documentation also says not to use $objReader->setReadDataOnly(true); unless you understand what it is doing; it tells PHPExcel to load only the rw data, not the formatting of data; and it is the formatting that differentiates a number from a date in Excel.

Aspose Cells - Change named range reference

I have a named range that current refers to A1:I8 in a worksheet. I would like to change it to refer to a larger part of the worksheet. Does anyone know of a method in Aspose that does this? Or a method that lets me delete the existing range in Aspose?
You may try the following sample code for your reference:
Workbook workbook = new Workbook("e:\\test\\Book1.xlsx");
Worksheet sheetRef = workbook.Worksheets[0];
NameCollection names = workbook.Worksheets.Names;
Name name = names["MyRange"];
name.RefersTo = "=A1:J10";

how to create dynamic excel sheets based on table data

Given one table in SQL Server which holds consolidated data from three source tables including one column called OFFICE which differentiates the records from each other.
The three source tables hold data from three offices.
I want to create an Excel file dynamically which will have 3 sheets in one workbook based on the three different different offices (ex. office1, office2, office3) resulting in each sheet containing the relevant data according to its office.
Please recommend an approach using dynamic Excel destination in SSIS as I don't want to use an approach which creates a template file and then copies that template to destination excel file.
While this can be accomplished using a scipt task and C#, a far easier solution is demonstrated at
http://www.rafael-salas.com/2006/12/import-header-line-tables-_116683388696570741.html
and the follow-up
http://www.rafael-salas.com/2008/03/ssis-and-dynamic-excel-destinations_01.html#!
But to summarize the relevant details, you need to use an 'Execute SQL Task' to dynamically create the sheet at runtime prior to using it as a destination.
Create a new variable to hold the Sheet name and set this variable to the Office you are working with as you iterate through them.
Also, create a variable to hold the Create table statement that will create each sheet.
For example,
"CREATE TABLE "+ #[User::SheetName] + "(HeaderID INTEGER, HeaderName NVARCHAR(50), LineID INTEGER, LineName NVARCHAR(50), LineDetails NVARCHAR(50))"
and Set the SQLSourceType Property of the Execute SQL task inside of the For Each container to Variable and choose the Variable you created to hold the create statement.
In the Excel Destination Component, Change the data access mode to ‘Table Name or View Name Variable’ and choose the sheet name variable you created from the variable dropdown list.
I have several SSIS packages that perform a similar function. A single Excel file consists of multiple worksheets with each worksheet populated by results from a separate SQL query. Here are the basic generic steps I applied. Before you begin, make certain you create a connection manager for both the database to be applied and the output Excel file.
1) Create a Script task in Control flow and populate it like the following. Here I am creating the Excel file along with the worksheets it will contain. (Worksheets should never include any spaces or special characters.) My code below is in C#.
using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.SqlServer.Dts.Runtime;
namespace ST_87e8d62a054b4e16b60297154afc19d8.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
//Create First worksheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Names";
//Define column headers for "RawData" WorkSheet
xlWorkSheet.Cells[1, 1] = "First Name";
xlWorkSheet.Cells[1, 2] = "Last Name";
xlWorkSheet.Cells[1, 3] = "Title";
// Create Second Worksheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet.Name = "Addresses";
//Define column headers for "CCDN" WorkSheet
xlWorkSheet.Cells[1, 1] = "Street";
xlWorkSheet.Cells[1, 2] = "City";
xlWorkSheet.Cells[1, 3] = "State";
xlWorkSheet.Cells[1, 4] = "Zip";
xlWorkSheet.Cells[1, 5] = "Country";
string Filename = "C:\\MyFile.xls";
if (File.Exists(Filename))
{
File.Delete(Filename);
}
xlWorkBook.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Dts.TaskResult = (int)ScriptResults.Success;
}
2) Create in your database two tables that will be populated temporarily. That is, one table will be populated for the results of the first worksheet and the second table will be populated for the results of the second worksheet. It is a good naming approach to preface the names of each table with "Working_" so that you know the purpose of each. I took the approach of using tables instead of views is because I like to sort (ORDER BY) my results, which cannot be done with a view.
3) Add to the SSIS package two Execute SQL Tasks under Control Flow. The first task will run an INSERT SQL statement that will populate the first table you just created and the second task will run another INSERT SQL statement that will populate the second table just created.
4) Add to the SSIS package two Data Flow tasks under Control Flow. The first will be for populating the first worksheet and the second for populating the second worksheet.
5) Select the first Data Flow task and add to it under Data Flow an OLE DB Source where you will define the OLE DB conenction manager (your database) and then the table or view. Select the first new table created. Make certain all of the columns of interest are selected and that you can perform a preview.
6) Add a Data Conversion flow task and then an Excel Destination flow task.
7) Repeat steps 5 and 6 for the second worksheet and table.
8) Finally under Control Flow add an Excel SQL Task that will remove the contents of the two Working tables. You do not want the old contents to be included the next time the package is run.
Now, if you want to play around with formatting of the Excel file after it is completed and impress your manager, you can also do that in code with a final Task Script (also using C#). The nice part about this approach is that you are not having to apply any special formatting functions in your SQL, Excel is doing all the work. You could actually include the formatting in Step 1 and as soon as you copy the data over in the following steps, it is automatically formatted. As with any report output, there is not point in making SQL perform formatting steps (adding additional work to the database server) when it is more efficient to let Excel or SSRS do what they do best.
public void Main()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel.Range xlRange;
xlApp = new Excel.ApplicationClass();
string Filename = "C:\\MyFile.xls";
xlWorkBook = xlApp.Workbooks.Open(FileName, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Format cells in Names worksheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//Set the header range in bold font
xlRange = xlWorkSheet.get_Range("a1", "p1");
xlRange.Font.Bold = true;
xlRange.WrapText = true;
//Freeze first row listing headers
xlWorkSheet.Application.ActiveWindow.SplitRow = 1;
xlWorkSheet.Application.ActiveWindow.FreezePanes = true;
//Auto adjust the width of each column
xlWorkSheet.Columns.AutoFit();
xlRange = xlWorkSheet.get_Range("c1", "j6467");
xlRange.Cells.Locked = false;
xlRange.Interior.Color = 65535;
xlRange = xlWorkSheet.get_Range("o1", "p6467");
xlRange.Cells.Locked = false;
xlRange.Interior.Color = 65535;
//Do not alert when saving changes to Excel file.
xlWorkBook.Application.DisplayAlerts = false;
//Save Excel file modifications
xlWorkBook.Save();
//Close workbook and application
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//Release from cache.
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
//Set formatting of percent cells
xlRange = xlWorkSheet.get_Range("d3", "d7");
xlRange.NumberFormat = "###,###%";
//Define the top left cell and bottom right cell of the table in the Excel worksheet
xlRange = xlWorkSheet.get_Range("c1", "c7");
//Draw grid of thin line around each cell in table
xlRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, 1);
//Draw thick border around entire table
xlRange = xlWorkSheet.get_Range("a1", "d7");
xlRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexAutomatic, 1);
//Right justify columns B and C
xlRange = xlWorkSheet.get_Range("b3", "c7");
xlRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
//Do not alert when saving changes to Excel file.
xlWorkBook.Application.DisplayAlerts = false;
//Save Excel file modifications
xlWorkBook.Save();
//Close workbook and application
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//Release from cache.
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Dts.TaskResult = (int)ScriptResults.Success;
}
And that's about it. Notice that just for the purpose of this example, I'm hardcoding the filename. But in my actual code I am applying a User variable which is then populated by another SQL statement pulling the name from another database table. For best practices, it a good idea to keep your SSIS packages entirely table-driven. That way, any changes made to names and locations are made in a database table in a record specific to your SSIS package... avoiding any need to update your SSIS package and go through the dev to QA to production lifecycle again.
Hope this helps and please let me know if you have any questions.

How to refer to cell without A1 notation?

In my Access VBA I have hyperlink, which is using the following way to link to cell:
oSheet.Cells(1, i).Formula = "=HYPERLINK(""#Sheet2!E6"", """ & !TestCase & """)"
However, instead of E6, I want to use row,col notation, since all my internal application logic is using Cells/rows/cols.
Thanks.
Instead of Formulause FormulaR1C1(Row / Column format)
Here are two examples:
Set the formula of your cells to =$B$1 :
oSheet.Cells(1, i).FormulaR1C1 = "=R1C2"
Set the formula of A1 to =C2, A2 to =C3 etc.:
Range("A1:A10").FormulaR1C1 = "=R[1]C[2]"

Adding new output column to custom data flow component SSIS

I'm trying to add new output column using synchronous custom data flow component(below is the code). While testing I found that input columns I added are not getting displayed in Output columns only the new added column is getting displayed. I'm not sure where problem is. Please help!
Public Overloads Overrides Sub ProvideComponentProperties()
Dim input As IDTSInput100 = ComponentMetaData.InputCollection.New()
input.Name = "Input"
Dim Output As IDTSOutput100 = ComponentMetaData.OutputCollection.New()
Output.Name = "Output Rows"
Output.Description = "Output rows with unique row ID appended."
'Adds new column RowID to output columns list
Dim rowIDColumn As IDTSOutputColumn100 = Output.OutputColumnCollection.[New]
rowIDColumn.Name = "Row ID"
rowIDColumn.SetDataTypeProperties(DataType.DT_UI8, 0, 0, 0, 0)
Output.SynchronousInputID = input.ID
Thanks in advance
Sai
my bad! for Data flow components only new columns will be displayed in the advance editor, but when we take the output arrow and attach to destination(like excel or file or db) then it shows all the columns including the input columns and newly added columns in the mapping tab of destination editor. :)