-2

I am new in google apps script. I need help for creating new spreadsheet by using existing spreadsheet and copy and sort the data into newly created spreadsheet using google apps script. Can anyone help me?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • 2
    Possible duplicate of [Is it possible to 'prefill' a google form using data from a google spreadsheet?](http://stackoverflow.com/questions/20108511/is-it-possible-to-prefill-a-google-form-using-data-from-a-google-spreadsheet) – Shahzad Barkati Oct 02 '15 at 05:40

3 Answers3

4

this does the trick:

function copySpreadSheet(newSpreadSheetName) {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var data = sheet.getSheetValues(1, 1, 1000, 26);  
  var newSheet = SpreadsheetApp.create(newSpreadSheetName)
  newSheet.getSheetByName("Sheet1")
          .getRange(1,1,1000,26).setValues(data);

}

function main() {
  copySpreadSheet("some_Spreadsheet");
}

After executing main, you can find the new spreadsheet in your drive file list. If you want to change the range of the data that should be copied, adjust the getSheetValues() method like this: getSheetValues(startRow, startColumn, numRows, numColumns). Don't forget to do the same for the getRange method a few lines below, otherwise you will get an error.

jvdh
  • 612
  • 1
  • 6
  • 19
0

You can use this simpler code:

function copySpreadSheet() {

  var s = SpreadsheetApp.openById("id"); 
  var destination = DriveApp.getFolderById("idOfFolder");
  DriveApp.getFileById("id_ofFile").makeCopy("New name", destination);
}

This will create a copy of your file in a new destination.

Mario Zamora
  • 109
  • 7
0

The SpreadsheetApp#spreadsheet class has a method copy:

var wb = SpreadsheetApp.getActiveSpreadsheet();  
var new = wb.copy("Copy of " + wb.getName());

This copy method copies the current spreadsheet and returns a reference to the new one, so additional tasks (such as removing unneeded sheets, formatting ranges, adding additional values, etc.) can be performed.

tehhowch
  • 9,645
  • 4
  • 24
  • 42