I need help with a Google Apps Script for my note-taking in Google Docs. I want to highlight different parts of my list items automatically to make reviewing easier. Here’s my current code:
function colorizeNotes() {
const document = DocumentApp.getActiveDocument();
const mainContent = document.getBody();
const bulletPoints = mainContent.getListItems();
bulletPoints.forEach((point, index) => {
if (point.getNestingLevel() === 0) {
point.setBackgroundColor('#FFFF00');
}
});
}
I’m getting an error: TypeError: Cannot read property 'getNestingLevel' of undefined. What’s wrong with my script? I’m new to JavaScript and Google Apps Script, so I might be missing something obvious. Any tips on fixing this and making it work?
I’ve dealt with similar issues in Google Apps Script before. The problem likely stems from how you’re retrieving list items. Instead of using getListItems(), try iterating through all body elements. This approach is more reliable, especially with complex documents.
Here’s a modified version of your script that should work:
function colorizeNotes() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const numChildren = body.getNumChildren();
for (let i = 0; i < numChildren; i++) {
let element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.LIST_ITEM) {
let listItem = element.asListItem();
if (listItem.getNestingLevel() === 0) {
listItem.setBackgroundColor('#FFFF00');
}
}
}
}
This should highlight top-level list items without errors. If you need to handle nested items differently, just adjust the nesting level condition. Let me know if you need any clarification!
hey ethan, i’ve run into this before. the problem’s probably with how ur getting list items. try this:
function colorizeNotes() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
for (var i = 0; i < body.getNumChildren(); i++) {
var elem = body.getChild(i);
if (elem.getType() == DocumentApp.ElementType.LIST_ITEM) {
var item = elem.asListItem();
if (item.getNestingLevel() == 0) {
item.setBackgroundColor('#FFFF00');
}
}
}
}
this should work better. lemme know if u need more help!
I’ve encountered similar issues when working with Google Apps Script and Google Docs. The error you’re getting suggests that some list items in your document might not be recognized correctly. Here’s what worked for me:
Instead of using getListItems(), try iterating through all elements in the document body. This approach catches all list items, even if they’re nested in other structures. Here’s a modified version of your script that should work:
function colorizeNotes() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const numChildren = body.getNumChildren();
for (let i = 0; i < numChildren; i++) {
let element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.LIST_ITEM) {
let listItem = element.asListItem();
if (listItem.getNestingLevel() === 0) {
listItem.setBackgroundColor('#FFFF00');
}
}
}
}
This should highlight all top-level list items without throwing errors. If you want to highlight nested items differently, you can adjust the condition for the nesting level. Hope this helps!