Bug in google doc list item management?

245 views
Skip to first unread message

Robin MANAL

unread,
Dec 7, 2021, 9:51:19 AM12/7/21
to Google Apps Script Community

I have developped a script to transform paragraphs into list item, using a specific list item model (StyleT1 in the doc). I have a very strange behavior: 1/ if you select the first paragraph of the doc and launch the script, it works well and the paragraph become a list item copying the format of StyleT1 2/ if you select the second paragraph and apply the script, then all the list item paragraph become with a number instead of a bullet

I think it is clearly a bug: what do you think?

https://docs.google.com/document/d/16i4nzxj0ptIgjaD6pjVedJqi3wwlw-PtN-W-AGOVuoU/edit?usp=sharing

function manageStyles() {
try {

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var elementTable = [];
var paragraphe, ParagrapheIndex, Text;

var selectionRange = doc.getSelection() ? doc.getSelection().getRangeElements() : null;

if (!selectionRange) {
  console.log("No selection!");
  return;
}

var ListT1 = body.findText("StyleT1");    
ListT1 = ListT1.getElement().getParent();    

for (var i=0; i < selectionRange.length; i++){
 elementTable[i] = selectionRange[i].getElement();
 }

for (var i=0; i < elementTable.length; i++){
  if (elementTable[i].getType() == DocumentApp.ElementType.TEXT) {
    paragraphe = elementTable[i].getParent();     
  } else if (elementTable[i].getType() == DocumentApp.ElementType.PARAGRAPH || elementTable[i].getType() == DocumentApp.ElementType.LIST_ITEM) {
    paragraphe = elementTable[i];
  } else {
    console.log("Not applicable the selection!");
    continue;
  }

Text = paragraphe.getText();
ParagrapheIndex = body.getChildIndex(paragraphe);
body.removeChild(paragraphe);
paragraphe = body.insertListItem(ParagrapheIndex,Text); 
paragraphe.setListId(ListT1);  
/*paragraphe.setGlyphType(DocumentApp.GlyphType.BULLET);
paragraphe.setNestingLevel(0);  */          

  
} // fin du for
} catch (e) {
console.log("Error in manageStyles " + e.toString());
}
}

Thx Rman

Martin Hawksey

unread,
Dec 7, 2021, 5:48:55 PM12/7/21
to Google Apps Script Community
I'm afraid I don't entirely follow what you are doing, but things I know glyphtype are easily overwritten/lost when inserting lists. If you are applying particular formatting it's sometimes better to hardcode. Pulled together from a couple of examples does the following help (looks for a para starting ## and converts to a bullet list)?

function manageStyles2(){
  const ListT1 = {
    [DocumentApp.Attribute.FONT_FAMILY]: "Droid Sans",
    [DocumentApp.Attribute.FONT_SIZE]: 12,
    [DocumentApp.Attribute.BOLD]: true
  };

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paras = body.getParagraphs();
  for (var i = 0; i < paras.length; i++) {
    if (paras[i].editAsText().getText().indexOf("##") == 0) {
      // This element is a future list item
      var elem = paras[i];
      // What's the text going to be?
      var text = elem.editAsText().getText()
      // Where will it be inserted?
      var childIndex = body.getChildIndex(elem)
      // I should remove the old element
      body.removeChild(elem)
      // And take it's place with this new one with Glyph.
      body.insertListItem(childIndex, text).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes(ListT1);
    }
  }
}

Robin MANAL

unread,
Dec 8, 2021, 5:02:52 AM12/8/21
to Google Apps Script Community
Thx for your answer
My purpose is to apply a specific format as list to a paragraph (should be starting with ## like in your example).
The issue is that for specific format it does not work at all, as you mentioned.
For hardcoding, that i have tried, it is not possible to set the specific glyph that you can select manually in the menu of google doc. For example the start glyph i use in my doc is not selectable in google script.

Martin Hawksey

unread,
Dec 8, 2021, 4:33:29 PM12/8/21
to Google Apps Script Community
Ah ok - so in summary you have a list in your doc with a custom bullet style and for all paragraphs that start with an indicator (e.g. ##) you want them to use that same custom bullet style? 

Robin MANAL

unread,
Dec 9, 2021, 4:11:58 AM12/9/21
to Google Apps Script Community
Yes.
As I can not reproduce with the function the style i want, i have tried to overpass that by setting a specific paragraph with my style and to apply it to the new i create.
But it does not work.
Reply all
Reply to author
Forward
0 new messages