【问题标题】:How do I create a paragraph break in Google Form help text?如何在 Google 表单帮助文本中创建分节符?
【发布时间】:2014-03-05 19:30:45
【问题描述】:

我查看了 Google 的产品论坛,但找不到任何东西。帮助文本字段是为简短文本设计的,但我想插入多段文章。如果没有分段符,我会得到一堆难以阅读的文本。

【问题讨论】:

    标签: google-forms


    【解决方案1】:

    这一直困扰着我很长时间,我想出了一个基于 Apps Script 的不太优雅但高效的解决方案。帕维尔·阿加科夫也有同样的想法!我的版本也适用于多次出现,如果 Google 表单在您编辑文本时删除了换行符,则可以重新运行。


    1. 编辑表单时,从主菜单打开脚本编辑器。

    2. 新建一个脚本,将内容替换为下面的代码。保存并返回到您的表单。

    3. 重新加载页面。您会注意到主菜单中有一个新选项,如下所示

      该“脚本”菜单是由我们的脚本添加的。暂时不要用,效果不大。

    4. 编辑内容时,使用四个空格作为换行符。

    5. 从“脚本”菜单运行脚本。现在庆祝?‍♀️

    一些值得注意的事情:

    • 您将在第一次运行脚本时收到权限请求。没关系,看消息,做你该做的。

    • 一旦出现换行符,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(空格字符),因此它不包含空格以外的任何内容。
    【解决方案2】:

    我发现您无法通过编辑器执行此操作,但可以通过脚本执行此操作。 进入主菜单 -> 脚本编辑器; 将以下代码传递给编辑器;

    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"));
        }
      }
    }

    然后设置触发器以在表单打开时启动此方法。

    【讨论】:

    • 嘿...我想我们肯定想要 .indexOf > 0 ,不是吗?如果你明白了,也许会改变 :) 我们不能只编辑一个字符 :)
    • @MikeM - 不,因为这将执行多次,您只需更换一次即可。至少我的情况是这样的"B:"
    • 啊,我明白你的意思了...我在想字符串中的文本也是 \n 的其他示例。我的坏:)
    • 这对我不起作用。我运行此代码,但没有收到任何 CR: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
    【解决方案3】:

    我自己在这个问题上挣扎了太久! 但是,当您知道它的简单性时: 转到“添加项目” 选择“章节标题” 此选项允许您将分段文本放入表单中。

    【讨论】:

      【解决方案4】:

      很抱歉这个坏消息,但这对我来说似乎是不可能的。

      【讨论】:

        【解决方案5】:

        以上解决方案都不适合我,所以我添加了一个 unicode 字符 https://www.compart.com/en/unicode/U+2002 粘贴 4 到 5 次,这就是它的外观

        【讨论】:

          【解决方案6】:

          我找到了答案!在您正在输入文本的框中,转到“开发人员”选项卡中的“属性”。您将获得一个下拉菜单。菜单底部是“普通测试属性”,带有“允许回车(多段)”复选框。

          【讨论】:

          • 我在哪里可以找到谷歌表单中的开发者标签?我找不到它
          【解决方案7】:

          截至 2018 年 6 月,以下工作(但仅记录了第二个选项):

          1. 只需在说明中添加新行,它就会显示在表单中 - 尝试在段落中使用两个。
          2. 如果您想要更多风格 - 添加“标题和说明” - 请参阅工具栏中显示“Tᴛ”的第二个选项。标题将始终添加额外的空间(即使它是空的),并将任何标题显示为倒置的、更大的文本。如果您只需要“标题”后跟问题,则可以禁用说明。

          【讨论】:

            【解决方案8】:

            这是一个更好的解决方案,但基于上述。它允许您编辑上述解决方案所没有的表单:

            // 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));
              }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-05-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多