如何将 VBA 转换为 Google Apps 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25877139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:56:25  来源:igfitidea点击:

How to convert VBA to Google Apps Script?

vbagoogle-apps-scriptgoogle-sheets

提问by Tom Ruiz

My company is switching from Microsoft Office to Google Docs, I have this (several) code I want to use in Google Spreadsheets but I have no idea on how to do that.

我的公司正在从 Microsoft Office 切换到 Google Docs,我有这个(几个)代码我想在 Google 电子表格中使用,但我不知道如何去做。

Consider this sample of code:

考虑这个代码示例:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("E:J")) Is Nothing Then
        Cells(Target.Row, 1) = MonthName(Month(Date))
        Cells(Target.Row, 2) = Format(Now, "mm/dd/yyyy")
    End If
End Sub

回答by SoftwareTester

If you want to convert from VBA tot GAS there is one site you really must look http://ramblings.mcpher.com/

如果您想从 VBA 转换为 GAS,您必须查看一个站点http://ramblings.mcpher.com/

If that site doesn't help, I guess you're in big trouble!

如果该网站没有帮助,我想您遇到了大麻烦!

回答by miro

Use GSpread.NET. to work with Google Spreadsheets by using Microsoft Excel API. You needn't rewrite all code, just replace CreateObject(Excel.Application) to CreateObject(GSpreadCOM.Application).

使用 GSpread.NET。使用 Microsoft Excel API 处理 Google 电子表格。您无需重写所有代码,只需将 CreateObject(Excel.Application) 替换为 CreateObject(GSpreadCOM.Application)。

examples

例子

回答by Alan Wells

The function is structured like this:

该函数的结构如下:

Private Sub Worksheet_Change(ByVal Target As Range)
  Statements here
End Sub

That would probably convert to something like:

那可能会转换为类似的东西:

function Worksheet_Change(ByVal) {
  Statements Here;
};

Instead of End Subthe function in JavaScript ends with a curly brace }.

而不是End SubJavaScript中的函数以花括号结尾}

Instead of End If, the If, then statement also ends with a curly brace }.

End IfIf, then 语句也以花括号结尾,而不是}

if (condition) {code here;}

That's the syntax for a JavaScript Ifstatement. There is no Thenkey word in JavaScript.

这是 JavaScript If语句的语法。ThenJavaScript 中没有关键字。

回答by Anees Hameed

This is currently impossible in google app script as there is neither worksheet selection change eventnor selection change eventin spreadsheet services. List of Google Spreadsheet events are Here.

这在 google 应用程序脚本中目前是不可能的,因为电子表格服务中既没有工作表选择更改事件,也没有选择更改事件。Google 电子表格事件列表在这里

But we have time driven triggersin GAS, through this you will be able to run a function at some particular time. In your case lets say morning 12AM. You have to write a function which will run at 12AM to go through all the sheets and then format Cells(Target.Row, 1) and Cells(Target.Row, 2) on to the format which you like.

但是我们在 GAS 中有时间驱动的触发器,通过它你将能够在某个特定时间运行一个函数。在您的情况下,可以说早上 12 点。您必须编写一个函数,该函数将在上午 12 点运行以遍历所有工作表,然后将 Cells(Target.Row, 1) 和 Cells(Target.Row, 2) 格式化为您喜欢的格式。

Definitely you have to restructure your current spreadsheet... Welcome to Google App Scripting.

当然,您必须重新构建您当前的电子表格... 欢迎使用 Google App Scripting。