顯示具有 VBA 標籤的文章。 顯示所有文章
顯示具有 VBA 標籤的文章。 顯示所有文章

2026年10月20日 星期二

[研究]GAS(Google Apps Script)可以自動轉成為(Visual Basic for Applications)嗎?

[研究]GAS(Google Apps Script)可以自動轉成為(Visual Basic for Applications)嗎?

2026-10-20

可以協助將 GAS(Google Apps Script)轉換成 VBA(Visual Basic for Applications),但目前不能保證一鍵自動、完整且正確地轉換所有程式碼。

主要原因:GAS 和 VBA 使用不同的程式語言、執行環境及 API。 因此,通常需要將程式邏輯轉換,再重新設計與測試相關功能。

一、GAS 與 VBA 轉換比較

項目

自動轉換的可行性

說明

變數、條件判斷、迴圈

高

語法需要調整

加總、排序、資料篩選

高

可依需求改寫

讀取、寫入儲存格

中高

GAS 與 Excel VBA 的 API 不同

跨工作表處理

高

需要調整物件及工作表參照

跨檔案讀寫

中

取決於檔案位置及存取方式

Google 試算表、Gmail、Drive 整合

低至中

通常需要改用 Microsoft 服務或其他 API

網頁應用程式、觸發器

低

通常需要重新設計

以上是一般情況的估計,實際難度取決於程式內容。

二、實際範例:GAS 自動改寫為 VBA

假設您有一段 GAS,將 A1 與 A2 相加,結果寫入 A3。

原始 GAS 程式碼

function addCells() {
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getActiveSheet();

  const a1 = sheet.getRange("A1").getValue();
  const a2 = sheet.getRange("A2").getValue();

  sheet.getRange("A3").setValue(a1 + a2);
}

轉換後的 Excel VBA 程式碼

Sub AddCells()

    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim a1 As Double
    Dim a2 As Double

    a1 = ws.Range("A1").Value
    a2 = ws.Range("A2").Value

    ws.Range("A3").Value = a1 + a2

End Sub

兩段程式的功能相同,但執行環境不同:

  • GAS:在 Google 試算表中執行。

  • VBA:在桌面版 Microsoft Excel 中執行。

(完)

2026年10月19日 星期一

[研究]有 VBA 的 .xlsx 上傳 成為 Google 試算表後,會變成 GAS ? 還可以跑 ?

[研究]有 VBA 的 .xlsx 上傳 成為 Google 試算表後,會變成 GAS ? 還可以跑 ? 

2026-10-19

可以,但要注意一個關鍵觀念:Excel 的 VBA 不會因為把 .xlsx 上傳到 Google 雲端硬碟、再轉換成 Google 試算表,就自動變成 GAS(Google Apps Script)。

Excel VBA 與 Google Apps Script 是兩種不同的程式語言與執行環境。不過,您可以將 VBA 的功能改寫成 GAS,讓它在 Google 試算表中繼續執行。

一、Excel VBA 上傳 Google 試算表後會怎樣?

項目

Excel .xlsx / .xlsm

Google 試算表

主要程式語言

VBA

Google Apps Script(JavaScript)

原有 VBA 程式碼

.xlsm 可保存 VBA;.xlsx 不支援保存 VBA 專案

不會自動轉換成 GAS

一般儲存格公式

大部分可保留,部分函數可能有差異

轉換後需檢查相容性

工作表、格式、資料

通常可以匯入

可能需要檢查格式與功能差異

按鈕與巨集

可能無法正常運作

通常需要重新建立操作介面及 GAS

自動執行 VBA

只能在支援 VBA 的 Excel 環境執行

不能直接執行 VBA

改寫後的 GAS

不適用

可以在 Google 試算表中執行

特別注意:如果您的原始檔是 .xlsx,它本身通常就沒有儲存 VBA 專案;如果實際上有 VBA 巨集,原始檔通常是 .xlsm。

二、VBA 與 GAS 的轉換概念

例如,您在 Excel VBA 裡寫了以下程式:

Sub HelloWorld()
    Range("A1").Value = "Hello World"
End Sub

這段程式會將 Excel 工作表的 A1 儲存格填入 Hello World。

改寫成 GAS 後:

function helloWorld() {
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getActiveSheet();

  sheet.getRange("A1").setValue("Hello World");
}

功能相同,但程式碼並不相同。

(完)

[研究]VBA測試

[研究]VBA測試

2026-10-19

 假設您已經有 Test.xlsx。

  1. 使用 Excel 開啟 Test.xlsx。

  2. 按 Alt + F11,開啟 VBA 編輯器。

  3. 選擇「插入」→「模組」。
  4. 輸入以下測試程式碼:
Sub HelloWorld()
    Range("A1").Value = "Hello World"
    Range("A2").Value = Now
End Sub
  1. 回到 Excel,按 F12(另存新檔)。

  2. 將檔案類型選為「Excel 啟用巨集的活頁簿(*.xlsm)」。
  3. 儲存後,按 Alt + F8,選擇 HelloWorld,再按「執行」。

預期結果:

  • A1:顯示 Hello World

  • A2:顯示目前日期與時間














(完)

2026年10月8日 星期四

[研究]比較 Google Apps Script 和 Office Excel VBA 和 M365 Excel VBA、Office Scripts

[研究]比較 Google Apps Script 和 Office  Excel VBA 和 M365 Excel VBA、Office Scripts

2026-10-08

