简体   繁体   中英

Google Apps Script: Creating a folder in Google Drive from a Google Form submission

I am trying to create a function which, upon the submission of a Google form, creates a folder in Google Drive using the first two fields of the form to name the folder. Currently the form is being created without issue, but no folder is created upon submission. Here's the function I've come up with based on some Googling which uses only the first field to create the folder name:

function createForm() {
 var form = FormApp.create('New Form');

var clientName = form.addTextItem();
 clientName.setTitle('Client Name');
var clientAddress = form.addTextItem();
 clientAddress.setTitle('Address');
var clientPhoneNumber = form.addTextItem();
 clientPhoneNumber.setTitle('Phone Number');

function createFolder(form) {
  var items = form.response.getItemResponses()
  var name = items[0].getResponse();
  DriveApp.createFolder(name);
}


}

Any ideas where I'm going wrong?

As @Cooper suggests in the comments - there is nothing in your script that binds the createFolder function to the form and the form submission action. What you have to do is to separate the createFolder function from the createForm function and have the createForm function install a trigger to the created form that triggers the createFolder function on submission. See below example:

function createForm() {
 var form = FormApp.create('New Form');

var clientName = form.addTextItem();
 clientName.setTitle('Client Name');
var clientAddress = form.addTextItem();
 clientAddress.setTitle('Address');
var clientPhoneNumber = form.addTextItem();
 clientPhoneNumber.setTitle('Phone Number');
ScriptApp.newTrigger('createFolder').forForm(form).onFormSubmit().create();
}

function createFolder(form) {
  var items = form.response.getItemResponses()
  var name = items[0].getResponse();
  DriveApp.createFolder(name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM