Reputation:
I have google document which contains list items. Now I want to get the values of those list items. For example, document contains this:
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);
}
Upvotes: 0
Views: 4009
Reputation: 201513
I believe your goal is as follows.
You want to retrieve the glyph from the list items by searching the text.
For example, when there is the following list,
1. Text New
2. Text Old
By searching Text New
and Text Old
, you want to retrieve the glyphs of 1.
and 2.
.
Unfortunately, in the current stage, it seems that the glyph cannot be directly retrieved. This has already been mentioned in the comment and the existing answer.
Fortunately, the glyph type can be retrieved by Google Document service. And, the glyph format can be retrieved Google Docs API. I thought that when these are used, a workaround might be able to be proposed. So, this is just my try. I'm not sure whether this method can be used for all situations. So please be careful about this.
This script uses Docs API. So please enable Docs API at Advanced Google services.
function myFunction() {
// Please set the searching texts in the list.
// const searchText = ["One One One", "Two Two Two", "ii ii ii ii ii", "C", "Factory Floor"];
// In this case, all list itmes are retrieved.
const searchText = DocumentApp.getActiveDocument().getBody().getListItems().map(e => e.getText().trim());
// This is an object for the numbers with the GlyphType. When you want to use more numbers and GlyphTypes, please add them.
const numObj = {
"NUMBER": ["1", "2", "3", "4", "5", "6"],
"LATIN_LOWER": ["a", "b", "c", "d", "e", "f"],
"LATIN_UPPER": ["A", "B", "C", "D", "E", "F"],
"ROMAN_LOWER": ["i", "ii", "iii", "iv", "v", "vi"],
"ROMAN_UPPER": ["I", "II", "III", "IV", "V", "VI"],
"GLYPH_TYPE_UNSPECIFIED": ["", "", "", "", "", ""],
"noGlyphType": ["", "", "", "", "", ""],
};
// 1. Retrieve list object using Docs API.
const doc = DocumentApp.getActiveDocument();
const listObj = Docs.Documents.get(doc.getId(), { fields: "lists" }).lists;
// 2. Retrieve list items using Document service and create an object for searching the texts.
const listItems = doc.getBody().getListItems();
const obj = listItems.reduce((o, e) => {
const listId = e.getListId();
const glyphType = e.getGlyphType() || "GLYPH_TYPE_UNSPECIFIED";
const nestingLevel = e.getNestingLevel().toString();
const text = e.getText();
if (o[listId]) {
if (o[listId][nestingLevel]) {
if (o[listId][nestingLevel][glyphType]) {
o[listId][nestingLevel][glyphType].text.push(text);
} else {
o[listId][nestingLevel] = { [glyphType]: { text: [text], glyphFormat: listObj[listId].listProperties.nestingLevels[nestingLevel].glyphFormat, nestingLevel } };
}
} else {
o[listId][nestingLevel] = { [glyphType]: { text: [text], glyphFormat: listObj[listId].listProperties.nestingLevels[nestingLevel].glyphFormat, nestingLevel } };
}
} else {
o[listId] = { [nestingLevel]: { [glyphType]: { text: [text], glyphFormat: listObj[listId].listProperties.nestingLevels[nestingLevel].glyphFormat, nestingLevel } } };
}
return o;
}, {});
// 3. Search text and output an object for retrieving glyphs.
const getRes = (obj, search, res = [], parents = []) => {
Object.entries(obj).forEach(([k, v]) => {
if (Array.isArray(v)) {
v = v.map(vv => vv.trim());
const temp = search.filter(s => v.includes(s));
if (temp.length > 0) {
const parent = parents[parents.length - 1];
res.push(temp.map(e => ({ search: e, k: parent.k == "null" ? "noGlyphType" : parent.k, idx: v.indexOf(e), glyphFormat: parent.v.glyphFormat, nestingLevel: parent.v.nestingLevel })));
}
} else if (typeof v == "object") {
parents.push({ k, v });
getRes(v, search, res, parents);
}
});
return search.map(s => res.flat().filter(t => t.search == s)[0]);
}
const res = getRes(obj, searchText).map(e => {
const count = [...e.glyphFormat].reduce((c, e) => e == "%" ? c + 1 : c, 0);
return e ? e.glyphFormat.replace(/%([0-9]+)/g, (_, p, o) => count > 1 ? numObj[e.k][o == 0 ? Number(p) : e.idx] : numObj[e.k][e.idx]) : "Text was not found."
});
console.log(res);
}
When the above script is run, all list items are retrieved and the glyphs are returned. And, for example, when your sample Document shown in the following image is used and const searchText = ["One One One", "Two Two Two", "ii ii ii ii ii", "C", "Factory Floor"];
is used as the search texts,
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
.
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());
Unfortunately, in the current stage, it seems that the glyph type of check box type cannot be retrieved.
Upvotes: 1
Reputation: 6072
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
.
Upvotes: 1