Free Support Forum - aspose.com

Can Not apply indexing with [] Error

The full error is

Can Not apply indexing with [] to an expression of type Aspose.Cells.Pivot.PivotTable
Code is
//_excelReport.Worksheets.Add();
Worksheet sheet = _excelReport.Worksheets[0];
//Aspose.Cells.PivotTables pivotTables = sheet.PivotTables;
Aspose.Cells.Pivot.PivotTable pivotTables = sheet.PivotTables[0];
int cnt = ds1.Tables[0].Rows.Count;
int index = sheet.PivotTables.Add("=Incremental_Revenue!A1:G" + cnt, “A1”, “table”);
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];

I can not find anything on this error. Please help

Hi,


On which line of code from your code segment you find the error? Also, seeing the following line of code in your code segment, well, it won’t work if there is no pivot table existed on the sheet:
e.g
Sample code:

//Aspose.Cells.PivotTables pivotTables = sheet.PivotTables;
Aspose.Cells.Pivot.PivotTable pivotTables = sheet.PivotTables[0];

Furthermore, please try our latest version Aspose.Cells for .NET v8.5.2 if it makes any difference. If you still find the issue, kindly create a sample console demo application (runnable) with v8.5.2, zip it and attach it here to reproduce the issue on our end, we will check it soon. Also attach your template files if you have any.

Thank you.



Here is more of the code, I have a pivot table being loaded


//_excelReport.Worksheets.Add();
Worksheet sheet = _excelReport.Worksheets[0];
//Aspose.Cells.PivotTables pivotTables = sheet.PivotTables;
Aspose.Cells.Pivot.PivotTable pivotTables = sheet.PivotTables[0];
int cnt = ds1.Tables[0].Rows.Count;
int index = sheet.PivotTables.Add("=Incremental_Revenue!A1:G" + cnt, “A1”, “table”);
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables(index); This line errors
//showing grand totals for rows.
pivotTable.RowGrand = false;
pivotTable.IsAutoFormat = true;

I just found this in this forum, it is from you, your code and you have the same thing as me, so why doesn’t it work. WBT I do have 8.5.2 Here is your code


Worksheet sheet2 = workbook.Worksheets[workbook.Worksheets.Add(SheetType.Chart)];
sheet2.Name = “Pivot_Chart”;
PivotTables pivotTables = sheet.PivotTables;
//Adding a PivotTable to the worksheet
int index = pivotTables.Add("=A1:C8", “E20”, “PivotTable1”);
//Accessing the instance of the newly added PivotTable
PivotTable pivotTable = pivotTables[index]; I realise yours has square brackets and mine has (), I had changed it to try it, doesn’t work either way. I get the error;
“Can Not apply indexing with [] to an expression of type Aspose.Cells.Pivot.PivotTable”
//Draging the first field to the row area.
pivotTable.AddFieldToArea(PivotFieldType.Row, 0);
//Draging the second field to the column area.
pivotTable.AddFieldToArea(PivotFieldType.Column, 1);
//Draging the third field to the data area.
pivotTable.AddFieldToArea(PivotFieldType.Data, 2);
index = sheet2.Charts.Add(ChartType.Pie, 0, 5, 10, 10);
sheet2.Charts[index].PivotSource = “Sheet1!PivotTable1”;
sheet2.Charts[index].HidePivotFieldButtons = false;
//Saving the Excel file
workbook.Save(@“d:\test\pivotreport_chart1.xls”);

Hi,


Thanks for providing more code segment(s) and details.



int cnt = ds1.Tables[0].Rows.Count;

Could you debug what is the value in cnt?

I am afraid, unless we have your sample console application (runnable) with your template file to reproduce the issue on our end, we cannot evaluate your issue precisely and help you much. Please do the needful and attach the sample console application, we will check it soon.

Note: Since you might be importing data from DataSet/ DataTable to your spreadsheet, so kindly create dynamic data tables with your desired data in your codes to be imported into the worksheet first and remove any inter dependencies regarding data source/ database, so we could run your project properly.

Thank you.

I can’t debug anything, it won’t compile. I have entered the complete procedure below, just copy it and paste it in a class, and you will see the errors. This is what this tread is all about. The error: “Can not apply indexing with [] to an expression of type Aspose.Cells.Pivot.PivotTable”


public void PivotTable(DataSet ds1)
{

//_excelReport.Worksheets.Add();
Worksheet sheet = _excelReport.Worksheets[0];
//Aspose.Cells.PivotTables pivotTables = sheet.PivotTables;
Aspose.Cells.Pivot.PivotTable pivotTables = sheet.PivotTables[0];
int cnt = ds1.Tables[0].Rows.Count;
int index = sheet.PivotTables.Add("=Incremental_Revenue!A1:G" + cnt, “A1”, “table”);
PivotTable pivotTable = pivotTables[index]; This is the only line that errors!
This is what the question is about, the full error is "Can not apply index with
//showing grand totals for rows.
pivotTable.RowGrand = false;
pivotTable.IsAutoFormat = true;

//Draging the first field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 0);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 5);

//Draging the second field to the column area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 4);
pivotTable.ColumnFields[0].IsAutoSort = true;
pivotTable.ColumnFields[0].IsAscendSort = true;

//Draging the third field to the data area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 6);
pivotTable.DataFields[0].NumberFormat = “$#,##0”;
sheet.Cells.CreateRange(“A1”, “A1”).ColumnWidth = 25;
sheet.FreezePanes(“C3”, 20000, 12);
}

Hi,


I think I spotted the culprit line in your code segment, there is an error in your line of code:
i.e.,
PivotTable pivotTable = pivotTables[index]; This is the only line that errors!
This is what the question is about, the full error is "Can not apply index with
(Note: you have defined pivotTables as of the type Aspose.Cells.Pivot.PivotTable, so there is no question about using it as collection indexor, so you cannot use it as pivotTables[]. But if you have defined pivotTables object as of Type Aspose.Cells.Pivot.PivotTableCollection, then it would work)

please change it to:
PivotTable pivotTable = sheet.PivotTables[index];


Moreover, please remove or un-comment the following lines of code accordingly:
//Aspose.Cells.PivotTables pivotTables = sheet.PivotTables; ----------You may un-comment the line as:
Aspose.Cells.Pivot.PivotTableCollection pivotTables = sheet.PivotTables;

Please remove this line if there is no PivotTables in the Worksheet (you should also name it properly as it represents a single Pivot Table and not collection or otherwise you may keep it there):
Aspose.Cells.Pivot.PivotTable pivotTables = sheet.PivotTables[0];



Let us know if you still have any issue.

Thank you.