UNPKG

vuexcel-utils

Version:
49 lines (45 loc) 1.62 kB
/** * 弹出一个选择框获取选中的位置 * @see https://dev.office.com/reference/add-ins/shared/bindings.addfrompromptasync * @param {string} type - 选择框单个格子还是复数格子 [text, matrix] * @param [object, string] options - 弹出框的相关配置,参阅office 官方 单独传入则变为选择框文字 * @return {Promise} 包含地址的promise * */ import RequestContext = Excel.RequestContext; export default function (options: string | Object | null = null) { // 转换传入的options if (typeof options === 'string') { options = { promptText: options } } /** * 调用Office的系统对话框 * 要获取区域只能使用Matrix * */ // 正式获取 return new Promise((resolve, reject) => { Office.context.document.bindings.addFromPromptAsync(Office.BindingType.Matrix, options, function (asyncResult: any) { if (asyncResult.status === 'succeeded') { Excel.run(function (ctx: RequestContext) { let id = asyncResult.value.id let binding = ctx.workbook.bindings.getItem(id) let range = binding.getRange() range.load('address') return ctx.sync() .then(function () { Office.context.document.bindings.releaseByIdAsync(asyncResult.value.id) resolve(range.address) }) }) .catch((e: Error) => { reject(e) Office.context.document.bindings.releaseByIdAsync(asyncResult.value.id) }) // 删除刚刚绑定的id } else { reject(asyncResult) } }) }) }