Home > Mobile >  How to get value of List Items using Google Apps Script
How to get value of List Items using Google Apps Script

Time:10-27

I have google document which contains list items. Now I want to get the values of those list items. For example, document contains this:

  1. Text New
  2. Text Old

How can I get list items (1,2)?

I used the following code, but it just gives me an array of list item objects but not the values of these list items.

function docFunction() {
  var doc = DocumentApp.getActiveDocument();
  var docBody = doc.getBody();
  var docText = docBody.getListItems();
 
   Logger.log(docText);
 
}

CodePudding user response:

There is not way to retrieve the values you are needing.

Moreover, ListItem refers to the item in the list itself, not the Glyph.

The closest thing to getting the information you want is by using the getGlyphType which will return the type of glyph you are using, in this case NUMBER.

Reference

  • enter image description here

    the following result is obtained.

    [ '1.', '2.', 'ii.', '(A)', '1.3' ]
    

    Unfortunately, I couldn't find the method for identifying I of I1. and I2.. By this, the glyph of Factory Floor becomes 1.3.

    Note:

    • In the current stage, in my proposed script, I couldn't find the method for identifying I of I1. and I2.. When I could find the method, I would like to update my sample script.

    • This is just my try. I'm not sure whether this method can be used for all situations. So please be careful about this.

    • When you want to retrieve the glyphs for all list items, please modify the above script as follows.

      • From

          const searchText = ["One One One", "Two Two Two", "ii ii ii ii ii", "C", "Factory Floor"];
        
      • To

          const searchText = DocumentApp.getActiveDocument().getBody().getListItems().map(e => e.getText().trim());
        

    References:

  • Related