แปลงมาโคร VBA เป็น Apps Script
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนเริ่มต้น
คุณอาจต้องอัปเดต API ที่ไม่เข้ากันในโค้ด VBA ก่อนที่จะแปลงไฟล์
สำหรับไฟล์ที่มีสถานะเป็นรองรับทุกประการ คุณสามารถดำเนินการต่อ
ไปยังขั้นตอนที่ 1: แปลงไฟล์
แก้ไข VBA API ที่ไม่เข้ากัน
สำหรับไฟล์ที่มีสถานะรองรับด้วยวิธีแก้ปัญหาชั่วคราวหรือต้องตรวจสอบ คุณสามารถใช้วิธีแก้ปัญหาชั่วคราวและการแก้ไขหลังจากแปลงใน Apps Script ได้ แต่เราขอแนะนำให้แก้ไขโค้ด VBA ที่คุณคุ้นเคยก่อนที่จะแปลงไฟล์
สำหรับ API แต่ละรายการที่ทำเครื่องหมายเป็นรองรับด้วยวิธีแก้ปัญหาหรือต้องตรวจสอบใน
รายงานความเข้ากันได้ เราขอแนะนำให้ทำดังนี้
- หากฟังก์ชันที่ API ดำเนินการไม่จำเป็นต่อการทำงานของมาโคร VBA อย่างถูกต้อง
ให้นำออกจากโค้ด VBA หากเป็นเช่นนั้น ให้เปลี่ยนโค้ดเพื่อ
ใช้ลักษณะการทำงานที่คล้ายกันกับ VBA API ที่รองรับ
- สำหรับ API ที่มีสถานะรองรับด้วยวิธีแก้ปัญหา หากคุณเปลี่ยนไปใช้ API ของ VBA ที่รองรับไม่ได้ ให้ปล่อย API ของ VBA ไว้ตามเดิม หลังจากแปลงเป็น Apps
Script แล้ว ระบบจะแนะนำวิธีแก้ปัญหาที่ทราบสำหรับ API นี้ในความคิดเห็นของโค้ด Apps Script ที่แปลงแล้ว
- หากปัญหาเกิดจากโครงสร้างภาษาที่ไม่ได้ใช้ ให้เขียนโค้ดใหม่เพื่อหลีกเลี่ยงการใช้โครงสร้างเหล่านั้น
สำหรับ API ที่มีสถานะเป็นต้องตรวจสอบ ให้ตรวจสอบว่าโค้ดของคุณมี API ต่อไปนี้หรือไม่
Adodb.connection
CreateObject
: API นี้มักใช้เพื่อเชื่อมต่อกับฐานข้อมูลและซอฟต์แวร์การวางแผนทรัพยากรขององค์กร
Shell.execute
OleObject
หากใช่ เราขอแนะนำว่าอย่าแปลงไฟล์ที่ API เหล่านี้เป็นของ
และลองดูตัวเลือกอื่นๆ API เหล่านี้มักจะดำเนินการที่สำคัญ
สำหรับมาโคร เช่น การเชื่อมต่อกับฐานข้อมูลหรือการเข้าถึงทรัพยากรในเครื่อง และ
โดยทั่วไปแล้ว Apps Script ไม่ใช่โซลูชันที่ดี
ขั้นตอนที่ 1: แปลงไฟล์
- เปิด Google ไดรฟ์ในคอมพิวเตอร์
- คลิกส่วนเสริม Macro Converter
ในแผงด้านข้างทางด้านขวา หากไม่เห็นแผงด้านข้าง ให้คลิกแสดงแผงด้านข้าง chevron_left ที่ด้านขวาล่าง
- คลิกเพิ่มไฟล์และโฟลเดอร์ ตัวแปลงมาโครจะรู้จักเฉพาะไฟล์ Excel
- เลือกไฟล์หรือโฟลเดอร์ที่ต้องการแปลง แล้วคลิกเลือก เลือกไฟล์ครั้งละไม่เกิน 2,000 ไฟล์
- หากต้องการเปลี่ยนตำแหน่งที่บันทึกไฟล์ที่แปลงแล้ว ให้คลิกเปลี่ยนโฟลเดอร์ปลายทาง edit แล้วเลือกโฟลเดอร์ที่ต้องการ มิฉะนั้น ระบบจะบันทึกไฟล์ไว้ในโฟลเดอร์ไดรฟ์ของฉัน
- คลิกแปลง
- เมื่อการแปลงเสร็จสมบูรณ์ ให้คลิกดูผลลัพธ์
ขั้นตอนที่ 2: ทดสอบไฟล์ที่แปลงแล้ว
เรียกใช้โค้ด Apps Script
หลังจากแปลงไฟล์แล้ว ให้ทดสอบฟังก์ชัน Apps Script ทดสอบไฟล์ที่แปลงแล้ว
ด้วยข้อมูลที่คุณใช้กับไฟล์ Excel เป็นประจำ หากเป็นไปได้ ให้เปรียบเทียบเอาต์พุตของไฟล์ชีตที่แปลงแล้วกับเอาต์พุตของไฟล์ Excel ต้นฉบับ
ทดสอบทริกเกอร์
หากไฟล์มีทริกเกอร์ เช่น onOpen()
, onEdit()
หรือ onClick()
ให้ทดสอบ
ทริกเกอร์ด้วย ทริกเกอร์ VBA บางรายการจะไม่แปลงโดยอัตโนมัติและต้อง
แก้ไขใน Apps Script ดูแก้ไขปัญหาที่พบบ่อย
ตรวจสอบไฟล์ ReadMe
หากระบบสร้างไฟล์ ReadMe พร้อมกับไฟล์ที่แปลงแล้ว ให้ตรวจสอบปัญหาการแปลง
ที่ระบุไว้ในไฟล์ ReadMe
- หากปัญหาอาจส่งผลต่อเคสที่คุณยังไม่ได้ทดสอบ ให้ใช้
การเปลี่ยนแปลงที่แนะนําในโค้ด
- หากคุณทดสอบสถานการณ์ที่เป็นไปได้ทั้งหมดแล้วและทุกอย่างทำงานได้ตามที่ต้องการ คุณก็ไม่จำเป็นต้องทำการเปลี่ยนแปลงใดๆ
ขั้นตอนที่ 3: แก้ไขข้อผิดพลาด
หากพบข้อผิดพลาดขณะทดสอบไฟล์ โปรดดูแก้ไขข้อผิดพลาดในโค้ดที่แปลงแล้ว
หากโค้ดทำงานโดยไม่มีข้อผิดพลาด แต่ผลลัพธ์ไม่เป็นไปตามที่คาดไว้ ให้เปิดไฟล์ ReadMe ของไฟล์ ตรวจสอบแต่ละส่วนเพื่อช่วยพิจารณาว่าสาเหตุของปัญหา
คืออะไร และใช้การแก้ไขที่แนะนำ
หลังจากแก้ไขข้อผิดพลาดแล้ว ให้ทดสอบไฟล์อีกครั้งเพื่อให้แน่ใจว่าทุกอย่างทำงานได้ตามที่
ต้องการ
บทความที่เกี่ยวข้อง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 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)"]]