【问题标题】:Hard Line Break in Google Apps Script (Google Docs with Google Form Field to create new bullet in bulleted list)Google Apps 脚本中的硬换行符(使用 Google 表单字段在项目符号列表中创建新项目符号的 Google 文档)
【发布时间】:2019-12-07 05:38:19
【问题描述】:

我正在使用 Google 表单来提供一个 Google 文档(通过 Google 表格),并且在这个 Google 文档中我有一个预先存在的项目符号列表。我想输入将另一个项目符号添加到列表中的代码。

我的方法是在表单中最后一个预填项目符号的末尾添加一个###newLine### 标签。然后我在 GAS 中使用了replace.Text(###newLine###),然后在新行中添加了'\n'

问题是这个'\n' 插入了一个软换行符(如Shift+Enter),它不会创建一个新的项目符号。它只是在前一个项目符号下创建一个新行。我通过添加/删除与上述段落相关的项目符号在文档中进行了测试,很明显,这个新行与上述段落相关联。我想要的是一个硬换行符(就像简单地按 Enter 键一样),它将创建一个新的项目符号。

代码如下:

body.replaceText('###newLine###', '\n' + 'Produces a soft (shift+enter) line break.');

也试过了:

body.appendParagraph(). 

这个附在正文的末尾,似乎没有替换文本。

var insertPar = body.insertParagraph(21, 'test insert paragraph');
body.replaceText('###newBullet###', insertPar);

这会将它放在正确的位置,但不会作为列表的一部分。

var listItemTest = body.appendListItem('#listItemTest#');
body.replaceText('###newBullet###', listItemTest);

这会在正文末尾附加一个编号列表,但不会替换文本或添加到现有的项目符号列表中。

08-03-19,在 Jescanellas 的帮助下,我尝试了以下方法。它在我提供的原始测试文档中完美运行,但我无法将其移植到其他文档。我认为这是因为我无法获得正确的数据以将其附加到正确级别的列表中,但我不确定我在哪里搞砸了。

  var formDataEntered = functionName.values[11] || ''; //This var is retrieved from the sheet attached to a form. It's the submitted data.
  var listItem = body.getListItems(); //We're getting the list.

   for (var i = 0; i < listItem.length;i++){ //We're creating a loop here.
     var item = body.getListItems()[i]; //This gets the list and applies the loop to it.

     if ((item.findText('###bulletTestPlaceholder###')) && (formDataEntered != '')){ //The ###bulletTestPlaceholder### is just a placeholder in the doc where I want to insert the bullet. Your purpose with the item.findText is to identify the list level we're going for - NOT to use the text itself as a placeholder and replace the text. 

        var index = body.getChildIndex(item); //You're getting all the data about var item (where we got the list and applied the loop).
        var level = item.getNestingLevel();  //This gets the nesting level of var item. I'm wondering if this might be the issue as it's not specific to the findText('###bulletTestPlaceholder###')?
        var glyph = item.getGlyphType(); //This gets the bullet type. 

        body.insertListItem((index + 1), formDataEntered).setNestingLevel(level).setGlyphType(glyph); //This is the location in the list where teh bullet will be placed. It also sets the nesting level and glyph type. I've tried playing with the nesting level using integers, but that doesn't fix it.

        item.replaceText('###bulletTestPlaceholder###',''); //removes '###bulletTestPlaceholder###' text after it's no longer needed.

        break; //stops the loop from looping multiple times.

     } else if ((item.findText('###bulletTestPlaceholder###')) && (formDataEntered == '')) {
       item.replaceText('###bulletTestPlaceholder###',''); //removes '###bulletTestPlaceholder###' text and avoids new line if no formDataEntered
     }

  }

