Hi,I would like to update the datasources of all my pivot tables with sheet.Cells.MaxRow before saving. But since tcd.Datasource returns a string array I don't know what to do and would like to avoid to parse the datasource string.
I wrote the following code :
foreach (Excel.Worksheet sheet in _doc.Worksheets) { foreach (Excel.Pivot.PivotTable tcd in sheet.PivotTables) {
string[] datasources = tcd.DataSource; // TODO: need to update the string array and throw the new value to ChangeDataSource method tcd.ChangeDataSource(); } }
We are using an excel file as a template and fill some sheets with rows, pivot tables datasources are set in the excel template file (by selecting all the sheet), in runtime I don't know where the datasources get the rows.
In runtime, I need to remove the (empty) option from all pivot filters and then to do that I want to update the datasource to take only filled rows (not the full sheet).
Do you have a method to parse datasource string (like Donnees!$A:$O) to get the sheet name, the start column name, the end column name, the start row name, the end row name) ?
I could parse it and rebuild a datasource string myself but I don't know if datasource string are always formatted using the same format ("=[Sheet name]![$Col name start$Row index start]:[$Col name end$Row index end]). So my goal is to update $Row index end with sheet.Cells.MaxDataRow.
I am not totally sure about needs. But, kindly use your own code to parse datasource to get sheetname, first row, first column, last row and last column etc.
See the simple code for reference. Kindly use your own code (after referring to my code) to perform your desired task.
Sample code:
var wb = new Workbook(@“E:\test2\PivotTable.xlsx”);
var pivotTable = wb.Worksheets[0].PivotTables[“PivotTable1”];
Thanks, I've finally developed my own datasources string parser to handle the following cases :
1. Donnees!$A:$O : All datas FROM col A TO Col O 2. Donnees!$A$4 : Identify one cell ($A$4) 3. Donnees!$2:$4 : All datas FROM row 2 TO row 4 4. Donnees!$B:$B : Identify one column : B 5. Donnees!$5:$5 : Identify one row : 5 6. Donnees!$A$5:$C$22: Range FROM col A, line 5 TO col C, line 22
I hope I handled all datasources formats types.
I have another question :
Can I uncheck all (empty) values for all pivot filters of all pivot tables by code ?
Can I uncheck all (empty) values for all pivot filters of all pivot tables by code ?
Could you give us details/steps involved on how to do it in MS Excel for a pivot table. Please attach a sample file and more details. Also, attach some screen shots to perform the functionality in MS Excel. We will check it soon.