简体   繁体   中英

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. 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. I then used replace.Text(###newLine###) in GAS, and then added '\\n' for a new line.

The problem is that this '\\n' inserts a soft line break (like a Shift+Enter), and it doesn't create a new bullet. 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.

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. 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. After this it removes the ###origApproach### paragraph. 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

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.

You can read more about these functions here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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