Ask Your Question
2

How can an app script be used to create a new Google Sheets file with a particular range and sheets from an existing sheet when it is activated?

asked 2023-07-20 14:00:20 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-20 14:24:01 +0000

ladyg gravatar image

Here is a sample Google Apps Script code that creates a new Google Sheets file with a particular range and sheets from an existing sheet when activated:

function createNewSheet() {
  // Get the source spreadsheet and the specific sheet
  var sourceSpreadsheet = SpreadsheetApp.getActive();

  // Define the source sheet and the range to copy
  var sourceSheet = sourceSpreadsheet.getSheetByName('Sheet1');
  var sourceRange = sourceSheet.getRange('A1:D10');

  // Create a new spreadsheet
  var newSpreadsheet = SpreadsheetApp.create("New Spreadsheet");

  // Copy the range to the new spreadsheet
  var destinationSheet = newSpreadsheet.getActiveSheet();
  sourceRange.copyTo(destinationSheet.getRange('A1'), {contentsOnly:true});

  // Add additional sheets to the new spreadsheet
  newSpreadsheet.insertSheet('Sheet2');
  newSpreadsheet.insertSheet('Sheet3');
}

To activate this script, simply run the createNewSheet function from the script editor or assign it to a button or trigger.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-20 14:00:20 +0000

Seen: 10 times

Last updated: Jul 20 '23