将 VBA 宏转换为 Apps 脚本
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
准备工作
您可能需要在转换文件之前更新 VBA 代码中不兼容的 API。对于状态为完全支持的文件,您可以继续执行第 1 步:转换文件。
修改不兼容的 VBA API
对于状态为支持但需采取变通方法或需要调查的文件,您可以在 Apps 脚本中转换后应用变通方法和修复,但我们建议您先修改熟悉的 VBA 代码,然后再转换文件。
对于兼容性报告中标记为“支持但需采取变通措施”或“需要调查”的每个 API,我们建议执行以下操作:
第 1 步:转换文件
- 在电脑上打开 Google 云端硬盘。
- 在右侧面板上,点击宏转换器插件
。如果您没有看到侧边栏,请点击右下角的“显示侧边栏”图标 chevron_left。
- 点击添加文件和文件夹。宏转换器仅识别 Excel 文件。
- 选择要转换的文件或文件夹,然后点击选择。每次选择的文件数量不得超过 2,000 个。
- 如需更改转换后文件的保存位置,请点击“更改目标文件夹”图标 edit,然后选择所需文件夹。否则,文件会保存在“我的云端硬盘”文件夹中。
- 点击转换。
- 转换完成后,点击查看结果。
第 2 步:测试转换后的文件
运行 Apps 脚本代码
转换文件后,请测试 Apps 脚本函数。使用您通常在 Excel 文件中使用的数据测试转换后的文件。如果可能,请将转换后的 Google 表格文件的输出与原始 Excel 文件的输出进行比较。
测试触发器
如果您的文件包含 onOpen()
、onEdit()
或 onClick()
等触发器,请同时测试这些触发器。部分 VBA 触发器不会自动转换,需要在 Apps 脚本中进行处理。请参阅解决常见问题。
查看 ReadMe 文件
如果转换后的文件附带生成了 ReadMe 文件,请查看 ReadMe 文件中列出的转换问题。
- 如果这些问题可能会对您尚未测试的用例造成影响,请将建议的更改应用到您的代码中。
- 如果您已测试所有可能的场景,并且一切都按预期运行,则可能无需进行更改。
第 3 步:修正错误
如果您在测试文件时遇到错误,请参阅修正转换后的代码中的错误。
如果代码运行没有错误,但结果不符合您的预期,请打开文件的 ReadMe 文件。请查看每个部分,以帮助确定问题的原因并应用建议的修复措施。
修复错误后,请再次测试该文件,确保一切正常运行。
相关文章
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Macro Converter add-on helps you convert your VBA macros to Apps Script, allowing them to run in Google Sheets.\u003c/p\u003e\n"],["\u003cp\u003eBefore converting, review the compatibility report and modify or remove incompatible VBA APIs for better conversion results.\u003c/p\u003e\n"],["\u003cp\u003eThe conversion process involves adding files to the add-on, initiating the conversion, and then testing the converted Apps Script functions and triggers in Google Sheets.\u003c/p\u003e\n"],["\u003cp\u003eIf errors occur or the results are unexpected, consult the generated ReadMe files and the troubleshooting documentation to fix issues and ensure proper functionality.\u003c/p\u003e\n"],["\u003cp\u003eAfter successful conversion and testing, your macros will be operational within Google Sheets, leveraging the capabilities of Apps Script.\u003c/p\u003e\n"]]],[],null,["# Convert VBA macros to Apps Script\n\nBefore you begin\n----------------\n\nYou might want to update incompatible APIs in your VBA code before you convert\nyour files. For files that have the status *Supported exactly* , you can proceed\nto [Step 1: Convert your files](/apps-script/guides/macro-converter/convert-files#step_1_convert_your_files).\n\n### Modify incompatible VBA APIs\n\nFor files that have the status *Supported with workaround* or *Needs\ninvestigation*, you can apply your workarounds and fixes after the conversion\nin Apps Script, but we recommend that you modify the VBA code that you're\nfamiliar with first, before you convert your files.\n\nFor each API marked as *Supported with workaround* or *Needs investigation* in\nthe compatibility report, we recommend the following:\n\n- If the function performed by the API isn't critical to your VBA macro working properly, remove it from your VBA code. If it is, change your code to implement a similar behavior with [supported VBA APIs](/apps-script/guides/macro-converter/compatible-vba-apis).\n- For APIs with the status *Supported with workaround*, if you can't switch to supported VBA APIs, leave your VBA APIs as is. After you convert to Apps Script, a known workaround for this API is recommended in the comments of the converted Apps Script code.\n- If the issue is coming from an [unimplemented language construct](/apps-script/guides/macro-converter/fix-conversion-errors#unimplemented_language_constructs), rewrite your code to avoid using those constructs.\n- For APIs with the status *Needs investigation*, check if your code contains\n any of the following APIs:\n\n - `Adodb.connection`\n - `CreateObject`: This API is often used to connect to database and enterprise resource planning software.\n - `Shell.execute`\n - `OleObject`\n\n If yes, we recommend that you don't convert the files these APIs belong to\n and investigate other options. These APIs tend to perform critical operations\n for a macro, like connecting to a database or accessing a local resource, and\n Apps Script is generally not a good solution.\n\nStep 1: Convert your files\n--------------------------\n\n1. On your computer, open [Google Drive](https://drive.google.com/drive/my-drive).\n2. On the right side panel, click the Macro Converter add-on . If you don't see the side panel, at the bottom right, click Show side panel chevron_left.\n3. Click **Add files and folders**. The Macro Converter only recognizes Excel files.\n4. Choose the files or folders you want to convert and click **Select**. Select fewer than 2,000 files at a time.\n5. To change where your converted files are saved, click Change destination folder edit, and select the folder you want. Otherwise, the files are saved in your MyDrive folder.\n6. Click **Convert**.\n7. When the conversion completes, click **View results**.\n\nStep 2: Test your converted files\n---------------------------------\n\n### Run your Apps Script code\n\nAfter you convert your files, test the Apps Script functions. Test the converted\nfiles with the data you normally use with your Excel files. If possible, compare\nthe output of your converted Sheets files with the output of your original Excel\nfiles.\n\n### Test your triggers\n\nIf your files contain triggers like `onOpen()`, `onEdit()`, or `onClick()`, test\nyour triggers, too. Some VBA triggers won't convert automatically and need to be\naddressed in Apps Script. See [Address common issues](/apps-script/guides/macro-converter/address-conversion-issues).\n\n### Review ReadMe files\n\nIf a ReadMe file was generated with your converted file, review the conversion\nissues listed within the ReadMe file.\n\n- If the issues might be problematic for cases you haven't tested, apply the recommended changes to your code.\n- If you've tested all possible scenarios and everything works as intended, you probably don't need to make changes.\n\nStep 3: Fix errors\n------------------\n\nIf you run into errors while testing your files, see [Fix errors in your\nconverted code](/apps-script/guides/macro-converter/fix-conversion-errors).\n\nIf the code runs without errors, but the result isn't what you expected, open\nthe file's ReadMe file. Review each section to help determine what's causing the\nissue and apply the recommended fix.\n\nAfter you fix errors, test the file again to make sure everything works as\nintended.\n\nRelated articles\n----------------\n\n- [Macro Converter add-on overview](/apps-script/guides/macro-converter/overview)\n- [Determine if VBA macros are compatible](/apps-script/guides/macro-converter/compatibility-report)\n- [Fix errors in your converted code](/apps-script/guides/macro-converter/fix-conversion-errors)\n- [Address common issues](/apps-script/guides/macro-converter/address-conversion-issues)\n- [Watch Macro Converter tutorials](/apps-script/guides/macro-converter/tutorials)\n- [List of compatible VBA APIs](/apps-script/guides/macro-converter/compatible-vba-apis)"]]