Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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 如何将多行单元格拆分为Google工作表的数组?_Javascript_Google Apps Script - Fatal编程技术网

Javascript 如何将多行单元格拆分为Google工作表的数组?

Javascript 如何将多行单元格拆分为Google工作表的数组?,javascript,google-apps-script,Javascript,Google Apps Script,我保存了一份电子表格,上面有我读过的各种漫画的标题和发行编号。我想有一个专栏,提供每个标题阅读的问题数量。有些书名有多行发行号。例如,2016年开始的《复仇者》系列在一个单元中列出了以下发行编号,每个编号在单元内的新行上: #1-11 #1.1-5.1 #1MU #672-676 我试图编写一个脚本,将每一行分隔成一个数组项,然后对于数组中的每一项,使用正则表达式提取数字以执行计算以确定总计数。(上述发行数量为22期,但问题是在我更新发行数量时,让脚本为我和其他标题确定这一点。) 以下是我目前

我保存了一份电子表格,上面有我读过的各种漫画的标题和发行编号。我想有一个专栏,提供每个标题阅读的问题数量。有些书名有多行发行号。例如,2016年开始的《复仇者》系列在一个单元中列出了以下发行编号,每个编号在单元内的新行上:

#1-11
#1.1-5.1
#1MU
#672-676
我试图编写一个脚本,将每一行分隔成一个数组项,然后对于数组中的每一项,使用正则表达式提取数字以执行计算以确定总计数。(上述发行数量为22期,但问题是在我更新发行数量时,让脚本为我和其他标题确定这一点。)

以下是我目前掌握的情况:

function calcIssueCount(x) {
  // Set initial value to 0:
  var issueCount = 0;
  // Make an array from the lines of the cell
  // split by the line break:
  var box = x.split("\n");
  for (var line in box) {
    // Determine if the line includes a 
    // range of issue numbers, indicated
    // by presence of a hyphen:
    if ("-" in line === True) {
      // Remove the # symbol from the string:
      line = line.replace("#","");
      // Extract the number before the hyphen
      // and delete the hyphen:
      var a = line(/[0-9}+\.|[0-9]+-/);
      a = a.replace("-","");
      // Extract the number after the hyphen 
      // and delete the hyphen:
      var b = line(/-[0-9}+\.|[0-9]+/);
      b = b.replace("-","");
      // Turn a and b into numbers:
      a = number(a)
      b = number(b)
      // Subtract the range start from the 
      // range end:
      var c = b - a;
      // Add 1 because a range of 1-5 includes 5
      // issues, while 5-1 is only 4:
      c += 1;
      // Update the count for the number of 
      // issues in the cell by adding the 
      // line's value:
      issueCount += c;
    }
    else {
      // If there's no hyphen, there's no 
      // range; the count of issues on the
      // line is 1:
      issueCount += 1;
    }
}
}
calcIssueCount(x)
将x作为单元格名称(例如,D15)

我可能到处都犯错误,但我特别认为我不了解如何首先将单元格数据放入数组中。这里缺少什么?

您希望从单元格中的以下值中检索“22”

#1-11
#1.1-5.1
#1MU
#672-676
作为从值中检索“22”的逻辑,我理解如下

  • #1-11
    中检索
    1
    11
  • #1.1-5.1
    中检索
    1.1
    5.1
  • 中检索
    672
    676
  • 每行从后面的数字中减去前面的数字
  • 将1添加到所有行
  • 对于不带
    -
    的行,仅添加1
根据上述逻辑,可以获得
(11-1+1)+(5.1-1.1+1)+(1)+(676-672+1)=22
。如果我对你的逻辑的理解是正确的,下面的修改怎么样

