With the following script , a new form will be created
// Create a new form, then add a checkbox question, a multiple choice question,
// a page break, then a date question and a grid of questions.
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
item.setTitle('What condiments would you like on your hot dog?');
item.setChoices([
item.createChoice('Ketchup'),
item.createChoice('Mustard'),
item.createChoice('Relish')
]);
form.addMultipleChoiceItem()
.setTitle('Do you prefer cats or dogs?')
.setChoiceValues(['Cats','Dogs'])
.showOtherOption(true);
form.addPageBreakItem()
.setTitle('Getting to know you');
form.addDateItem()
.setTitle('When were you born?');
form.addGridItem()
.setTitle('Rate your interests')
.setRows(['Cars', 'Computers', 'Celebrities'])
.setColumns(['Boring', 'So-so', 'Interesting']);
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
Ask:
How to achieve the reverse effect, that is, from an existing form get the generation script?
As Rubén says , there is no generic way to perform reverse engineering from form to script, but you can write an ad hoc Script using the methods
getActiveForm()
andgetItems()
, then I put an ad hoc script inspired by the following answers:Scripts:
To see result:
Ctrl
+Intro
.Copy text and perform cleanup to finally get:
What is the Utility?
One way to use it would be to recycle questions from an existing form.
See also
Auto-generating Google Forms
I understand that it is not possible to create a Google Apps Script code sequence automatically, which would be a form of reverse engineering , but it is possible to make a partial report of the elements of a form.
I say partial because as of today only the Google Apps Script Forms Service is available as an official API and it does not include all the elements that are available through the forms editor. For example, tests cannot be created/read completely.
As for the methods you could use are:
Some posts about Google Forms where some of the above methods are used:
See also
Auto-generating Google Forms