Home > Mobile >  Google Sheets - Transpose Sheet Names
Google Sheets - Transpose Sheet Names

Time:10-08

I would like the list of sheet names to be transposed, so instead of starting at H4 and down the rows, they would be set from H4 and across the columns.

I assume sheet.getRange("H4:H").clear(); will need to change to sheet.getRange("H4:4").clear();

But I do not know how to change the rest of the code to achieve what I need.

function sheetNames() {
  const ss = SpreadsheetApp.getActive();
  const sheetNames = ss.getSheets().map(sheet => [sheet.getName()]);
  const sheet = ss.getSheetByName("Master");
  sheet.getRange("H4:H").clear();
  sheet.getRange(4,8,sheetNames.length).setValues(sheetNames);
};

Again your help is much appreciated!

CodePudding user response:

It's need only two small changes:

function sheetNames() {
  const ss = SpreadsheetApp.getActive();
  const sheetNames = ss.getSheets().map(sheet => sheet.getName // <-- here
  const sheet = ss.getSheetByName("Master");
  sheet.getRange("H4:4").clear(); // to clear the range is a good idea
  sheet.getRange(4,8,1,sheetNames.length).setValues([sheetNames]); // <-- here
};

In your original code you're getting the array of names like this:

[[name1],
 [name2],
 [name3]]

Which means 1 column with 3 rows.

In the changed code you will get the array like this:

[ name1, name2, name3 ]

Which means one row with 3 cells.

  • Related