Quantcast
Channel: NetOffice - MS Office in .NET
Viewing all articles
Browse latest Browse all 1741

Commented Unassigned: Range.Offset[] does not work [22312]

$
0
0
The indexer .Offset[] does not work as expected. See full code below. I use Excel 2010 and NetOffice for .NET 4.5 obtained as a NuGet package

```
using System;
using Excel = NetOffice.ExcelApi;
using System.IO;

namespace NetOfficeBugs
{
class Program
{
static void Main(string[] args)
{
using (var exApp = new Excel.Application())
{
Console.WriteLine($"Excel Version: {exApp.Version}");

var xml = File.ReadAllText("example.xml");

exApp.Visible = true;

var workbook = exApp.Workbooks.Add();
var sheet = (Excel.Worksheet)workbook.Worksheets[1];


//Uncomment what you want to test
//OffsetNetOffice(sheet);
//OffsetUnderlyingObject(sheet);
}

Console.WriteLine();
Console.WriteLine("Done executing!");
Console.ReadLine();
}

private static void OffsetUnderlyingObject(Excel.Worksheet sheet)
{
var cell1 = (dynamic)sheet.Cells[5, 5].UnderlyingObject;
Console.WriteLine($"cell1: {cell1.Address}");

var cell2 = cell1.Offset[-1, 0];
Console.WriteLine($"cell2: {cell2.Address}");

var cell2OtherOverLoad = cell1.Offset[-1];
Console.WriteLine($"cell2: {cell2OtherOverLoad.Address}");


/*Output:
Excel Version: 14.0
cell1: $E$5
cell2: $E$4
cell2: $E$4
*/

}

private static void OffsetNetOffice(Excel.Worksheet sheet)
{
var cell1 = sheet.Cells[5, 5];
Console.WriteLine($"cell1: {cell1.Address}");

var cell2 = cell1.Offset[-1, 0];
Console.WriteLine($"cell2: {cell2.Address}");

var cell2OtherOverLoad = cell1.Offset[-1];
Console.WriteLine($"cell2: {cell2OtherOverLoad.Address}");

/*Output:
Excel Version: 14.0
cell1: $E$5
cell2: $D$3
cell2: $E$3
*/
}
```
Comments: The output looks different. Thats the problem right? What means the Console.WriteLine($"cell1: {cell1.Address}"); call ?? (IIS? PowerShell?) This is not C# in a desktop what i known. There is a known issue with this indexers in visual basic - because vb can not see the real this indexer. (You need an up cast here before) It looks like you face the same trap - but i didnt understand why. Can you explain more about the context? Sebsastian

Viewing all articles
Browse latest Browse all 1741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>