0
votes

I'm having a problem with a file.makeCopy(name, destination) function. I have a form and a spreadsheet template in a folder. I want to copy them to another directory. Here it is my code:

  // Create the destination folder
  var newFolder = parentFolder.createFolder(institute + " - a.s. " + schoolYear + " - " + school + " - Classe " + classroom + section);

  // Create a copy of the template form in the created folder and name it "questionario"
  var formFile = formTemplate.makeCopy('questionario', newFolder);
  var form = FormApp.openById(formFile.getId());

  // Create a spreadsheet for answers in the new folder and name it "risposte"
  var repliesFile = responseTemplate.makeCopy('risposte', newFolder);
  var replies = SpreadsheetApp.openById(repliesFile.getId());

  // Connect the form to the spreadsheet
  form.setDestination(FormApp.DestinationType.SPREADSHEET, replies.getId());

Both files, form and spresdsheet, are correctly copied to the new folder. The problem is another copy of the form is created in the source directory (template directory). That is not happening to the spreadsheet: the script create a copy in directory I want and no copies are created in the template folder. I can't figure it out, any idea? Some images are attached.

Template folder template folder

Destination folder destination folder

1

1 Answers

0
votes

Whenever you make a copy of a Spreadsheet that has a linked Form, a copy of that Form is automatically made and linked to the new Spreadsheet (you can test this by manually making a copy of the Spreadsheet in Google Drive). So when your script makes a copy of the form and links it to the Spreadsheet, this is extra work because it's already been done.

You might be better off to just rename and move the copy of the form that is automatically generated:

// Create the destination folder
var newFolder = parentFolder.createFolder(institute + " - a.s. " + schoolYear + " - " + school + " - Classe " + classroom + section);

// Create a spreadsheet for answers in the new folder and name it "risposte"
var repliesFile = responseTemplate.makeCopy('risposte', newFolder);
var replies = SpreadsheetApp.openById(repliesFile.getId());

// Get the URL of the linked form
var formUrl = replies.getSheetByName('NameOfTheRepliesSheet').getFormUrl();
var formId = FormApp.openById(formUrl).getId();

// Get the Form as a Drive File object so we can change its parent folder.
var formFile = DriveApp.getFileById(formId);

// Rename the file.
formFile.setName('questionario');

// You can't 'move' a file in Drive: you add it to one folder and remove it from the other.
var oldFolder = formFile.getParents().next();
newFolder.addFile(formFile);
oldFolder.removeFile(formFile);

Note that now there is no need to call form.setDestination(), because the form was already linked when the Spreadsheet copy was made.