將 VBA 巨集轉換為 Apps Script
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
事前準備
轉換檔案前,您可能需要更新 VBA 程式碼中不相容的 API。如果檔案狀態為「完全支援」,請繼續進行步驟 1:轉換檔案。
修改不相容的 VBA API
如果檔案的狀態為「支援但需使用變通方法」或「需要調查」,您可以在轉換後透過 Apps Script 套用變通方法和修正內容,但建議您先修改熟悉的 VBA 程式碼,再轉換檔案。
如果相容性報告中,有任何 API 標示為「支援但需要解決方法」或「需要調查」,建議採取下列做法:
步驟 1:轉換檔案
- 在電腦上開啟 Google 雲端硬碟。
- 在右側面板中,按一下「巨集轉換器」外掛程式
。如果沒有看到側邊面板,請按一下右下方的「顯示側邊面板」圖示 chevron_left。
- 按一下「新增檔案和資料夾」。巨集轉換器只會辨識 Excel 檔案。
- 選擇要轉換的檔案或資料夾,然後按一下「選取」。一次選取的檔案數量少於 2,000 個。
- 如要變更轉換後檔案的儲存位置,請按一下「變更目的地資料夾」圖示 edit,然後選取所需資料夾。否則檔案會儲存在「我的雲端硬碟」資料夾中。
- 按一下「轉換」。
- 轉換完成後,按一下「查看結果」。
步驟 2:測試轉換後的檔案
執行 Apps Script 程式碼
轉換檔案後,請測試 Apps Script 函式。使用您平常在 Excel 檔案中使用的資料,測試轉換後的檔案。盡可能比較轉換後的 Google 試算表檔案輸出內容與原始 Excel 檔案的輸出內容。
測試觸發條件
如果檔案包含 onOpen()
、onEdit()
或 onClick()
等觸發條件,請一併測試觸發條件。部分 VBA 觸發條件不會自動轉換,必須在 Apps Script 中處理。請參閱「解決常見問題」。
查看 ReadMe 檔案
如果轉換後的檔案產生了 ReadMe 檔案,請查看 ReadMe 檔案中列出的轉換問題。
- 如果問題可能會對您尚未測試的案例造成影響,請將建議的變更套用至程式碼。
- 如果您已測試所有可能的情境,且一切運作正常,則可能不需要進行變更。
步驟 3:修正錯誤
如果在測試檔案時發生錯誤,請參閱「修正轉換後的程式碼中的錯誤」。
如果程式碼執行時沒有錯誤,但結果不符合預期,請開啟檔案的 ReadMe 檔案。請詳閱各節內容,找出問題原因並套用建議的修正方式。
修正錯誤後,請再次測試檔案,確保一切運作正常。
相關文章
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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)"]]