简体   繁体   中英

Create new spreadsheet name it by the name of sheet Google Scripts

The function below creates a new spreadsheet from the active sheet. It names the file based on Cell A1 currently. I'd like to make it so the name is based on the name of the active sheet given not A1. Any suggestions?

function createSheet(){

var sheet = SpreadsheetApp.getActiveSheet(); // Get current active sheet.

var sheet_name = sheet.getRange("A1").getDisplayValue(); // Get the value of cell B1, used to name the new spreadsheet.

var folder = DriveApp.getFolderById("1S4Gwem7PZiryj-eDPLR2jeHALpkuZadV"); // Get the ID of the folder where you will place a copy of the spreadsheet.

var newSS = SpreadsheetApp.create(sheet_name); // create new blank spreadsheet in a root folder
var asFile = DriveApp.getFileById(newSS.getId()); // get new spreadsheet as a file

folder.addFile(asFile); // add this file to destination folder
DriveApp.getRootFolder().removeFile(asFile); // remove a file from root folder
  
  var copiedSheet = sheet.copyTo(newSS); // copy active sheet to new spreadsheet
copiedSheet.setName(sheet_name); // rename copied sheet
newSS.deleteSheet(newSS.getSheetByName('Sheet1')); // remove "Sheet1" sheet which was created by default in new spreadsheet
  
}

Answer:

Change

var sheet_name = sheet.getRange("A1").getDisplayValue()

to

var sheet_name + sheet.getName()

I hope this is helpful to you!

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM