Google Sheets的变体数组自定义函数?例如VBA。

huangapple go评论115阅读模式
英文:

Variant Array Custom Function Google Sheets? VBA For Example

问题

以下是将VBA代码翻译成JavaScript代码的部分:

  1. function vbe(Num) {
  2. var ary = [];
  3. for (var i = 0; i < Num; i++) {
  4. ary.push(1);
  5. }
  6. return ary;
  7. }

希望这对您有所帮助。如果您需要进一步的翻译,请告诉我。

英文:

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?

  1. Function vbe(Num As Long) As Variant
  2. Dim ary As Variant
  3. Dim i As Long
  4. ReDim ary(Num - 1)
  5. For i = 0 To Num - 1
  6. ary(i) = 1
  7. Next i
  8. vbe = ary
  9. End Function

Google Sheets的变体数组自定义函数?例如VBA。

答案1

得分: 1

你可以编写一个自定义公式,根据指定的参数创建一个包含 "1" 的数组,例如:

  1. function myFunction(numberColumns) {
  2. var ones=[];
  3. ones[0]=[];
  4. for(var i=0;i<numberColumns;i++){
  5. ones[0].push(1);
  6. }
  7. return ones;
  8. }

现在,你只需要在单元格中调用该函数,例如输入:

=myFunction(12)

有关在Google Sheets中使用自定义函数的有用信息可以在Google Sheets自定义函数文档Google Apps脚本文档中找到。

英文:

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

  1. function myFunction(numberColumns) {
  2. var ones=[];
  3. ones[0]=[];
  4. for(var i=0;i&lt;numberColumns;i++){
  5. ones[0].push(1);
  6. }
  7. return ones;
  8. }

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.

huangapple
  • 本文由 发表于 2020年1月3日 22:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定