【问题讨论】:

  • 嗨,汤姆,欢迎来到 Stack Overflow!如果您可以编辑它以包含您迄今为止编写的代码,它将改善您的问题:)
  • 感谢您的反馈。我已经添加了相关行。
  • body.appendParagraph()添加一个新段落怎么样?
  • @Jescanellas,感谢您的回复。我用我尝试过的新东西(包括 body.appendParagraph)编辑了我的原始帖子。刚刚发现这一点,我认为解决方案可能在这里(stackoverflow.com/questions/17773938/…),但我明天必须回来。感谢您的帮助和耐心。我对这些东西很陌生。
  • 抱歉,现在我不确定您要做什么。我知道您有一个项目符号列表,其中包含多个项目,并且您将 ###newBullet## 添加到最后一个项目以将其替换为其他文本。最终目标是将项目添加到列表中吗?你能发布一个列表的例子和你想要的结果吗?谢谢。

标签: javascript regex google-apps-script google-docs line-breaks


【解决方案1】:

经过一番调查,感谢您的示例和links,我想我得到了解决方案。如果有任何问题或我误解了什么,请告诉我,我会更正帖子。

这会在所有段落(列表项)中搜索单词###origApproach###,并在其旁边添加###formData###。在此之后,它会删除 ###origApproach### 段落。我注释了一些行以防您不想删除它。

 function myFunction() {

  var body = DocumentApp.getActiveDocument().getBody();
  var listItem = body.getListItems();

   for (var i = 0; i < listItem.length;i++){
     var item = body.getListItems()[i];

     if (item.findText('###origApproach###')){

        var index = body.getChildIndex(item);
        var level = item.getNestingLevel();      
        var glyph = item.getGlyphType();
        //In case the indentation is changed:
        var indentFirst = item.getIndentFirstLine();
        var indentStart = item.getIndentStart();

        //Added both setIndents to fix the indentation issue. 
        body.insertListItem((index + 1), '###formData###').setNestingLevel(level).setGlyphType(glyph).setIndentFirstLine(indentFirst).setIndentStart(indent);
        body.removeChild(item); //Comment this if you don't want to remove the  ###origApproach### paragraph

        //Uncomment this if you want to keep the paragraph and remove ###origApproach###
        //item.replaceText('###origApproach###','');
        break;

    }

  }
}

如果删除带有单词###origApproach### 的段落,请将index + 1 更改为index

编辑

如果它改变了您的 glpyh 样式,您可以通过使用 parameters BULLET, HOLLOW_BULLET, NUMBER, 等而不是变量字形来强制它。

您可以阅读更多关于这些功能的信息here

【讨论】:

  • @Jescanelllas,谢谢!它似乎工作!我必须稍微尝试一下才能使其在我的原始文档中工作,但这是我的错,因为示例的结构不同。我应该考虑到这一点。去解构并尝试从你在这里所做的事情中学习。如果可以的话,我可能会问你更多问题。谢谢你帮助我!这比我预期的要复杂(但是,我还是超级绿色,所以现在一切都令人惊讶)。
  • 我已为您投票,但收到此消息“感谢您的反馈!声望低于 15 人的投票将被记录,但不要更改公开显示的帖子得分。”只是想让您知道您的帮助并非不受欢迎,@Jescanellas。
  • 它在之前的测试文档中完美运行,但是当我将它插入新文档时,它会更改列表类型和项目符号缩进。以下是正在发生的事情:(docs.google.com/document/d/…)。我想我一定错过了一些非常简单的东西。我已经编辑了我的问题,以展示我玩过的东西以及我如何理解你的代码的工作方式。我试图让它更直观一点。感谢您的耐心和帮助!
  • 不客气 :)。我不明白为什么要更改缩进,因为当我进行测试时,它显示单词放置正确,就像其他项目符号一样。我编辑了我的答案来解决这个问题。
  • 谢谢!这就说得通了。因此,我们指定项目符号类型和缩进来覆盖导致更改的任何内容。好奇,对我来说,我将主要列表更改为编号列表。知道是什么原因造成的吗?这是插入工作的示例,但列表类型已更改。 docs.google.com/document/d/…
猜你喜欢
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多