如何在 Google 文档中添加清单?

问题描述 投票:0回答:2

如何使用 JavaScript 在 Google 文档中添加清单。我正在插入多个关键字,我想将其应用为项目符号点,用户可以在其中单击和取消单击。

javascript google-apps-script google-docs
2个回答
3
投票

不幸的是,现在看起来不可能,但有一个解决方法。

当您

getType()
清单
Element
时,您确实会得到'ListItem`类型。

从逻辑上讲,这意味着复选框应该是

GlyphType
,但没有与清单相对应的记录类型。如果您尝试在手动创建的检查清单上执行
getGlyphType()
,您会得到
null
。当然,如果您尝试
setGlyphType(null)
,您不会看到复选框。

因此,通过阅读文档和一些测试,我们不得不得出结论,不幸的是,目前这是不可能的。

但是,您可以使用

copy()
方法按照以下方式创建清单的独立副本:

body.appendListItem(listItem.copy()));

当然,清单可以位于不同的文档中。我没有找到一种方法来操作检查列表文本值(它似乎覆盖了所有内容),因此您可能需要事先在单独的文档中预定义列表项。


-1
投票

我通过 Google Docs API 在 Python 中这样做:

# open the doc
doc = service.documents().get(documentId=docId).execute()

# iterate thru the content sections finding the highest endIndex value,
# afterwhich I will add my checklist
content = doc['body']['content']
nextIndex = max([v['endIndex'] for v in content]) + 1

# create a set of batchUpdate requests to insert each line of my checklist array of strings
requests = []
for line in checklist:
    requests.append({
        'insertText': {
            'text': line + '\n',
            'endOfSegmentLocation': {}     # using this will add lines to end of doc
        }
    })

# add the clecklist text lines 
service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()

# get the updated doc
doc = service.documents().get(documentId=docId).execute()

# find the new 'endIndex' value
content = doc['body']['content']
lastIndex = max([v['endIndex'] for v in content])

# create a request to set the range of my checklist to a BULLET_CHECKBOX list
requests = [
    {
        'createParagraphBullets': {
            'range': {
                'startIndex': nextIndex,
                'endIndex': lastIndex
            },
            'bulletPreset': 'BULLET_CHECKBOX'
        },
    }
]
service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()
© www.soinside.com 2019 - 2024. All rights reserved.