简体   繁体   English

在 Google Sheets 中制作自定义 function,代码在工作表之外工作,但不在工作表中

[英]Making a custom function in Google Sheets, the code works outside of sheets but not in sheets

So I'm writing a custom function for Google sheets which is supposed to take a single row or cells, produce a 1 dimensional array and use that data to create a URL.因此,我正在为 Google 工作表编写一个自定义 function,它应该采用单行或单元格,生成一维数组并使用该数据创建 URL。 This is basically so I can help my friends track the prices of an item in a video game.这基本上是为了让我可以帮助我的朋友跟踪视频游戏中物品的价格。

I have ran the code in a javascript environment outside of Google Sheets and everything acted as expected.我已经在 Google 表格之外的 javascript 环境中运行了代码,并且一切都按预期运行。 But when I put the code in the script editor, it runs with unexpected behavior.但是当我将代码放在脚本编辑器中时,它会以意想不到的行为运行。 The range of cells will always be 1 row by 13 columns but some of the columns may be void so the script is supposed to truncate the array of values so that there are no empty indexes.单元格范围将始终为 1 行 x 13 列,但某些列可能是空的,因此脚本应该截断值数组,以便没有空索引。 Then it is supposed to insert a "."然后它应该插入一个“。” between each value as it is concatenated into a string to produce the final URL.每个值之间,因为它被连接成一个字符串以产生最终的 URL。 For some reason it does not remove any values and the "."由于某种原因,它不会删除任何值和“。” are ","s when the URL is created in the cell.当在单元格中创建 URL 时,是 ","s。

Here is the code:这是代码:

function STONKLINK(prices, premrkt) {
  var webLink = "https://turnipprophet.io?prices=";
  var priceLink = "";

  for (i = (prices.length - 1); prices[i] == 0; i--){
    prices.pop();
  }
  for (i = 0; i < prices.length; i++) {
    priceLink = (priceLink + prices[i] + ".") 
  }

  priceLink = priceLink.substring(0, priceLink.length - 1);

  if (premrkt == "N/A") {
    webLink = webLink + priceLink;
  } else if (premrkt == "Fluctuating") {
    webLink = webLink + priceLink + "&pattern=0";
  } else if (premrkt == "Small Spike") {
    webLink = webLink + priceLink + "&pattern=3";
  } else if (premrkt == "Large Spike") {
    webLink = webLink + priceLink + "&pattern=1";
  } else if (premrkt == "Decreasing") {
    webLink = webLink + priceLink + "&pattern=2";
  }
  return webLink;

}

Any help is very much appreciated.很感谢任何形式的帮助。 Thank you so much.太感谢了。

EDIT: While I still do not understand why this is happening, and I would like to understand, I have added a simple work around.编辑:虽然我仍然不明白为什么会发生这种情况,并且我想了解,但我添加了一个简单的解决方法。 By adding this line of code通过添加这行代码

webLink = webLink.replace(/,/g, ".");

before the return , all the commas will be replaced with dots.return之前,所有的逗号都将被替换为点。 Still, any insight into the problem will be much appreciated.尽管如此,对这个问题的任何见解都将不胜感激。 Thank you again.再次感谢你。

When you store values in Spreadsheets, the values which have a , are considered to be strings whilst the ones which have a .当您在电子表格中存储值时,具有 a ,值被视为strings ,而具有. will be considered to be numbers .会被认为是numbers

So for example, if you have these values stored in the B column:例如,如果您将这些值存储在B列中:

电子表格中的值

If you log the typeof(val1) and typeof(val2) (where val1 and val2 are the values stored in B1 respectively B2 ), this is what you will get:如果您记录typeof(val1)typeof(val2) (其中val1val2分别是存储在B1中的值B2 ),您将得到以下结果:

记录值

So the issue you were encountering is due to this fact.所以你遇到的问题是由于这个事实。 The values you have stored in your sheet are numbers and when you try to build the webLink string they are converted into strings - therefore, the .您存储在工作表中的值是数字,当您尝试构建webLink字符串时,它们会转换为字符串 - 因此, . for a number becomes , .一个数变成, .

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM