1
votes

At the moment, I have to go through every sheet and use this macro to align it vertically and horizontally. Is there a way of tweaking this script to align all sheets instead of having to do each one manually?

function Alignment() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();
  sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
  spreadsheet.getActiveRangeList().setHorizontalAlignment('center')
  .setVerticalAlignment('middle');
};
1

1 Answers

1
votes

Get all sheets and iterate over each sheet:

function Alignment() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheets = spreadsheet.getSheets();
  sheets.forEach(sheet=>{
  sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).activate();
  spreadsheet.getActiveRangeList().setHorizontalAlignment('center')
  .setVerticalAlignment('middle');
  });
};

If you want to get more performance but you won't be able to see the changes as they are hapenning in the sheets, then try this:

function Alignment() {
  const spreadsheet = SpreadsheetApp.getActive();
  const sheets = spreadsheet.getSheets();
  sheets.forEach(sheet=>{
  let rg=sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
  rg.setHorizontalAlignment('center')
  .setVerticalAlignment('middle');
  });
};