項目 Google Apps Script Office Excel VBA Microsoft 365 Excel VBA
開發語言 JavaScript 類 VBA VBA
主要平台 Google Workspace 雲端 Office 桌面版 Microsoft 365 桌面版
Excel ❌ ✅ ✅
Google Sheets ✅ ❌ ❌
Word ❌ ✅ ✅
PowerPoint ❌ ✅ ✅
Outlook ❌ ✅ ✅
Gmail ✅ ❌ ❌
Google Drive ✅ ❌ ❌
OneDrive 可透過 API 可 可,整合較佳
SharePoint API/服務 可 整合較佳
雲端執行 ⭐⭐⭐⭐⭐ ⭐ ⭐⭐~⭐⭐⭐
本機檔案操作 ⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Windows 系統操作 ⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
網頁 API ⭐⭐⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐
排程執行 ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐
多人協作 ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐⭐⭐
伺服器部署 不需要 通常需要電腦 通常需要電腦
無人值守 ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐
學習門檻 中 中 中
Excel 細部控制 ⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Google 生態系 ⭐⭐⭐⭐⭐ ⭐ ⭐
Microsoft 生態系 ⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
適合大量 Excel 自動化 ⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
適合跨平台 ⭐⭐⭐⭐⭐ ⭐ ⭐⭐
是否需要安裝 Excel ❌ ✅ ✅


比較 **GAS + VBA + Office Scripts**

Office Scripts 使用 TypeScript/JavaScript 類型語法,主要用於 Excel 網頁版與 Microsoft 365 雲端自動化。

項目 GAS VBA Office Scripts
全名 Google Apps Script Visual Basic for Applications Office Scripts
主要平台 Google Workspace Windows / Mac Office Microsoft 365
主要 Excel Google Sheets Excel 2021/2024/M365 Excel for Web / M365
執行位置 Google 雲端 本機 Excel 雲端
程式語言 JavaScript 類型 Visual Basic TypeScript
是否需安裝 ❌ ✅ Excel ❌
可離線執行 ❌ ✅ ❌
操作本機檔案 較有限 ★★★★★ ★
操作 Excel 工作表 ★★★★★ ★★★★★ ★★★★★
操作 Google Sheets ★★★★★ ❌ ❌
操作 Word ★★★★ ★★★★★ ★★★
操作 Outlook ★★★★ ★★★★★ ★★★
操作 PowerPoint ★★★ ★★★★★ ★★★
操作 Gmail ★★★★★ ❌ ❌
操作 OneDrive ★★★★ ★★★★ ★★★★★
操作 SharePoint ★★★★ ★★★ ★★★★★
HTTP / REST API ★★★★★ ★★★ ★★★★★
Webhook ★★★★★ ★★ ★★★★
排程執行 ★★★★★ ★★★ ★★★★★+Power Automate
事件觸發 ★★★★★ ★★★★★ ★★★
GUI 操作 ★★ ★★★★★ ★
Excel UserForm ❌ ★★★★★ ❌
巨集按鈕 ★★★ ★★★★★ ★★★
Power Automate 整合 ★★ ★★★ ★★★★★
Google Workspace 整合 ★★★★★ ★ ❌
Microsoft 365 整合 ★★★ ★★★★★ ★★★★★
適合企業流程 ★★★★ ★★★★ ★★★★★
適合個人 Excel ★★★ ★★★★★ ★★★★
學習門檻 ★★★ ★★★ ★★★★
雲端多人協作 ★★★★★ ★★ ★★★★★

比較 Google Apps Script 和 Office  Excel VBA 和 M365 Excel VBA、Office Scripts

項目 Google Apps Script Office 2021/2024 Excel VBA M365 Excel VBA Office Scripts
廠商 Google Microsoft Microsoft Microsoft
主要平台 Google Workspace Windows Office Microsoft 365 Microsoft 365
主要對象 Google Sheets 桌面 Excel 桌面 Excel Excel Web / M365
程式語言 JavaScript 類 VBA VBA TypeScript
是否免費 是 隨 Office 隨 M365 隨符合資格的 M365
是否需要安裝 不需要 需要 Excel 需要 Excel 不需要傳統 VBA 環境
Windows ✓ ✓ ✓ ✓
macOS ✓ ✓* ✓ ✓
瀏覽器 ✓ ✗ 部分功能 ✓
手機/平板 間接 ✗ 部分 ✓/有限
雲端執行 ✓ ✗ 部分 ✓
本機檔案操作 較弱 非常強 非常強 弱
Excel COM/Windows API ✗ ✓ ✓ ✗
操作 Excel Google Sheets 非常強 非常強 強
操作 Word/Outlook 透過 Workspace API ✓ ✓ ✗/有限
操作 Google Drive ✓ API/其他方式 API/其他方式 ✗
操作 OneDrive/SharePoint API 可 ✓ ✓
排程 ✓ Windows 工作排程器等 Power Automate 等 ✓
觸發器 非常強 VBA Event VBA Event 強
多人共同使用 非常適合 較弱 適合 非常適合
Web API 非常方便 可,但較麻煩 可 ✓
Power Automate ✗ 可透過其他方式 ✓ ✓
Git/版本控制 可 較不方便 較不方便 較適合
跨平台 非常好 較差 中等 非常好
學習難度 中 中~高 中~高 中
最適合 Google 生態系 本機 Excel 自動化 M365 企業自動化 M365 雲端 Excel 自動化

(完)

相關

Apps Script | Google for Developers
https://developers.google.com/apps-script

Google Apps Script 總覽
https://developers.google.com/apps-script/overview?hl=zh-tw