Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从自定义GoogleSheets函数返回2D数组时,从类检索的值保留为空_Javascript_Google Sheets_Scripting - Fatal编程技术网

Javascript 从自定义GoogleSheets函数返回2D数组时,从类检索的值保留为空

Javascript 从自定义GoogleSheets函数返回2D数组时,从类检索的值保留为空,javascript,google-sheets,scripting,Javascript,Google Sheets,Scripting,如果标题有点混乱,我很抱歉。基本上我有一个自定义的GoogleSheets函数,它接收一个单元格范围作为参数。我有一个名为Employee的类,其构造函数将ID作为参数,并将值保存在变量中。在自定义函数中,我创建了一个名为Employees的数组,并为范围中的每个值添加一个Employee类的新实例,该实例的值作为构造函数参数。然后创建一个名为IDs的空数组,循环遍历Employees数组中的每个元素,并将保存在类中的ID值添加到新数组中。如果我返回IDs数组,那么当我在GoogleSheets

如果标题有点混乱,我很抱歉。基本上我有一个自定义的GoogleSheets函数,它接收一个单元格范围作为参数。我有一个名为Employee的类,其构造函数将ID作为参数,并将值保存在变量中。在自定义函数中,我创建了一个名为Employees的数组,并为范围中的每个值添加一个Employee类的新实例,该实例的值作为构造函数参数。然后创建一个名为IDs的空数组,循环遍历Employees数组中的每个元素,并将保存在类中的ID值添加到新数组中。如果我返回IDs数组,那么当我在GoogleSheets上调用该函数时,项目列表将垂直显示。但是,如果我改为创建一个2D数组,并将IDs变量添加为其中一个元素,同时创建一个以固定元素作为第二个值的新数组。如果我返回新的2D数组,当我调用函数时,Ids数组应该水平打印在相邻单元格上,第二个数组的值应该打印在下面。但是,不会打印ID,只打印第二个数组的值。如果在遍历Employees数组并将其ID添加到Ids数组后,我添加了一个常量值,那么在调用函数时只打印该值。只有当我返回2D数组时才会发生这种情况,如果我只返回ID,它们都会被打印出来

在下面的函数中,当在Google Sheets上调用chart函数时,工作表上的ids数组将留空

class Employee{
  constructor(employee){
    this.employeeID = employee;
  }
}
function chart(employeeIDs){
  employees = [];
  for(x = 0; x < employeeIDs.length; x++)
    employees.push(new Employee(employeeIDs[x]));
  ids = [];  
  for(x = 0; x < employees.length; x++)
    ids.push(employees[x].employeeID);
  vehicles = ["Car", "road", "bus", "Plane", "Submarine"];
  return [ids, vehicles];
}
然后,当调用该函数时,将打印ID中的项目列表。 如果在添加Employee类中的值后向ids数组添加其他值,例如

ids = [];  
for(x = 0; x < employees.length; x++)
  ids.push(employees[x].employeeID);
ids.push(12)
vehicles = ["Car", "road", "bus", "Plane", "Submarine"];
return [ids, vehicles];
'''
Then only the number 12 is printed at the end, plus the entire contents of the vehicles array.
ids=[];
对于(x=0;x
我想问题的原因可能是每行的每列长度不同。但是为了正确地复制您的情况,您能否提供您正在使用的示例公式?顺便问一下,如果我将返回改为…与您的顶部和底部脚本之间的关系,我可以问您吗?我感谢您的帮助,我实际上设法缩小了问题范围,并以更容易理解的方式重新提交了问题。谢谢你的回复。我检查了一下,然后贴了一个答案。你能确认一下吗。如果这不是你期望的方向,我道歉。顺便说一句,在自定义函数中,当数组是二维数组时,每个列的长度似乎不需要像
const sample=()=>[[1,2,3],[1],[2,3],[1,2]。所以我的第一个评论是我的错误。对此我深表歉意。你将如何回答这个问题?细胞范围看起来如何@鲁本伍德
ids = [];  
for(x = 0; x < employees.length; x++)
  ids.push(employees[x].employeeID);
ids.push(12)
vehicles = ["Car", "road", "bus", "Plane", "Submarine"];
return [ids, vehicles];
'''
Then only the number 12 is printed at the end, plus the entire contents of the vehicles array.