Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active September 26, 2022 07:11
Show Gist options
  • Save datagistips/6a54bb56962bc8af5e2e5ebcacc56121 to your computer and use it in GitHub Desktop.
Save datagistips/6a54bb56962bc8af5e2e5ebcacc56121 to your computer and use it in GitHub Desktop.
Google Apps Script to Create a Call for Comments Google Form from a TableSchema
// 1. Go to Google Drive
// 2. New > Google Apps Script > Name it 'Auto Google Form - Schema'
// 3. Copy-Paste below code
// 4. Select 'process' function next to 'Debugging' button
// 4. Save
// 5. Press 'Execute'
function getItem(field) {
var s1 = field.name + ' ('+field.title+')';
var s2 = field.description;
var s3 = 'Type : ' + field.type;
var s4 = 'Exemple : ' + field.example;
var s5 = 'Obligatoire : ' + (field.constraints.required ? 'Oui' : 'Non');
// Assembling text
var s = s1 + '\n\n' + s2 + '\n\n' + s3 + '\n\n' + s4 + '\n\n' + s5
// Enum values ?
var enumValue = field.constraints.enum;
if (enumValue != null) {
var s6 = 'Valeurs : '+ enumValue
s = s + '\n\n' + s6
}
return s
}
function createForm(schemaUrl, formTitle, formDescription) {
// Parse Schema
var response = UrlFetchApp.fetch(schemaUrl); // get feed
var json = JSON.parse(response.getContentText());
// Create & name Form
var form = FormApp.create(formTitle)
.setTitle(item)
.setDescription(formDescription)
// Create fields
var fields = json.fields;
for (i in fields){
var field = fields[i];
var item = getItem(field)
Logger.log(item)
form.addParagraphTextItem()
.setTitle(item)
.setRequired(false);
}
}
function process() {
// !! CHANGE THIS
var schemaUrl = "https://raw.githubusercontent.com/CEREMA/schema-arrete-circulation-marchandises/master/schema.json";
var formTitle = "Appel à Commentaires - Schéma des arrêtés de transport pour la circulation de marchandises en ville";
var formDescription = "Le but de ce questionnaire est d'avoir un retour sur les champs du schéma, les valeurs possibles, leur caractère obligatoire ou pas\n\nSchéma : https://github.com/CEREMA/schema-arrete-circulation-marchandises";
createForm(schemaUrl, formTitle, formDescription)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment