Variant Array Custom Function Google Sheets? VBA For Example

99封情书 提交于 2021-01-29 10:30:26

问题


The following function below will add "1" to every column across the excel sheet. If I put =vbe(12) in A1, it will put "1" in columns "A1:L1". How can I translate this VBA to JavaScript for Google Sheets?

Function vbe(Num As Long) As Variant
   Dim ary As Variant
   Dim i As Long
   ReDim ary(Num - 1)
   For i = 0 To Num - 1
      ary(i) = 1
   Next i
   vbe = ary
End Function


回答1:


You can write a custom formula that creates an array of "1"s with the length as a specified parameter, e.g.

function myFunction(numberColumns) {
  var ones=[];
  ones[0]=[];
  for(var i=0;i<numberColumns;i++){
    ones[0].push(1);
  }
  return ones;
}

Now, you just need to call the function from a cell, e.g. by typing

=myFunction(12)

Useful information can be found in the documentation about Custom Functions in Google Sheets and Google Apps Script.



来源:https://stackoverflow.com/questions/59580142/variant-array-custom-function-google-sheets-vba-for-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!