英文:
Variant Array Custom Function Google Sheets? VBA For Example
问题
以下是将VBA代码翻译成JavaScript代码的部分:
function vbe(Num) {
var ary = [];
for (var i = 0; i < Num; i++) {
ary.push(1);
}
return ary;
}
希望这对您有所帮助。如果您需要进一步的翻译,请告诉我。
英文:
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
得分: 1
你可以编写一个自定义公式,根据指定的参数创建一个包含 "1" 的数组,例如:
function myFunction(numberColumns) {
var ones=[];
ones[0]=[];
for(var i=0;i<numberColumns;i++){
ones[0].push(1);
}
return ones;
}
现在,你只需要在单元格中调用该函数,例如输入:
=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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论