繁体   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)

我正在使用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);

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

在2008年8月3日,我在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
     }

  }

经过一番调查,并感谢您的示例和链接,我想我已经找到了解决方案。 如果有任何问题或我误解了某些内容,请告诉我,我将更正该帖子。

这会在所有段落(列表项)中搜索单词###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样式,则可以使用参数 BULLET, HOLLOW_BULLET, NUMBER,等代替变量字形来强制使用它。

您可以在此处阅读有关这些功能的更多信息

暂无
暂无

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

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