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
Google apps script 为什么我的返回数组函数赢了';t使用或结合;“数学”;方法返回到数组';s的价值观?_Google Apps Script_Google Sheets_Return_Return Value_Spreadsheet - Fatal编程技术网

Google apps script 为什么我的返回数组函数赢了';t使用或结合;“数学”;方法返回到数组';s的价值观?

Google apps script 为什么我的返回数组函数赢了';t使用或结合;“数学”;方法返回到数组';s的价值观?,google-apps-script,google-sheets,return,return-value,spreadsheet,Google Apps Script,Google Sheets,Return,Return Value,Spreadsheet,我猜。。。我正在尝试编写一个函数,它返回一个数组,我可以将该数组转换为字符串格式。就像我在这里要提到的:我有随机颜色(CustomColorSarray,takenColorsArray)函数取决于类是否已填充,如果两者都为空,则返回颜色值已通过数学方法计算,如果CustomColorSarray类已由用户填充以放置自己的数组,如果这两个类都已由用户写入,则返回数组的var已在该函数中的较早位置设置。为了使这一点清晰可见,请参见以下代码: 这是实现上述函数的HTML代码。 一种简单而直

我猜。。。我正在尝试编写一个函数,它返回一个数组,我可以将该数组转换为字符串格式。就像我在这里要提到的:我有随机颜色(CustomColorSarray,takenColorsArray)函数取决于类是否已填充,如果两者都为空,则返回颜色值已通过数学方法计算,如果CustomColorSarray类已由用户填充以放置自己的数组,如果这两个类都已由用户写入,则返回数组的var已在该函数中的较早位置设置。为了使这一点清晰可见,请参见以下代码:


这是实现上述函数的HTML代码。


一种简单而直接的修复代码的方法是替换代码行

randomSecondColor=personaldrandomcolor(randomColor(toString(skipFirstColors)))
为了

randomSecondColor=personaldrandomcolor(randomColor(JSON.parse(skipFirstColors)))
原因是
randomColor
接受元素数组,并向其传递字符串。参数skipFirstColors是一个字符串,实际上是字符串列表的JSON表示形式。您希望它是一个实际的列表,而不是表示列表的字符串

函数将计算skipFirstColors返回的字符串,并返回它所表示的列表


此外,我建议您重新访问并重构代码。您应该尝试使用内置类型(如列表)作为函数返回,而不是使用对象的JSON表示(序列化为字符串),因为这样可以避免此类错误,并使代码更清晰。

预期的输出是什么?问题是什么?什么有效,什么无效?见@TheMaster。。。为什么在我测试从“randomSecondColor”的var运行函数时,例如:console.log(randomSecondColor);输出并不是我真正期望的结果是什么?字符不是颜色的值???您好@JamalLudin,您能描述一下您想要实现的目标吗?也许那样我们可以理解这个问题并帮助你。谢谢@Carlesg97。。。我想从randomSecondColor的返回中准确地选择值,它必须是颜色值,而不是输出的字符…
typeof customColorsArray
字符串,而不是
数组。对吗<代码>customColorsArray[Math.floor(Math.random()*customColorsArray.length)];}这与
“随便什么”[1]
天哪,谢谢。。。最后,你们清楚明确地回答了我的问题。这也是@TheMaster从一开始就试图向我解释的事情,非常感谢你们两位!!!其他人也猜。。。再感谢一千次…@jamalludin很高兴知道它对你有用,伙计!:)哦,嘿@Carlesg97我可以再问你一次吗?这仍然是一个关于上面发生的事情的问题!为什么在我的“randomSecondColor”变量中,有时但不太经常,它只会在几次之后结束,它会显示数组的所有项,即使我已经添加了代码来替换“randomSecondColor”中加载的内容,那么这将导致跳过第一种颜色???对不起,这是我自己解决的。我刚刚用.replace()行类中的一些正则表达式解决了这个问题,这将使它得到修复。
function randomColor(customColorsArray, takenColorsArray) {
 var text = "",
     colors = ["orange", "yellow", "red", "maroon"];

   if (customColorsArray && takenColorsArray) {
      var text = "["+colors+"]";
   }
     else if (!customColorsArray && !takenColorsArray) {
      text += colors[Math.floor(Math.random() * colors.length)];
   }
     else {
      text += customColorsArray[Math.floor(Math.random() * customColorsArray.length)];
  };

   return text;
}

function personalRandomColor(e) {
 var text = "";

   if (e == "orange") {text += "almond";}
    else if (e == "yellow") {text += "melrose";}
    else if (e == "red") {text += "danube";}
    else if (e == "maroon") {text += "magenta";};

  return text;
}
bla... bla... bla...
  var customColorsArrays = randomColor('passingClass', 'takenColor'),
      randomFirstColor = randomColor(),
      skipFirstColors = customColorsArrays.replace('\[', '\[\"').replace('\]', '\"\]').replace(/[\,]/g, '\"\, \"').replace('\"'+randomColor()+'\"\,', ''),
      randomSecondColor = personalRandomColor(randomColor(toString(skipFirstColors))),
bla... bla... bla...