Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabriel-r/3a6f47df1b2ccf03b0862f039fd2d56c to your computer and use it in GitHub Desktop.
Save gabriel-r/3a6f47df1b2ccf03b0862f039fd2d56c to your computer and use it in GitHub Desktop.
This has been bugging me for a long time and I've came up with a not so elegant but efficient solution based on Apps Script.
// From https://stackoverflow.com/questions/22207368/
function onOpen() {
var ui = FormApp.getUi();
ui.createMenu('Scripts')
.addItem('Replace 4+ spaces with line breaks in Title and Description', 'addLineBreaks')
.addToUi();
}
function addLineBreaks() {
var theForm = FormApp.getActiveForm();
var theQuestions = theForm.getItems();
var thePlaceholder = new RegExp(/\s{4,99}|\n/, 'gi');
for (i = 0; i < theQuestions.length; i++) {
var theText = theQuestions[i].getHelpText();
if (theText.search(thePlaceholder) > 0 ) {
theQuestions[i].setHelpText(theText.replace(thePlaceholder,' \n'));
}
theText = theQuestions[i].getTitle();
if (theText.search(thePlaceholder) > 0 ) {
theQuestions[i].setTitle(theText.replace(thePlaceholder,' \n'));
}
}
}
@gabriel-r
Copy link
Author

This has been bugging me for a long time and I've came up with a not so elegant but efficient solution based on Apps Script.

  • You will get a permission request the first time you run the script. It's ok, read the message and do what you have to do.
  • Google Forms removes line breaks every time you edit the field, which sucks. Just run the script again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment