Javascript 将VBA转换为Google应用程序脚本

Javascript 将VBA转换为Google应用程序脚本,javascript,vba,excel,google-apps-script,Javascript,Vba,Excel,Google Apps Script,我有下面的VBA代码。我不知道它是如何工作的,但我想把它转换成谷歌表单 我希望有人可以: 向我解释VBA在做什么,这样我就可以,也许,充分地解释它,将其编程为Google Apps脚本 或 演示如何通过Google Sheets实现相同的VBA功能 下面是一个指向使用该函数的示例excel文件的链接: 虽然我不熟悉谷歌应用程序脚本,但我可以在第一部分帮助您 函数的要点似乎是将A列中的名称与作为参数传入的目标名称匹配的所有值相加,第4行中的日期与作为参数的目标日期匹配。(行由name确定

我有下面的VBA代码。我不知道它是如何工作的,但我想把它转换成谷歌表单

我希望有人可以:

  • 向我解释VBA在做什么,这样我就可以,也许,充分地解释它,将其编程为Google Apps脚本

  • 演示如何通过Google Sheets实现相同的VBA功能

下面是一个指向使用该函数的示例excel文件的链接:

虽然我不熟悉谷歌应用程序脚本,但我可以在第一部分帮助您

函数的要点似乎是将A列中的
名称
与作为参数传入的
目标名称
匹配的所有值相加,第4行中的
日期
与作为参数的
目标日期
匹配。(行由
name
确定,列由
date
确定)然后将总值作为
双精度返回

下面是逐行评论

Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range

Dim lastrow As Long

With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
    col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
    If col = 0 Then 'Couldn't find 'targetDate'
        getData = CVErr(xlErrNA) 'Function returns the appropriate error
        Exit Function 'Exit the function after setting the return value
    End If
    Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'

    For Each cell In names 'Looping through every 'cell' in the 'names' range
        If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
            res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
        End If
    Next cell
End With

getData = res 'Return the total
End Function

你认为它是如何工作的?所以,做一个狂热者。试一试,然后带着任何具体问题回来。谢谢J_V,这很有帮助。我应该能够用JS伪代码推理出类似的东西,并像@Jean-François Corbett建议的那样尝试一下。非常感谢大家。我会在检查完一些东西后再跟进。很高兴你发现它很有用!
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range

Dim lastrow As Long

With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
    col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
    If col = 0 Then 'Couldn't find 'targetDate'
        getData = CVErr(xlErrNA) 'Function returns the appropriate error
        Exit Function 'Exit the function after setting the return value
    End If
    Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'

    For Each cell In names 'Looping through every 'cell' in the 'names' range
        If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
            res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
        End If
    Next cell
End With

getData = res 'Return the total
End Function