如何在ExcelScript中访问Excel工作簿内容类型属性并转换VBA脚本?
可行,实现步骤如下:
1. 编写Excel Script脚本
打开Excel Online,点击顶部菜单栏的「自动化」>「脚本编辑器」,将默认代码替换为以下内容:
function main(workbook: ExcelScript.Workbook) { // 替换为你需要获取的属性名称 const targetPropertyName = "目标属性名"; // 获取内容类型属性集合 const contentTypeProps = workbook.getContentTypeProperties(); try { // 获取指定属性并读取值 const property = contentTypeProps.getItem(targetPropertyName); const propertyValue = property.getValue() as string; // 示例:将属性值写入A1单元格,可按需修改写入位置 workbook.getActiveWorksheet().getRange("A1").setValue(propertyValue); } catch (error) { console.log(`未找到属性 ${targetPropertyName} 或获取失败: ${error}`); } }
2. 设置打开时自动触发
- 在脚本编辑器界面,点击右上角的三个点图标(更多选项),选择「添加触发器」
- 在触发器设置面板中,选择「工作簿打开时」作为触发事件,保存设置即可
补充:实现自定义函数(类似原VBA的调用方式)
如果需要像原VBA一样在单元格直接调用获取属性,可编写Excel Script自定义函数:
/** * 获取SharePoint内容类型属性值 * @customfunction * @param propertyName 属性名称 * @returns 属性值或错误信息 */ function getSharePointProperty(propertyName: string): string { const workbook = ExcelScript.getWorkbook(); const contentTypeProps = workbook.getContentTypeProperties(); try { const property = contentTypeProps.getItem(propertyName); return property.getValue() as string; } catch (error) { return `获取失败: ${error}`; } }
使用时在单元格输入=getSharePointProperty("你的属性名称")即可调用。
内容的提问来源于stack exchange,提问作者Benedikt
相关产品推荐
相关产品推荐

