【发布时间】:2014-03-05 19:30:45
【问题描述】:
我查看了 Google 的产品论坛,但找不到任何东西。帮助文本字段是为简短文本设计的,但我想插入多段文章。如果没有分段符,我会得到一堆难以阅读的文本。
【问题讨论】:
标签: google-forms
我查看了 Google 的产品论坛,但找不到任何东西。帮助文本字段是为简短文本设计的,但我想插入多段文章。如果没有分段符,我会得到一堆难以阅读的文本。
【问题讨论】:
标签: google-forms
这一直困扰着我很长时间,我想出了一个基于 Apps Script 的不太优雅但高效的解决方案。帕维尔·阿加科夫也有同样的想法!我的版本也适用于多次出现,如果 Google 表单在您编辑文本时删除了换行符,则可以重新运行。
新建一个脚本,将内容替换为下面的代码。保存并返回到您的表单。
从“脚本”菜单运行脚本。现在庆祝?♀️
一些值得注意的事情:
您将在第一次运行脚本时收到权限请求。没关系,看消息,做你该做的。
一旦出现换行符,Google 表单,上帝保佑它的心脏,将在您每次编辑字段时删除它们。轻微的激怒。只需再次运行脚本。
你需要使用的脚本是:
// 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'));
}
}
}
【讨论】:
thePlaceholder 更改为var thePlaceholder = new RegExp(/ {4}(?![\n\r\t$])/, 'gi');,则可以插入多个换行符。使用此正则表达式,每个 4 个空格将变成一个换行符,并检查确保那里没有换行符,这样每次运行脚本时它就不会继续添加换行符。此外,请使用 ` `(空格)而不是 \s(空格字符),因此它不包含空格以外的任何内容。
我发现您无法通过编辑器执行此操作,但可以通过脚本执行此操作。 进入主菜单 -> 脚本编辑器; 将以下代码传递给编辑器;
function addLineBreaks()
{
var form = FormApp.getActiveForm();
// find form items you need
var questions = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
for(i = 0; i < questions.length; i++)
{
var title = questions[i].getTitle();
// if no replacement has been done yet
if(title.indexOf("\n") < 0)
{
// this will add line break after <double space> like in markdown
questions[i].setTitle(title.replace(" ", " \n"));
}
}
}
然后设置触发器以在表单打开时启动此方法。
【讨论】:
"B:"
javascript form.addMultipleChoiceItem().setTitle("What category does the project fall under?").setHelpText("http://github.com/alexa/alexa-skills-kit-sdk-for-nodejs\n\nThe Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.").setChoiceValues(['API','EDUCATION', 'GENERAL', 'DATASET'])
\n。
我自己在这个问题上挣扎了太久! 但是,当您知道它的简单性时: 转到“添加项目” 选择“章节标题” 此选项允许您将分段文本放入表单中。
【讨论】:
很抱歉这个坏消息,但这对我来说似乎是不可能的。
【讨论】:
以上解决方案都不适合我,所以我添加了一个 unicode 字符 https://www.compart.com/en/unicode/U+2002 粘贴 4 到 5 次,这就是它的外观
【讨论】:
我找到了答案!在您正在输入文本的框中,转到“开发人员”选项卡中的“属性”。您将获得一个下拉菜单。菜单底部是“普通测试属性”,带有“允许回车(多段)”复选框。
【讨论】:
截至 2018 年 6 月,以下工作(但仅记录了第二个选项):
【讨论】:
这是一个更好的解决方案,但基于上述。它允许您编辑上述解决方案所没有的表单:
// Version 2020-10-07a: by Dennis Bareis
// Handles "{nl}" in form & question descriptions
// Forms API: https://developers.google.com/apps-script/reference/forms
// Based on https://stackoverflow.com/questions/22207368/
// This code @: https://stackoverflow.com/a/64216993/3972414
// [0] ... -> Script Editor -> Create New Script
// [1] Paste into script editor
// [2] Run onOpen()
// [3] On first run authorise script
// [4] This adds 2 scripts under a new button in the edit form UI
// (to the left of the "Send" button)
// [5] Use "START" before re-editing form
// [6] Use "END" to publish the changes
// 5&6 required as otherwise you end up with "line1Line2Line3" etc
String.prototype.replaceAll = function(search, replacement)
{
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
//This doesn't perform the function on open, just adds it to the UI, you run when finished.
function onOpen()
{
var ui = FormApp.getUi();
ui.createMenu('Scripts')
.addItem('[1] Prepare for RE-EDITING this form (restore {nl})', 'editFormStart')
.addItem('[2] Publish the form (finished editing, remove {nl})', 'editFormEnd')
.addToUi();
}
function editFormStart()
{
swapLineBreaks("\n", "{nl}")
}
function editFormEnd()
{
swapLineBreaks("{nl}", "\n")
}
function swapLineBreaks(FromText, ToText)
{
var form = FormApp.getActiveForm();
// find form items you need
var oForm = FormApp.getActiveForm();
var questions = oForm.getItems();
// Fix the form's description
var formDesc = oForm.getDescription()
oForm.setDescription(formDesc.replaceAll(FromText, ToText))
// Work through each question
for(i = 0; i < questions.length; i++)
{
//var QTitle = questions[i].getTitle();
//questions[i].setTitle( QTitle.replaceAll(FromText, ToText));
var QText = questions[i].getHelpText();
questions[i].setHelpText(QText.replaceAll(FromText, ToText));
}
}
【讨论】: