Hey @amjad.sahi / @peyton.xu , the above is not working for me, like I’m not getting any errors, but no filter is applied is my pivot table.
here is full code
if (customer.customerConfigurationBackupPivotTables) {
customer.customerConfigurationBackupPivotTables.forEach((pivotTable: CustomerConfigurationBackupPivotTable) => {
pivotTable.backupPivotTableFields.sort((a: BackupPivotTableFields, b: BackupPivotTableFields) => a.orderRowNumber - b.orderRowNumber);
});
}
customer.customerConfigurationBackupPivotTables.forEach((pivotTableConfig: CustomerConfigurationBackupPivotTable) => {
console.log(logTag, 'Working on Pivot Table:', pivotTableConfig.title);
sampleWorkbook.getWorksheets().add(`${pivotTableConfig.title}`);
const pivotSheet = sampleWorkbook.getWorksheets().get(`${pivotTableConfig.title}`);
const pivotTables = pivotSheet.getPivotTables();
const lastColumn = excelColumnAssigner(processedTransactionInvoiceDataKeys.length - 2);
const sourceData = `=${'InvoiceData'}!A7:${lastColumn}${lastDataRow + 1}`;
console.log("Source Data", sourceData);
const pivotTableIndex: number = pivotTables.add(sourceData, 'A1', `${pivotTableConfig.title}`);
const pivotTable = pivotTables.get(pivotTableIndex);
pivotTableConfig.backupPivotTableFields.forEach((fieldConfig: BackupPivotTableFields) => {
console.log(logTag, ` - Pivot Table Field: ${JSON.stringify(fieldConfig)} - fieldConfig.isChecked: ${fieldConfig.isChecked}`);
if (fieldConfig.isChecked) {
const fieldIndex = processedTransactionInvoiceDataKeys.findIndex(({ key }) => {
const matchesFieldName: boolean = key === fieldConfig.fieldName;
return matchesFieldName;
});
if (fieldIndex > 0) {
console.log(logTag, ` - fieldConfig.fieldName: ${fieldConfig.fieldName} - fieldConfig.fieldType: ${fieldConfig.fieldType} - Field Index: ${fieldIndex}`);
}
if (fieldIndex >= 0) {
try {
switch (fieldConfig.fieldType) {
case PivotFieldType.row:
console.log(`Adding ROW field. fieldIndex: ${fieldIndex}, PivotFieldType: ${aspose.cells.PivotFieldType.ROW}`);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, fieldIndex);
addNotEmptyFilter(pivotTable, fieldConfig.orderRowNumber - 1);
break;
case PivotFieldType.column:
console.log(`Adding COLUMN field. fieldIndex: ${fieldIndex}, PivotFieldType: ${aspose.cells.PivotFieldType.COLUMN}`);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.COLUMN, fieldIndex);
addNotEmptyFilter(pivotTable, fieldConfig.orderRowNumber - 1);
break;
case PivotFieldType.filter:
console.log(`Adding PAGE field. fieldIndex: ${fieldIndex}, PivotFieldType: ${aspose.cells.PivotFieldType.PAGE}`);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.PAGE, fieldIndex);
break;
case PivotFieldType.value:
console.log(`Adding DATA field. fieldIndex: ${fieldIndex}, PivotFieldType: ${aspose.cells.PivotFieldType.DATA}`);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.DATA, fieldIndex);
const dataFieldIndex = fieldConfig.orderRowNumber - 1;
const pf = pivotTable.getDataFields().get(dataFieldIndex);
if (pf) {
if (fieldConfig.valueSummarizeMethod.toLowerCase() === 'sum') {
console.log(`Setting SUM function. ConsolidationFunction: ${aspose.cells.ConsolidationFunction.SUM}`);
pf.setFunction(aspose.cells.ConsolidationFunction.SUM);
} else if (fieldConfig.valueSummarizeMethod.toLowerCase() === 'count') {
console.log(`Setting COUNT function. ConsolidationFunction: ${aspose.cells.ConsolidationFunction.COUNT}`);
pf.setFunction(aspose.cells.ConsolidationFunction.COUNT);
}
if (fieldConfig.customName) {
try {
pf.setDisplayName(fieldConfig.customName);
console.log(`Set display name for data field: ${fieldConfig.customName}`);
} catch (error: any) {
console.log(`Setting display name for data field failed: ${error.message}`);
}
}
} else {
console.error(`Field "${fieldConfig.valueSummarizeMethod}" not found in pivot table data fields.`);
}
break;
default:
console.error(`Unknown field type: ${fieldConfig.fieldType}`);
}
} catch (error: any) {
console.error(`Error in switch case for fieldType ${fieldConfig.fieldType}: ${error.message}`);
console.error(`fieldConfig: ${JSON.stringify(fieldConfig)}`);
console.error(`fieldIndex: ${fieldIndex}`);
}
} else {
console.error(`Field index out of bounds ${fieldIndex}`);
}
}
});
pivotTable.setRowGrand(true);
pivotTable.setColumnGrand(true);
pivotTable.setShowDrill(false);
console.log(logTag, "-->Start of setting row subtotals<--");
console.log(logTag, pivotTable.getRowFields().getCount(), JSON.stringify(pivotTable.getRowFields()), "Row Field Data");
if (pivotTable.getRowFields().getCount() > 0) {
for (let i = 0; i < pivotTable.getRowFields().getCount(); i++) {
try {
const rowField = pivotTable.getRowFields().get(i);
const fieldName = rowField.getName() || "";
if (!rowField || typeof rowField.setSubtotals !== 'function') {
console.warn(`Invalid row field ${fieldName} at index ${i}. Skipping.`);
continue;
}
rowField.setSubtotals(aspose.cells.PivotFieldSubtotalType.NONE, true);
console.log(`Successfully set subtotals for field: ${fieldName} and fieldIndex : ${i}`);
} catch (error) {
console.error(`Error setting subtotals for row field ${pivotTable.getRowFields().get(i).getName()} and fieldIndex : ${i} `, error);
}
}
}
console.log(logTag, "-->End of setting row subtotals<--");
console.log(logTag, "-->Start of setting column subtotals<--");
console.log(logTag, pivotTable.getColumnFields().getCount(), JSON.stringify(pivotTable.getColumnFields()), "Cloumn Field Data")
if (pivotTable.getColumnFields().getCount() > 0) {
for (let i = 0; i < pivotTable.getColumnFields().getCount(); i++) {
try {
const columnField = pivotTable.getColumnFields().get(i);
const fieldName = columnField.getName() || "";
if (!columnField || typeof columnField.setSubtotals !== 'function') {
console.warn(`Invalid column field ${fieldName} at index ${i}. Skipping.`);
continue;
}
columnField.setSubtotals(aspose.cells.PivotFieldSubtotalType.NONE, true);
console.log(`Successfully set subtotals for field: ${fieldName} and fieldIndex : ${i}`);
} catch (error) {
console.error(`Error setting subtotals for column field ${i}: ${pivotTable.getColumnFields().get(i)?.getName() ?? ""} : `, error);
}
}
}
console.log(logTag, "-->End of setting column subtotals<--");
pivotTable.refreshData();
pivotTable.calculateData();
console.log(logTag, "Pivot table has been created");
});
Here is sample data for customer.customerConfigurationBackupPivotTables
"customerConfigurationBackupPivotTables": [
{
"id": 520,
"title": "p",
"backupPivotTableFields": [
{
"id": 3624,
"fieldName": "timesheetEventDuration",
"customName": "Total Duration",
"isChecked": true,
"orderRowNumber": 1,
"fieldType": "value",
"valueSummarizeMethod": "Sum",
"customerConfigurationPivotTableId": 520
},
{
"id": 3625,
"fieldName": "eventDate",
"customName": "Absence Date",
"isChecked": true,
"orderRowNumber": 1,
"fieldType": "column",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3626,
"fieldName": "billedTotal",
"customName": "Total Amount",
"isChecked": true,
"orderRowNumber": 2,
"fieldType": "value",
"valueSummarizeMethod": "Sum",
"customerConfigurationPivotTableId": 520
},
{
"id": 3627,
"fieldName": "accountingCode",
"customName": "",
"isChecked": true,
"orderRowNumber": 2,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3628,
"fieldName": "accountingCodeDesc",
"customName": "",
"isChecked": true,
"orderRowNumber": 3,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3629,
"fieldName": "schoolName",
"customName": "",
"isChecked": true,
"orderRowNumber": 1,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3630,
"fieldName": "payCode",
"customName": "",
"isChecked": true,
"orderRowNumber": 10,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3631,
"fieldName": "payCodeDesc",
"customName": "",
"isChecked": true,
"orderRowNumber": 4,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3632,
"fieldName": "essEmplFullName",
"customName": "Substitute Name",
"isChecked": true,
"orderRowNumber": 5,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3633,
"fieldName": "employeeFullName",
"customName": "Employee Name",
"isChecked": true,
"orderRowNumber": 6,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3634,
"fieldName": "employeeExternalId04",
"customName": "Employee ID 04",
"isChecked": true,
"orderRowNumber": 7,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3635,
"fieldName": "confirmationNumber",
"customName": "Confirmation #",
"isChecked": true,
"orderRowNumber": 8,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3636,
"fieldName": "eventDurationType",
"customName": "",
"isChecked": true,
"orderRowNumber": 9,
"fieldType": "row",
"valueSummarizeMethod": "",
"customerConfigurationPivotTableId": 520
},
{
"id": 3637,
"fieldName": "batchId",
"customName": "Count Batch Number",
"isChecked": true,
"orderRowNumber": 3,
"fieldType": "value",
"valueSummarizeMethod": "Count",
"customerConfigurationPivotTableId": 520
},
{
"id": 3637,
"fieldName": "batchId",
"customName": "Count Batch Number",
"isChecked": true,
"orderRowNumber": 3,
"fieldType": "filter",
"valueSummarizeMethod": "Count",
"customerConfigurationPivotTableId": 520
}
]
}
]
So how my code works is it will be adding all the fileNames in customerConfigurationBackupPivotTables to pivot table one by one.
The Data for pivot table will be source from a excel sheet which I’ll be creating before this pivot table.
let me know if you guys need the full excel logic. It’s quite big that’s why I didn’t attach it here