Jerry Gearheart
Jerry Gearheart

Reputation: 1

Google Sheets - Duplicate workbook sheet and prompt for name of new worksheet in same workbook

Using Google Sheets. The first sheet in my workbook is a template that I'd like to duplicate, in the same workbook, and have a prompt ask me to name the new worksheet (thus preserving the name of the template worksheet.

I've seen several examples of this - without the ability to name the new worksheet what I'd like to name it.

I've accomplished this in MS Excel, and placed a macro button on the template sheet to execute the macro - but I haven't been able to do the same in Google Sheets.

Any help would be appreciated.

Upvotes: 0

Views: 355

Answers (1)

Cooper
Cooper

Reputation: 64100

Create New Sheet From Template with new name prompt

function createNewSheetFromTemplate(templatename="default") {
  const ss=SpreadsheetApp.getActive();
  const template=ss.getSheetByName(templatename);//default parameter
  const r=SpreadsheetApp.getUi().prompt('New File From Template',"Enter new file name", SpreadsheetApp.getUi().ButtonSet.OK);
  ss.insertSheet(r.getResponseText(),{template});//using object literal here. If you use the object literal here it cause problems for the script editor formatting. 
}

insertSheet(sheetname,options)

Object Intializer

This script requires the newer and faster V8 engine. If you use template literals as shown in the last line it causes problems with the script editor. There's an issue already created on that: https://issuetracker.google.com/issues/150198182

Upvotes: 1

Related Questions