Chuyển code VBA thành Google Script

Sử dụng tiện ích bổ sung Macro Converter để chuyển đổi các tập tin Microsoft Excel dùng macro sang Google Sheet

Tháng 12 17, 2020

Tính năng mới:

Google đã ra mắt Macro Converter cho Google Sheet. Tiện ích bổ sung Google Workspace này sẽ giúp chuyển đổi mã Visual Basic for Applications (VBA) được sử dụng trong macro Microsoft Excel thành mã Apps Script hoạt động trong Google Sheet.

Nhiều tổ chức sử dụng macro trong Sheet và Excel để tự động hóa các quy trình. Khách hàng của Google Workspace Enterprise Plus có thể sử dụng tiện ích bổ sung Macro Conversion để xác định tính tương thích của các tập tin Excel và giúp chuyển đổi nếu có thể. Điều này có thể giúp các nhóm tạo lại macro Excel trong Sheet, giúp tiết kiệm thời gian thông qua tự động hóa đồng thời giảm bớt công việc cần thiết để chuyển đổi tập tin.

Làm thế nào để bắt đầu:

  • Quản trị viên: quản trị viên có thể quản lý cách sử dụng các tiện ích bổ sung trong tổ chức. Truy cập Trung tâm trợ giúp để tìm hiểu cách cho phép hoặc hạn chế tiện ích bổ sung trong trình chỉnh sửa Docs.
  • Người dùng cuối: để bắt đầu, hãy tải xuống tiện ích bổ sung này từ Google Workspace Marketplace. Hãy truy cập Trung tâm trợ giúp để tìm hiểu thêm về cách chuyển đổi tập tin Excel sang tập tin Google Sheet hoặc tìm hiểu thêm trong tài liệu Chuyển đổi Macro Apps Script.

Thời gian triển khai:

  • Tính năng này đã khả dụng đối với tất cả người dùng.

Phiên bản phát hành:

  • Khả dụng đối với khách hàng Google Workspace Enterprise Plus và G Suite Enterprise for Education.
  • Không khả dụng đối với khách hàng Google Workspace Essentials, Business Starter, Business Standard, Business Plus, Enterprise Essentials và Enterprise Standard, cũng như G Suite Basic, Business, Education và Nonprofits.

Nguồn: LVtech




  • Learn Apps Script
    • Guides
    • Reference
    • Samples
    • Support
  • Build add-ons
  • Use the REST API

Stay organized with collections Save and categorize content based on your preferences.

Before you begin

You might want to update incompatible APIs in your VBA code before you convert your files. For files that have the status Supported exactly, you can proceed to Step 1: Convert your files.

Modify incompatible VBA APIs

For files that have the status Supported with workaround or Needs investigation, you can apply your workarounds and fixes after the conversion in Apps Script, but we recommend that you modify the VBA code that you’re familiar with first, before you convert your files.

For each API marked as Supported with workaround or Needs investigation in the compatibility report, we recommend the following:

  • 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.
  • 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.
  • If the issue is coming from an unimplemented language construct, rewrite your code to avoid using those constructs.
  • For APIs with the status Needs investigation, check if your code contains any of the following APIs:

    • Adodb.connection
    • CreateObject: This API is often used to connect to database and enterprise resource planning software.
    • Shell.execute
    • OleObject

    If yes, we recommend that you don’t convert the files these APIs belong to and investigate other options. These APIs tend to perform critical operations for a macro, like connecting to a database or accessing a local resource, and Apps Script is generally not a good solution.

Step 1: Convert your files

  1. On your computer, open Google Drive.
  2. On the right side panel, click the Macro Converter add-on
    Chuyển code VBA thành Google Script
    . If you don’t see the side panel, at the bottom right, click Show side panel .
  3. Click Add files and folders. The Macro Converter only recognizes Excel files.
  4. Choose the files or folders you want to convert and click Select. Select fewer than 2,000 files at a time.
  5. To change where your converted files are saved, click Change destination folder , and select the folder you want. Otherwise, the files are saved in your MyDrive folder.
  6. Click Convert.
  7. When the conversion completes, click View results.

Step 2: Test your converted files

Run your Apps Script code

After you convert your files, test the Apps Script functions. Test the converted files with the data you normally use with your Excel files. If possible, compare the output of your converted Sheets files with the output of your original Excel files.

Test your triggers

If your files contain triggers like onOpen(), onEdit(), or onClick(), test your triggers, too. Some VBA triggers won’t convert automatically and need to be addressed in Apps Script. See Address common issues.

Review ReadMe files

If a ReadMe file was generated with your converted file, review the conversion issues listed within the ReadMe file.

  • If the issues might be problematic for cases you haven’t tested, apply the recommended changes to your code.
  • If you’ve tested all possible scenarios and everything works as intended, you probably don’t need to make changes.

Step 3: Fix errors

If you run into errors while testing your files, see Fix errors in your converted code.

If the code runs without errors, but the result isn’t what you expected, open the file’s ReadMe file. Review each section to help determine what’s causing the issue and apply the recommended fix.

After you fix errors, test the file again to make sure everything works as intended.

  • Macro Converter add-on overview
  • Determine if VBA macros are compatible
  • Fix errors in your converted code
  • Address common issues
  • Watch Macro Converter tutorials
  • List of compatible VBA APIs

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2021-01-29 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]