Chuyển đổi macro VBA thành Apps Script
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trước khi bắt đầu
Bạn nên cập nhật các API không tương thích trong mã VBA trước khi chuyển đổi tệp. Đối với những tệp có trạng thái Được hỗ trợ chính xác, bạn có thể chuyển sang Bước 1: Chuyển đổi tệp.
Sửa đổi các API VBA không tương thích
Đối với những tệp có trạng thái Được hỗ trợ bằng giải pháp thay thế hoặc Cần điều tra, bạn có thể áp dụng các giải pháp thay thế và bản sửa lỗi sau khi chuyển đổi trong Apps Script, nhưng trước tiên, bạn nên sửa đổi mã VBA mà bạn quen thuộc trước khi chuyển đổi tệp.
Đối với mỗi API được đánh dấu là Được hỗ trợ bằng giải pháp tạm thời hoặc Cần điều tra trong báo cáo tương thích, bạn nên làm như sau:
- Nếu chức năng do API thực hiện không quan trọng đối với hoạt động đúng cách của macro VBA, hãy xoá chức năng đó khỏi mã VBA. Nếu có, hãy thay đổi mã để triển khai hành vi tương tự bằng các API VBA được hỗ trợ.
- Đối với các API có trạng thái Được hỗ trợ bằng giải pháp tạm thời, nếu không thể chuyển sang các API VBA được hỗ trợ, hãy giữ nguyên các API VBA. Sau khi bạn chuyển đổi sang Apps Script, một giải pháp tạm thời đã biết cho API này sẽ được đề xuất trong phần nhận xét của mã Apps Script đã chuyển đổi.
- Nếu vấn đề xuất phát từ một cấu trúc ngôn ngữ chưa được triển khai, hãy viết lại mã để tránh sử dụng các cấu trúc đó.
Đối với các API có trạng thái Cần điều tra, hãy kiểm tra xem mã của bạn có chứa bất kỳ API nào sau đây hay không:
Adodb.connection
CreateObject
: API này thường được dùng để kết nối với cơ sở dữ liệu và phần mềm lập kế hoạch nguồn lực doanh nghiệp.
Shell.execute
OleObject
Nếu có, bạn không nên chuyển đổi các tệp mà những API này thuộc về và hãy tìm hiểu các lựa chọn khác. Các API này thường thực hiện các thao tác quan trọng cho một macro, chẳng hạn như kết nối với cơ sở dữ liệu hoặc truy cập vào một tài nguyên cục bộ và Apps Script thường không phải là một giải pháp phù hợp.
Bước 1: Chuyển đổi tệp
- Trên máy tính, hãy mở Google Drive.
- Trên bảng điều khiển bên phải, hãy nhấp vào tiện ích bổ sung Macro Converter
. Nếu không thấy bảng điều khiển bên, hãy nhấp vào biểu tượng Hiện bảng điều khiển bên chevron_left ở dưới cùng bên phải.
- Nhấp vào Thêm tệp và thư mục. Trình chuyển đổi macro chỉ nhận dạng tệp Excel.
- Chọn tệp hoặc thư mục bạn muốn chuyển đổi rồi nhấp vào Chọn. Chọn ít hơn 2.000 tệp cùng một lúc.
- Để thay đổi vị trí lưu tệp đã chuyển đổi, hãy nhấp vào biểu tượng Thay đổi thư mục đích edit rồi chọn thư mục bạn muốn. Nếu không, các tệp sẽ được lưu trong thư mục Drive của tôi.
- Nhấp vào Chuyển đổi.
- Khi quá trình chuyển đổi hoàn tất, hãy nhấp vào Xem kết quả.
Bước 2: Kiểm thử các tệp đã chuyển đổi
Chạy mã Apps Script
Sau khi chuyển đổi tệp, hãy kiểm thử các hàm Apps Script. Kiểm thử các tệp đã chuyển đổi bằng dữ liệu mà bạn thường dùng với các tệp Excel. Nếu có thể, hãy so sánh đầu ra của các tệp Trang tính đã chuyển đổi với đầu ra của các tệp Excel gốc.
Kiểm thử điều kiện kích hoạt
Nếu tệp của bạn chứa các điều kiện kích hoạt như onOpen()
, onEdit()
hoặc onClick()
, hãy kiểm thử cả các điều kiện kích hoạt đó. Một số trình kích hoạt VBA sẽ không tự động chuyển đổi và cần được xử lý trong Apps Script. Xem phần Giải quyết các vấn đề thường gặp.
Xem xét tệp ReadMe
Nếu tệp ReadMe được tạo cùng với tệp đã chuyển đổi, hãy xem xét các vấn đề về việc chuyển đổi được liệt kê trong tệp ReadMe.
- Nếu các vấn đề có thể gây ra vấn đề cho những trường hợp mà bạn chưa kiểm thử, hãy áp dụng các thay đổi được đề xuất cho mã của bạn.
- Nếu đã kiểm thử tất cả các trường hợp có thể xảy ra và mọi thứ đều hoạt động như dự kiến, thì có thể bạn không cần thay đổi gì cả.
Bước 3: Khắc phục lỗi
Nếu bạn gặp lỗi trong khi kiểm thử tệp, hãy xem phần Khắc phục lỗi trong mã đã chuyển đổi.
Nếu mã chạy mà không gặp lỗi nhưng kết quả không như bạn mong đợi, hãy mở tệp ReadMe của tệp đó. Hãy xem xét từng phần để xác định nguyên nhân gây ra vấn đề và áp dụng biện pháp khắc phục được đề xuất.
Sau khi bạn sửa lỗi, hãy kiểm tra lại tệp để đảm bảo mọi thứ hoạt động như dự kiến.
Bài viết liên quan
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\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)"]]