将VBA转换为Google Apps脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将VBA转换为Google Apps脚本相关的知识,希望对你有一定的参考价值。
我有下面的VBA代码。我不知道它是如何工作的,但我想将其转换为Google表格。
我希望有人可以:
- 向我解释一下VBA正在做什么,以便我可以推断它足以将其编程为Google Apps脚本,
要么
- 告诉我如何通过Google表格实现相同的VBA功能。
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet)
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
col = Application.Match(targetDate, .Range("4:4"), 0)
If col = 0 Then
getData = CVErr(xlErrNA)
Exit Function
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A"))
For Each cell In names
If cell = targetName Then
res = res + cell.Offset(, col - 1)
End If
Next cell
End With
getData = res
End Function
这是一个示例excel文件的链接,其中使用了该函数:https://www.dropbox.com/s/h5vcjv9tlh1vvg7/Resources%20and%20Projects%20Full%20Example.xlsm
虽然我不熟悉Google Apps脚本,但我可以帮助您完成第一部分。
该函数的点似乎是将所有在a列中找到的name
与作为参数传入的targetName
匹配的值相加,并且在第4行中找到的date
与targetDate
匹配,name
也是参数。 (行由date
确定,列由double
确定。)然后将总值作为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
返回。
这是一行一行的评论。
qazxswpoi
以上是关于将VBA转换为Google Apps脚本的主要内容,如果未能解决你的问题,请参考以下文章
如何将 JSON 文件转换为使用 Google Apps 脚本分隔的新行?
使用 Google AppS 脚本将 .csv 文件转换为 .xls