Docs typos: Defined Names

33 views
Skip to first unread message

Darrell Reich

unread,
Aug 7, 2023, 11:34:29 AM8/7/23
to openpyxl-users
The example in the link below has two typos preventing it from working:
* f-string missing "f" in front of ref = line
* last line add using variable that does not exist? (new_range)


Creating a Global Definition

Global definitions are assigned to the workbook collection:

from openpyxl import Workbook from openpyxl.workbook.defined_name import DefinedName from openpyxl.utils import quote_sheetname, absolute_coordinate wb = Workbook() ws = wb.active # make sure sheetnames and cell references are quoted correctly ref = "{quote_sheetname(ws.title)}!{absolute_coordinate('A1:A5')}" defn = DefinedName("global_range", attr_text=ref) wb.defined_names["global_range"] = defn # key and `name` must be the same, the `.add()` method makes this easy wb.defined_names.add(new_range)
Reply all
Reply to author
Forward
0 new messages