I'm trying to write some code in google app scripts to detect if a ListItem has any italicized styling. listItem.getAttributes()[DocumentApp.Attribute.ITALIC] will only return true if the entire listItem is italicized, and null if some or none of the text is italicized. Is there any way to get true if even a single character has italicized style?
In your situation, how about the following sample script?
Please copy and paste the following script to the script editor of your Google Document and save the script.
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const listItems = body.getListItems();
listItems.forEach(e => {
const textObj = e.editAsText();
const text = textObj.getText();
const isItalic = [...Array(text.length)].some((_, i) => textObj.isItalic(i));
console.log({ isItalic, text });
});
}
When this script is used in the following sample Document,
the following result is obtained by console.log({ isItalic, text })
. In this sample, text
of sample text 1
and 3
of sample text 3
are set as the italic type.
{ isItalic: true, text: 'sample text 1' }
{ isItalic: false, text: 'sample text 2' }
{ isItalic: true, text: 'sample text 3' }
You can see that sample text 1
and sample text 3
return true
.
listItems.forEach(e => {
to [listItems[0]].forEach(e => {
.