automating-excel

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Automating Excel (JXA-first, AppleScript discovery)

在macOS上自动化Excel(优先使用JXA,结合AppleScript探索)

Relationship to the macOS automation skill

与macOS自动化技能的关系

  • Standalone for Excel, but aligned with
    automating-mac-apps
    patterns.
  • Use
    automating-mac-apps
    for permissions, shell, and UI scripting guidance.
  • PyXA Installation: To use PyXA examples in this skill, see the installation instructions in
    automating-mac-apps
    skill (PyXA Installation section).
  • 是针对Excel的独立技能,但与
    automating-mac-apps
    的模式保持一致。
  • 如需权限、Shell和UI脚本相关指导,请参考
    automating-mac-apps
    技能。
  • PyXA安装:若要使用本技能中的PyXA示例,请查看
    automating-mac-apps
    技能中的安装说明(PyXA安装章节)。

Core Framing

核心框架

  • Excel AppleScript dictionary is AppleScript-first; use Script Editor for discovery.
  • JXA is the production language for logic and data processing.
  • Collections are specifiers; read via methods, set via assignments.
  • Handle errors from Excel operations using try/catch blocks and Application error checking.
  • Excel的AppleScript字典以AppleScript为优先;可使用Script Editor进行探索。
  • JXA是用于逻辑和数据处理的生产级语言。
  • 集合是指定符;通过方法读取,通过赋值设置。
  • 使用try/catch块和应用程序错误检查来处理Excel操作中的错误。

Workflow (default)

工作流(默认)

  1. Discover dictionary terms in Script Editor (Excel).
  2. Prototype a minimal AppleScript command.
  3. Port to JXA and add defensive checks.
  4. Use bulk read/write (2D arrays) for performance.
  5. Use VBA
    run()
    when dictionary coverage is missing.
  1. 在Script Editor(Excel)中探索字典术语。
  2. 编写最小化的AppleScript命令原型。
  3. 移植到JXA并添加防御性检查。
  4. 使用批量读/写(二维数组)提升性能。
  5. 当字典未覆盖相关功能时,使用VBA的
    run()
    方法。

Validation Steps

验证步骤

  • Test with empty documents to verify error handling
  • Verify data integrity after batch operations
  • Check Excel UI responsiveness after automation runs
  • Log errors with specific Excel object paths for debugging
  • 使用空文档测试以验证错误处理能力
  • 批量操作后验证数据完整性
  • 自动化运行后检查Excel UI的响应性
  • 记录包含具体Excel对象路径的错误信息以用于调试

Examples

示例

Basic workbook read:
javascript
const Excel = Application('Microsoft Excel');
const workbook = Excel.workbooks[0];
const worksheet = workbook.worksheets['Sheet1'];
const range = worksheet.ranges['A1:B10'];
const data = range.value();  // Returns 2D array
Bulk write with performance toggle:
javascript
Excel.screenUpdating = false;
Excel.calculation = 'manual';
try {
  const range = worksheet.ranges['C1:D100'];
  range.value = my2DArray;
} finally {
  Excel.calculate();
  Excel.screenUpdating = true;
}
VBA escape hatch:
javascript
Excel.run('MyMacro', {arg1: 'value', arg2: 123});  // Calls VBA subroutine
基础工作簿读取:
javascript
const Excel = Application('Microsoft Excel');
const workbook = Excel.workbooks[0];
const worksheet = workbook.worksheets['Sheet1'];
const range = worksheet.ranges['A1:B10'];
const data = range.value();  // Returns 2D array
带性能开关的批量写入:
javascript
Excel.screenUpdating = false;
Excel.calculation = 'manual';
try {
  const range = worksheet.ranges['C1:D100'];
  range.value = my2DArray;
} finally {
  Excel.calculate();
  Excel.screenUpdating = true;
}
VBA应急方案:
javascript
Excel.run('MyMacro', {arg1: 'value', arg2: 123});  // Calls VBA subroutine

When Not to Use

不适用场景

  • For general macOS automation without Excel involvement
  • When AppleScript alone suffices (no JXA logic needed)
  • For Excel tasks requiring complex UI interactions (use
    automating-mac-apps
    for that)
  • When cross-platform compatibility is required
  • 无需涉及Excel的通用macOS自动化场景
  • 仅使用AppleScript即可满足需求的场景(无需JXA逻辑)
  • 需要复杂UI交互的Excel任务(此类场景请使用
    automating-mac-apps
    技能)
  • 需要跨平台兼容性的场景

What to load

需加载的资源

  • JXA Excel basics:
    automating-excel/references/excel-basics.md
  • Recipes (ranges, 2D arrays, formatting):
    automating-excel/references/excel-recipes.md
  • Advanced patterns (performance toggles, VBA bridge):
    automating-excel/references/excel-advanced.md
  • Pivot table guidance:
    automating-excel/references/excel-pivots.md
  • Charting guidance:
    automating-excel/references/excel-charts.md
  • Dictionary translation table:
    automating-excel/references/excel-dictionary.md
  • PyXA (Python) alternative:
    automating-excel/references/excel-pyxa.md
  • JXA Excel基础:
    automating-excel/references/excel-basics.md
  • 操作指南(单元格区域、二维数组、格式设置):
    automating-excel/references/excel-recipes.md
  • 高级模式(性能开关、VBA桥接):
    automating-excel/references/excel-advanced.md
  • 数据透视表指南:
    automating-excel/references/excel-pivots.md
  • 图表指南:
    automating-excel/references/excel-charts.md
  • 字典转换表:
    automating-excel/references/excel-dictionary.md
  • PyXA(Python)替代方案:
    automating-excel/references/excel-pyxa.md