Eric Padget
Eric Padget

Reputation: 1

create sheet with given name then copy values to the new sheet

I am stuck here. I would like to create a sheet with a name of a given cell in my main sheet (this part works). Then, I would like to copy the values from cells G4:I26 into the new sheet that was just created (not working). Here is what I have so far:

function newSheetLast() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = ss.getRange("J1").getDisplayValue();
  ss.insertSheet(sheetName, ss.getSheets().length);
  var source = ss.getRange('G4:I26'); 
  source.copyTo(sheetName.getRange('A1'), {contentsOnly: true});
  source.clear();

Upvotes: 0

Views: 146

Answers (1)

Kishan
Kishan

Reputation: 1810

Try the following script code:

function newSheetLast() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  var newSheetName = ss.getRange('J1').getDisplayValue();
  var newSheet = ss.insertSheet(newSheetName, ss.getSheets().length);
  var source = sheet.getRange('G4:I26'); 
  source.copyTo(newSheet.getRange('A1'), {contentsOnly: true});
  source.clearContent();
};


Change the Sheet name "Sheet1" in the above code with the sheet's name in which you have the cell "J1" containing the new sheet's name.

Upvotes: 2

Related Questions