简体   繁体   English

Google Apps脚本中的强行换行(带有Google Form字段的Google文档可在项目符号列表中创建新项目符号)

[英]Hard Line Break in Google Apps Script (Google Docs with Google Form Field to create new bullet in bulleted list)

I'm using Google Forms to feed a Google Doc (via Google Sheets), and in this Google Doc I have a pre-existing bulleted list. 我正在使用Google表格(通过Google表格)来提供Google文档,并且在此Google文档中我有一个预先存在的项目符号列表。 I would like to enter code that will add another bullet to the list. 我想输入将另一个项目符号添加到列表中的代码。

My approach has been to add a ###newLine### tag to the end of the last pre-filled bullet in the form. 我的方法是将###newLine###标记添加到表单中最后一个预填项目符号的末尾。 I then used replace.Text(###newLine###) in GAS, and then added '\\n' for a new line. 然后,我在GAS中使用了replace.Text(###newLine###) ,然后在新行中添加了'\\n'

The problem is that this '\\n' inserts a soft line break (like a Shift+Enter), and it doesn't create a new bullet. 问题在于,此'\\n'插入一个换行符(例如Shift + Enter),并且不会创建新的项目符号。 It just creates a new line under the prior bullet. 它只是在前一个项目符号下创建新行。 I have tested within the doc by adding/removing the bullet associated with the above paragraph, and it is clear that this new line is associated with the above paragraph. 我已经通过添加/删除与以上段落相关的项目符号在文档中进行了测试,很明显,此新行与以上段落相关。 What I would like is aa hard line break (like simply pressing Enter) which will create a new bullet. 我想要的是一个强行换行符(例如只需按Enter键),它将创建一个新项目符号。

Here's what the code looks like: 代码如下所示:

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

Also tried: 还尝试了:

body.appendParagraph(). 

This attached to the end of the body and didn't seem to replaceText. 它附着在正文的末尾,似乎没有替换文本。

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

This would put it in the right spot but not as part of the list. 这将把它放在正确的位置,而不是列表的一部分。

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

This appended a numbered list to the end of the body but would not replace text or add to the existing bulleted list. 这会在正文末尾附加一个带编号的列表,但不会替换文本或添加到现有的项目符号列表中。

08-03-19, I tried the following, per Jescanellas's assistance. 在2008年8月3日,我在Jescanellas的协助下尝试了以下方法。 It works perfectly in the original test doc I provided, but I can't port it over to other docs. 它可以在我提供的原始测试文档中完美运行,但是无法将其移植到其他文档中。 I think that's because I'm somehow failing to get the right data to attach it to the list at the right level, but I'm not sure where I'm messing up. 我认为那是因为我不知何故无法获得正确的数据,无法以正确的级别将其附加到列表中,但是我不确定自己在哪里搞砸了。

  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
     }

  }

After some investigating, and thanks to your examples and links I think I got the solution of this. 经过一番调查,并感谢您的示例和链接,我想我已经找到了解决方案。 If there is any issue or I misunderstood something, please tell me and I will correct the post. 如果有任何问题或我误解了某些内容,请告诉我,我将更正该帖子。

This searches for the word ###origApproach### in all the paragraphs (list items) and adds the ###formData### next to it. 这会在所有段落(列表项)中搜索单词###origApproach### ,并在其旁边添加###formData### After this it removes the ###origApproach### paragraph. 此后,它将删除###origApproach###段落。 I commented some lines in case you don't want to remove it. 我注释了一些行,以防您不想删除它。

 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;

    }

  }
}

If you remove the paragraph with the word ###origApproach### change index + 1 to index 如果您删除带有单词###origApproach###的段落,则将index + 1更改为index

EDIT 编辑

If it's changing your glpyh style, you can force it by using the parameters BULLET, HOLLOW_BULLET, NUMBER, etc instead of the variable glyph. 如果要更改glpyh样式,则可以使用参数 BULLET, HOLLOW_BULLET, NUMBER,等代替变量字形来强制使用它。

You can read more about these functions here 您可以在此处阅读有关这些功能的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM