2

VBA 是编写 excel 宏的语言。Google 表格仅支持将 Google 应用脚本作为宏语言。是否有直接或自动的方式将 VBA 脚本转换为 Google Apps 脚本脚本而无需重写代码?

这是我正在尝试转换的示例 VBA 脚本:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngStart As Range, rngCrit As Range
    Set rngStart = Range("A3")
    Set rngCrit = Range("B1")
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    If Not Intersect(Target, rngCrit) Is Nothing Then
        Range(rngStart, Cells(Rows.Count, rngStart.Column).End(xlUp)).ClearContents
        If Val(rngCrit) > 0 Then
            With rngStart.Resize(Val(rngCrit))
                .Value = "=ROW()-" & rngStart.Row - 1
                .Value = .Value
            End With
        End If
    End If
ExitPoint:
    Set rngStart = Nothing
    Set rngCrit = Nothing
    Application.EnableEvents = True
End Sub

我试过的:

我试图用一些等效的 Google 脚本替换一些命令。我的问题是我的想法太线性了。我正在尝试以与 VBA 完全相同的方式进行编码,并且只是尝试将关键字从 VBA 切换到 Google Script 中的关键字。我希望有一个直接的方法。

4

3 回答 3

1

有一些这样的“转换”示例 - 但它们通常是重新实现,而不是转换。这是一个微妙的区别,但很重要。虽然您可以将一种语言翻译成另一种语言是可行的,但真正的挑战在于 Excel/VBA 与 Google 电子表格/应用程序脚本/javascript 中的底层对象模型和可用方法之间的差异。

成功的最佳机会是使用 VBA Sub 作为一种规范;如果您可以清楚地解释 VBA Sub 实现了哪些功能,那么您就有了在您的 google 电子表格中实现的新功能规范的基础。

于 2013-05-30T20:29:41.640 回答
1

您可以使用GSpread.NET自动将 VBA 转换为 Google 脚本。它库允许使用 Microsoft Excel API 读取和写入 Google 电子表格。示例:http ://scand.com/products/gspread/tutorial.html

于 2016-06-02T11:44:50.417 回答
0

Google officially recommends Macro convertor add on.

Notes:

  • It currently seems to need enterprise plus version of Google workspace.
  • There's a compatibility check and, for VBA APIs, where there are corresponding Apps Script APIs(see list), the conversion is automatic.
  • It still isn't a copy paste job for apis that are not directly available. You still should have some proficiency in excel vba and apps script to attempt conversion, where the compatibility report suggests Supported with workarounds or Needs more investigation
于 2021-10-19T11:01:39.137 回答