【发布时间】: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