Google-sheets – Batch copying/renaming in Google Docs

google sheetsgoogle-apps-script

I need to copy and rename a base form (a spreadsheet) to several other forms in Google Docs. Imagine I have the following form:

  • My Base Form – @name (@course)

After running the script or whatever, I will end up with those forms:

  • My Form Copy – John Doe (Computer Science)
  • My Form Copy 2 – Donald (Arts)
  • My Form Copy 15 – Andrea (Music)

Any idea on how can I do that?

Best Answer

After some research, I figured it myself. Here is my solution if someone faces the same problem and needs a script for that:

Code

function makeForms() {
  // Google Spreadsheet ID
  var BASE_FORM_ID = "0AlDgVsiNXDhjdFlRTFVBeG5rNmprb3kHNxd1ItSFE";

  // object that holds the information 
  var aNames = [     
    {name: "Prof. Fernando", course: "Course name"},
    {name: "Prof. Rafael", course: "Course name"}
  ];

  // creation of variables
  var aSpreadsheet, aNewForm, aCurrentName = "";
  var aBaseForm = DocsList.getFileById(BASE_FORM_ID);

  // iterate over the names
  for(var i = 0; iLen = aNames.length; i < iLen; i++) {
    // set name
    aCurrentName = aNames[i].course+" ("+aNames[i].name+")";

    // make copy
    aNewForm = aBaseForm.makeCopy("Evaluation for " + aCurrentName);  

    // open spreadsheet and retrieve ID
    aSpreadsheet = SpreadsheetApp.openById(aNewForm.getId());

    // log the Url's 
    Logger.log(aCurrentName + "  "+aSpreadsheet.getFormUrl());
  }
}

References