0
votes

Im looking to make 60+ copies of a template spreadsheet using the google sheets API. I would like to be able to name each sheet using a cell in that same sheet.

function NewBracket() {
var spreadsheet = SpreadsheetApp.getActive();
var name = spreadsheet.getActiveRange().activate() ;
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Template'), true);
spreadsheet.duplicateActiveSheet();
spreadsheet.getRange('B1').activate();
name.copyTo(spreadsheet.getActiveRange(),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getActiveSheet().setName(name);
};

When I run this, the sheet is created properly, the cell B1 is named properly, but the newly created sheet is named Range, not the name that is input into cell B1. How do I get the above code to name the sheet the same name that shows up in the B1 cell?

1
change line into spreadsheet.getActiveSheet().setName(name.getValue());ra89fi
Make sure you handle the case where the name already exists in the workbook, too. (And consider refactoring the output of the macro recorder -- it makes no sense to activate selections when you can just use a variable to store the reference)tehhowch

1 Answers

0
votes

The answer came from user ra89fi in the comments, but it worked. I changed spreadsheet.getActiveSheet().setName(name); to spreadsheet.getActiveSheet().setName(name.getValue()); and it worked perfectly.