Transpose copies the last row only if I specify a +1 - Why is that?
Sample code below:
Range rng = cells.CreateRange(0, 1, cells.MaxDataRow+1, cells.MaxDataColumn);
PasteOptions opts = new PasteOptions();
opts.Transpose = true; //Paste transpose
…
Range rng2 = cells2.CreateRange(0, 0, cells.MaxDataColumn , cells.MaxDataRow );
rng2.Copy(rng, opts);
Hi,
Thanks for your posting and using Aspose.Cells.
To make Transpose work correctly, you need to specify correct rows and columns in the destination range, so if your source range is 3x4 then your destination range should be 4x3 or larger.
I have tested this issue with the following code using the latest version: Aspose.Cells for .NET 8.5.1 and it worked correctly.
I have also attached the source excel file used in this code and the output excel file generated by it for your reference.
C#
String filePath = “F:\Shak-Data-RW\Downloads\source.xlsx”;
Workbook workbook = new Workbook(filePath);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
Range range = cells.CreateRange(“B3:E6”);
Range dRange = cells.CreateRange(“H5:K8”);
PasteOptions opts = new PasteOptions();
opts.Transpose = true;
dRange.Copy(range, opts);
workbook.Save(filePath + “.out.xlsx”, SaveFormat.Xlsx);
Thanks for responding Shakeel.
My range is not fixed - so I cant use cell references. I totally agree if the source range is 3*5 - the destination range should be 5 * 3
But in my case, if there are 6 rows and 3 columns in the source range , destination range is created with 3 rows and 5 columns - I am not sure why? But if I create source range with 7 rows and 3 columns - It works !
I have attached my code. Please let me know.
Thanks!
Nams:My range is not fixed - so I cant use cell references. I totally agree if the source range is 3*5 - the destination range should be 5 * 3But in my case, if there are 6 rows and 3 columns in the source range , destination range is created with 3 rows and 5 columns - I am not sure why? But if I create source range with 7 rows and 3 columns - It works !
I have attached my code.
Hi,
Please find the file attached. I use different version (.NET 4.5.1) . if I use exact references like Shakeel, it works for me too. Please try to use references like me as in the code attached in my previous response. Thx!
Hi,
Hi,
That's the desired result. The code attached works for me too. Does it work if you remove +1 ? Please find the code updated. I am trying to understand why +1 is required.
Hi,
Hi,
The column index also starts at 0 right. Why cells.MaxDataColumn + 1 was not required? I don’t think +1 should be required at all. cells.maxdatarow and cells.maxdatacolumn already points to the maximum data row and data column respectively. Since the index starts at 0, they will be one less but +1 should not be needed. I checked if that was because my source file has hidden column but it was not so. Please provide me more info .
Hi,
Creates a Range object from a range of
cells.
Parameters
- firstRow
- First row of this range
- firstColumn
- First column of this range
- totalRows
- Number of rows
- totalColumns
- Number of columns
Range rng = cells.CreateRange(0, 1, cells.MaxDataRow, cells.MaxDataColumn); //Source Range --Not sure why +1 is required
Mind you, totalRows refers to count of rows used in the range. So, if you are creating a range e.g "B1:H6", totalRows should have 6 value. But since cells.MaxDataRow gives you 5 (0-based indexed position), so you will add cells.MaxDataRow +1 here. Similarly if you talk about totalColumns that refers to count of columns used in the range. But here, totalColumns should be 7 as we exclude A column while defining the range. But since cells.MaxDataColumn gives farthest column index which has data in it --> i.e., it gives 7 (Column H is the farthest column which has data in it and is @ 7th indexed position (0-based index position)), so now you won't need to add +1 anymore here as you start creating the range from B column already.
Hope, this helps a bit.
Thank you.
Thanks so much. That was very helpful!
Hi,
Great! Thanks.