Google apps script &引用;“引用不存在”;执行自定义函数时出错

Google apps script &引用;“引用不存在”;执行自定义函数时出错,google-apps-script,google-sheets,referenceerror,google-sheets-custom-function,Google Apps Script,Google Sheets,Referenceerror,Google Sheets Custom Function,我正在谷歌应用程序脚本中编写一个函数,当我调用我的函数时,我需要解决的最后一个错误似乎是谷歌表单中的“引用不存在”错误。我不知道该怎么办,因为我的代码似乎没有问题 这就是我的代码现在的样子。它不完整,因为我需要为用户输入更改它,但这是一个测试 在google sheets单元格中,我键入=sortingtesting() 函数排序测试() { 变量pInfo1=['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'o'、'p'、'q'、'r'

我正在谷歌应用程序脚本中编写一个函数,当我调用我的函数时,我需要解决的最后一个错误似乎是谷歌表单中的“引用不存在”错误。我不知道该怎么办,因为我的代码似乎没有问题

这就是我的代码现在的样子。它不完整,因为我需要为用户输入更改它,但这是一个测试

在google sheets单元格中,我键入
=sortingtesting()

函数排序测试()
{
变量pInfo1=['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'o'、'p'、'q'、'r'、's']
变量pInfo2=['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'o'、'p'、'q'、'r'、's']
变量pInfo3=['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'o'、'p'、'q'、'r'、's']
变量pWO=['1','','2','','3','4','5','6','7','','','8','','','','9','10']
var pSearch=['c','b','a']
var WO=[];
var Info1=[];
var Info2=[];
var Info3=[];
var搜索=[];
对于(变量i=0;i<18;i++)
WO[i]=pWO[i];
对于(变量i=0;i<18;i++)
{
Info1[i]=pInfo1[i];
}
对于(变量i=0;i<18;i++)
{
Info2[i]=pInfo2[i];
}
对于(变量i=0;i<18;i++)
{
Info3[i]=pInfo3[i];
}
对于(变量i=0;i<1;i++)
Search[i]=pSearch[i];
//声明辅助存储阵列及其计数器
var FinalArray1=[];
var FinalArray2=[];
var FinalArray3=[];
var LastArray=[];
var a=0;
var b=0;
var c=0;
var d=0;
//循环运行并使工单行中的所有单元格与工单编号相关
对于(变量行=0;行
找到的解决方案: 这是我的工作代码,使用来自google sheets的数组作为参数,我只是觉得将工作原型放在那里会很好:

function sortingtesting(WO, Info, Search) 
{ 
  // Declares secondary storage arrays and their counters
  var FinalArray1 = [];
  var FinalArray2 = [];
  var FinalArray3 = [];
  var LastArray = [];
  var a = 0;
  var b = 0;
  var c = 0;
  var d = 0;
  
  // loop to run and make all of the cells in the work order row relevant to the work order number instead of being blank
  for(var row = 0; row < WO.length; row ++)
  {
    var counter = row - 1;
    while(WO[row] == "")
    {
      WO[row] = WO[counter];
      counter--;
    }
  }
  
  // loop that goes through saving which work orders meet certain search criteria, each search criteria has its own separate secondary array to store the work orders that meet the criteria
  for(var col = 0; col < Info[0].length; col++)
  {
    for(var row = 0; row < Info.length; row++)
    {
      if(Info[row][col] == Search[0])
      {   
        FinalArray1[a] = WO[row];
        a++;
      }
      else if(Info[row][col] == Search[1])
      {
        FinalArray2[b] = WO[row];
        b++;
      }
      else if(Info[row][col] == Search[2])
      {
        FinalArray3[c] = WO[row];
        c++;
      }
    }
  }
  
  LastArray[0] = 'N/A';
  
  // loop to run through and get all the work orders that meet all of the criteria
  for(var i = 0; i < FinalArray1.length; i++)
  {
    for(var j = 0; j < FinalArray2.length; j++)
    {
      for(var k = 0; k < FinalArray3.length; k++)
      {
        if(FinalArray3[k] == FinalArray2[j] && FinalArray2[j] == FinalArray1[i])
        {
          LastArray[d] = FinalArray1[i];
          d++;
        }
      }
    }
  }
  
  return LastArray;
}
功能分类测试(WO、信息、搜索)
{ 
//声明辅助存储阵列及其计数器
var FinalArray1=[];
var FinalArray2=[];
var FinalArray3=[];
var LastArray=[];
var a=0;
var b=0;
var c=0;
var d=0;
//循环运行并使工单行中的所有单元格与工单编号相关,而不是为空
对于(变量行=0;行
TL;DR函数不应返回空数组

通过放置
返回“有效字符串”
在脚本的不同位置(将代码平分),您将看到返回LastArray;`正在导致错误

通过在调试器中运行代码,LastArray是一个空数组

根据实验,空数组不是公式中调用的函数的有效返回值,包含多个值的数组也不是。包含一个整数的数组是有效的


更改
var LastArray=[]
var LastArray=[1]演示了这一点。

你能设置一个断点并确定实际失败的行吗?当它设置成这样时,它没有失败的行,但是当google sheets调用函数时,它会说有一个引用错误,这非常有效。我仍然使用“return LastArray”,但我将LastArray[0]设置为N/A,这样,如果没有任何匹配项来填充数组,它将保持N/A而不是空数组。我将发布当前正在工作的代码作为编辑。
function sortingtesting(WO, Info, Search) 
{ 
  // Declares secondary storage arrays and their counters
  var FinalArray1 = [];
  var FinalArray2 = [];
  var FinalArray3 = [];
  var LastArray = [];
  var a = 0;
  var b = 0;
  var c = 0;
  var d = 0;
  
  // loop to run and make all of the cells in the work order row relevant to the work order number instead of being blank
  for(var row = 0; row < WO.length; row ++)
  {
    var counter = row - 1;
    while(WO[row] == "")
    {
      WO[row] = WO[counter];
      counter--;
    }
  }
  
  // loop that goes through saving which work orders meet certain search criteria, each search criteria has its own separate secondary array to store the work orders that meet the criteria
  for(var col = 0; col < Info[0].length; col++)
  {
    for(var row = 0; row < Info.length; row++)
    {
      if(Info[row][col] == Search[0])
      {   
        FinalArray1[a] = WO[row];
        a++;
      }
      else if(Info[row][col] == Search[1])
      {
        FinalArray2[b] = WO[row];
        b++;
      }
      else if(Info[row][col] == Search[2])
      {
        FinalArray3[c] = WO[row];
        c++;
      }
    }
  }
  
  LastArray[0] = 'N/A';
  
  // loop to run through and get all the work orders that meet all of the criteria
  for(var i = 0; i < FinalArray1.length; i++)
  {
    for(var j = 0; j < FinalArray2.length; j++)
    {
      for(var k = 0; k < FinalArray3.length; k++)
      {
        if(FinalArray3[k] == FinalArray2[j] && FinalArray2[j] == FinalArray1[i])
        {
          LastArray[d] = FinalArray1[i];
          d++;
        }
      }
    }
  }
  
  return LastArray;
}