修改点:
  • 当使用(框中的变量行){
    时,可以通过
    box[line]
    检索每个元素。
    • 在这次修改中,我使用了
      forEach
  • 在Javascript中,布尔表达式“true”和“false”
  • 不是对象。因此
    行中的
    “-”发生错误。
    
    • 在这次修改中,我使用了
      indexOf()
  • 在Javascript中,
    number()
    number()
  • 当您想将
    calissuecount(x)
    用作自定义函数时,在当前脚本中不会返回任何值。因此,如果您想检索
    issueCount
    ,请添加
    return issueCount
  • 我无法理解
    行(/[0-9}+\.[0-9]+-/)
    的功能。对此我很抱歉
在我的修改中,我使用
([0-9.]+)-([0-9.]+)
的正则表达式检索了前后数字。我认为有几种解决方案可以解决您的情况。因此,请将此视为其中之一

修改脚本: 模式1: 在此模式中,您的脚本被修改

function calcIssueCount(x) {
  // Set initial value to 0:
  var issueCount = 0;
  // Make an array from the lines of the cell
  // split by the line break:
  var box = x.split("\n");
  box.forEach(function(e){
    if (e.indexOf("-") > -1) {
      var numbers = e.match(/([0-9.]+)-([0-9.]+)/);
      // Extract the number before the hyphen
      // and delete the hyphen:
      var a = Number(numbers[1]);
      // Extract the number after the hyphen 
      // and delete the hyphen:
      var b = Number(numbers[2]);
      // Subtract the range start from the 
      // range end:
      var c = b - a;
      // Add 1 because a range of 1-5 includes 5
      // issues, while 5-1 is only 4:
      c += 1;
      // Update the count for the number of 
      // issues in the cell by adding the 
      // line's value:
      issueCount += c;
    } else {
      // If there's no hyphen, there's no 
      // range; the count of issues on the
      // line is 1:
      issueCount += 1;
    }
  });
  return issueCount;
}
模式2: 这是另一个示例脚本。在此模式中,不使用正则表达式。结果与模式1相同

function calcIssueCount(x) {
  return x.split("\n").reduce(function(c, e) {
    if (e.indexOf("-") > -1) {
      var numbers = e.slice(1).split("-");
      c += (Number(numbers[1]) - Number(numbers[0])) + 1;
    } else {
      c += 1;
    }
    return c;
  }, 0);
}
结果:

注:
  • 在这个修改后的脚本中,
    ([0-9.]+)-([0-9.]+)
    的正则表达式用于示例值。如果您想将值与其他模式一起使用,请告诉我。届时,您可以向我显示更多示例吗
参考资料:

如果我误解了你的问题,我很抱歉。

不,这太好了。忘记了返回函数,我觉得自己太傻了。我有一段时间没有做过编码,在这之前是用Python编写的。感谢你在回答的修改部分对更好/正确的JavaScript的解释。没有其他模式。我选择这个单元格是因为我t具有所有不同的编号模式。非常感谢!当我尝试使用这些改进的脚本时,我得到以下错误:TypeError:无法在非对象上使用instanceof(第5行)。我试着阅读有关instanceof的文章,看看是否可以解决问题,但我看不到它们之间的联系。@thektulu7对于给您带来的不便,我真的很抱歉。您能提供电子表格样本吗?如果可以,我可以用您的样本表确认情况。如果不能,请随时告诉我。这是一份副本:感谢您的帮助。我很抱歉试图理解这些东西,但我已经有一段时间没有编写任何代码了。恐怕我对几年前学到的东西已经生疏了……完成了!一些问题被编号了,例如,1.1-1.5,而不是像大多数小数点问题那样的1.1-5.1。因此,我必须为
b-a<1
的情况添加一个条件检查,并将
b和
a
减10。谢谢大家的帮助!这里有一个内置的公式方法:
=-SUMPRODUCT(IFERROR(IMREAL(TO_TEXT)(SPLIT(B34,“#”&CHAR(10))和“i”)+virtual(TO_TEXT)(SPLIT(B34,“#”&CHAR(10))和“i”)-1)
@I'-'I我可以从你的评论中学习。非常感谢。把你的评论作为答案发布怎么样?因为我认为你的答案对OP和其他用户也很有用。哦,这看起来很酷。当然,我对它的理解比Javascript要少。我想我有东西要学习,就像塔奈克说的!所以谢谢!这个方法有j不过,还有一个问题:它会产生很多负值。例如,如果我只阅读一个系列的第207期,则发行数量最终为-206。任何一本系列中只有一期的书都会被计算为0,等等。绝对值会在某个地方解决这个问题吗?或者更确切地说,是通过某种方式添加一个伪值进行减去吗?我想明天我会看一看o电子表格公式中的“地狱者”、“非真实”、“文本”和“想象者”都做了些什么。@Tanaike&thektulu7谢谢,但这最好留作评论。当在OP的工作表上检查时,它在多个级别上都失败了。脚本解决方案更容易适应。如果我能找到更好的解决方案,我会发布一个答案。再次感谢。@I'-'I感谢你的帮助