Skip to main content

SetHasAutoFormat

Sets whether the pivot table is automatically formatted on update, which in the editor fits the column widths to their content.

Syntax​

expression.SetHasAutoFormat(hasAutoFormat);

expression - A variable that represents an ApiPivotTable class.

Parameters​

NameRequired/OptionalData typeDefaultDescription
hasAutoFormatRequiredbooleanSpecifies whether to format the pivot table automatically on every update.

Returns​

This method doesn't return any data.

Example​

Keep the pivot table column widths unchanged when the pivot table is updated in a spreadsheet.

// How do I stop a pivot table from resizing its columns every time it is refreshed in a spreadsheet?

// Turn off the autofit of column widths and set the widths by hand for a pivot table in a spreadsheet.

let worksheet = Api.GetActiveSheet();

worksheet.GetRange('B1').SetValue('Region');
worksheet.GetRange('C1').SetValue('Style');
worksheet.GetRange('D1').SetValue('Price');

worksheet.GetRange('B2').SetValue('East');
worksheet.GetRange('B3').SetValue('West');
worksheet.GetRange('B4').SetValue('East');
worksheet.GetRange('B5').SetValue('West');

worksheet.GetRange('C2').SetValue('Fancy');
worksheet.GetRange('C3').SetValue('Fancy');
worksheet.GetRange('C4').SetValue('Tee');
worksheet.GetRange('C5').SetValue('Tee');

worksheet.GetRange('D2').SetValue(42.5);
worksheet.GetRange('D3').SetValue(35.2);
worksheet.GetRange('D4').SetValue(12.3);
worksheet.GetRange('D5').SetValue(24.8);

let dataRef = Api.GetRange("'Sheet1'!$B$1:$D$5");
let pivotTable = Api.InsertPivotNewWorksheet(dataRef);

pivotTable.AddDataField('Price');

pivotTable.AddFields({
rows: 'Region',
columns: 'Style',
});

pivotTable.SetHasAutoFormat(false);

let pivotWorksheet = Api.GetActiveSheet();
pivotWorksheet.GetRange('A1').SetColumnWidth(30);
pivotTable.Update();

pivotWorksheet.GetRange('A9').SetValue('Has auto format');
pivotWorksheet.GetRange('B9').SetValue(pivotTable.GetHasAutoFormat());