Skip to content

Instantly share code, notes, and snippets.

@iqbalmineraltown
Last active September 11, 2020 13:47
Show Gist options
  • Save iqbalmineraltown/f0efbd74517576fc3b64764f80d230e0 to your computer and use it in GitHub Desktop.
Save iqbalmineraltown/f0efbd74517576fc3b64764f80d230e0 to your computer and use it in GitHub Desktop.
Generate Google Forms on every form submit
var RESPONSE_SPREADSHEET_ID = <ID of Form Generator response spreadsheet>;
var FORMS_FOLDER_ID = <folder ID to put generated forms>;
var TARGET_LINK_COLUMN = <column number to put generated form link>;
var RESPONSE_LINK_COLUMN = <column number to put generated form response link>;
function doTest() {
SpreadsheetApp.setActiveSpreadsheet(SpreadsheetApp.openById(RESPONSE_SPREADSHEET_ID));
SpreadsheetApp.getActiveSheet().getRange(2,TARGET_LINK_COLUMN,1,1).setValue('Hello there');
}
/**
* Copyright Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START apps_script_sheets_custom_form_responses_quickstart]
/**
* A special function that inserts a custom menu when the spreadsheet opens.
*/
function onOpen() {
ScriptApp.newTrigger('onFormSubmit').forForm(FormApp.getActiveForm()).onFormSubmit().create();
}
/**
* Creates a Google Form that allows respondents to select which conference
* sessions they would like to attend, grouped by date and start time.
*
* @param {Spreadsheet} ss The spreadsheet that contains the conference data.
* @param {Array<String[]>} values Cell values for the spreadsheet range.
*/
function setUpForm_(sheet, values) {
for (var i = 1; i < values.length; i++) {
var eventItem = values[i];
var targetCell = sheet.getRange(i + 1, TARGET_LINK_COLUMN, 1, 1);
var targetCell2 = sheet.getRange(i + 1, RESPONSE_LINK_COLUMN, 1, 1);
if(targetCell.isBlank()){
// set title
var form = FormApp.create('Formulir ' + eventItem[1]);
// move to specified folder
var folder = DriveApp.getFolderById(FORMS_FOLDER_ID);
var file = DriveApp.getFileById(form.getId());
var parents = file.getParents();
parents.next().removeFile(file);
folder.addFile(file);
// set description
form.setDescription('Tanggal: ' + eventItem[2] +
'\nTitik Temu: ' + eventItem[3] +
'\nPenanggung Jawab: ' + eventItem[4]);
// set fields
form.addTextItem().setTitle('Username Telegram').setRequired(true);
form.setPublishingSummary(true);
targetCell.setValue(form.getPublishedUrl());
targetCell2.setValue(form.getSummaryUrl());
}
}
}
/**
* A trigger-driven function that sends out calendar invitations and a
* personalized Google Docs itinerary after a user responds to the form.
*
* @param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function onFormSubmit(e) {
SpreadsheetApp.setActiveSpreadsheet(SpreadsheetApp.openById(RESPONSE_SPREADSHEET_ID));
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
setUpForm_(sheet, values);
}
// [END apps_script_sheets_custom_form_responses_quickstart]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment