|
- function Region() {
- if (!(this instanceof Region)) throw new Error("请使用new实例化对象")
- //if (Region._instance != null) return Region._instance
- const cityTree = main()
- const menuName = "省市县级联菜单"
- try { CommandBars.Item(menuName).Delete() } catch {}
- const popupMenu = CommandBars.Add(menuName, msoBarPopup) // 弹出菜单
- popupMenu.Control = popupMenu // 扩充一个属性,方便递归
- const setPopupMenuControl = function(cityTree, popupControl, addr) {
- let cities = Object.keys(cityTree)
- let subPopupMenu, subTree
- cities.forEach(city => {
- subTree = cityTree[city]
- if (subTree != null) {
- if (city != addr) {
- subPopupMenu = popupControl.Control.Controls.Add(msoControlPopup)
- setPopupMenuControl(subTree, subPopupMenu, addr + city + ",") // 递归添加组件
- } else {
- subPopupMenu = popupControl // 减少一个层级
- setPopupMenuControl(subTree, subPopupMenu, city) // 递归添加组件
- }
- } else {
- subPopupMenu = popupControl.Control.Controls.Add(msoControlButton)
- subPopupMenu.Tag = addr + city
- subPopupMenu.OnAction = "setCity"
- }
- subPopupMenu.Caption = city
- })
- }
- setPopupMenuControl(cityTree, popupMenu, "")
-
- let city = ""
- const P = Region.prototype
- setCity = () => { city = CommandBars.ActionControl.Tag }
- P.getCity = () => { // 获取选择的城市
- city = ""
- CommandBars.Item(menuName).ShowPopup()
- return city.replace(",", "")
- }
- P.getCities = () => { // 获取选择的城市数组
- CommandBars.Item(menuName).ShowPopup()
- let cities = city.split(",")
- return cities.length > 0 ? cities : []
- }
- return Region._instance = this
- }
- function setCity() {} // 弹出菜单组件中的OnAction指向,只为服务于Region对象
- function Workbook_SheetSelectionChange(sht, rng) {
- if (rng.Count > 1||sht.Name!="菜单") return
- Application.Cursor=xlDefault
- rng.Value2 = new Region().getCity()
- }
- function Workbook_SheetBeforeRightClick(Sh, rg, cancel)
- {
- if (rg.Count > 1||Sh.Name!="右键菜单") return
- Application.CommandBars("Cell").Enabled = false
- Application.Cursor=xlDefault
- // const arr = [new Region().getCities()]
- // rg.Resize(arr.length, arr[0].length).Value2 = arr
- const arr = new Region().getCities()
- rg.Resize(1, arr.length).Value2 = arr
- }
复制代码
|
|