Ask Your Question
0

How can I arrange the Grand Total column in a Pivot Table in reverse order and adjust the table's color using Google App Script?

asked 2022-05-12 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-11-03 06:00:00 +0000

bukephalos gravatar image

To arrange the Grand Total column in a Pivot Table in reverse order using Google App Script, you can use the setSort method of the PivotGroup class. Here's an example code snippet:

function reverseSortGrandTotal() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var pivotTable = sheet.getPivotTables()[0]; // assuming there's only one pivot table

  var grandTotalIndex = pivotTable.getColumnPosition(pivotTable.getColumnLabel(pivotTable.getColumns().length - 1));

  var grandTotalGroup = pivotTable.getRows()[0].getPivotGroup(grandTotalIndex);
  grandTotalGroup.setSort(true); // true for descending order
}

To adjust the table's color, you can use the setBackgroundColor and setFontColor methods of the Range class to set the background color and font color of the cells containing the Pivot Table. Here's an example code snippet:

function adjustColor() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var pivotTable = sheet.getPivotTables()[0]; // assuming there's only one pivot table

  var pivotRange = pivotTable.getTableRange();
  pivotRange.setBackgroundColor("#ffffcc"); // set background color

  var fontStyle = pivotRange.getFontStyle();
  pivotRange.setFontColor("#000000"); // set font color
}

You can also adjust the format of the Pivot Table using other methods of the Range class, such as setBorder, setHorizontalAlignment, and setVerticalAlignment.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-12 11:00:00 +0000

Seen: 8 times

Last updated: Nov 03 '22