联合函数 - 在自定义函数中传递参数 - Google电子表格 [英] Union function - Passing argument in custom function - Google-spreadsheet

查看:134
本文介绍了联合函数 - 在自定义函数中传递参数 - Google电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试回答
此问题时,我发现了一个奇怪的行为。

While trying to answer this question, I found a strange behavior.

这里是我的代码:

Here's my code :

function remove(val, array){
  var res = new Array();
  for(var i=0; i<array.length; i++){
    if(array[i] != val){
      res.push(array[i]);
    }
  }
  return res;
}

//we assume that there is no duplicates values inside array1 and array2
function my_union(array1, array2){
  var longuer;
  var shorter;
  var arrayRes = new Array();
  if(array1.length < array2.length){
    longuer = array2;
    shorter = array1;
  } else {
    longuer = array1;
    shorter = array2;
  }

  for(var i=0; i<longuer.length; i++){
    arrayRes.push(longuer[i]);
    shorter = remove(longuer[i], shorter);
  }

  for(var i=0; i<shorter.length; i++){
    arrayRes.push(shorter[i]);
  }

  return arrayRes;
}

function test(){
  Browser.msgBox(my_union([1,2,3], [1,2]));
}

消息框明确表示1,2,3,但当您尝试调用这个函数在电子表格中有相同的值,它不能删除重复的值,为什么?

The message box clearly says 1,2,3 but when you try to invoke this function inside a spreadsheet with the same values, it fails to delete duplicated values, why ?

**编辑:**
感谢Henrique的回答,这里是代码:

**EDIT : ** Thanks to Henrique's answer , here's the code :

function remove(val, array){
  var res = new Array();
  for(var i=0; i<array.length; i++){
    if(array[i] != val){
      res.push(array[i]);
    }
  }
  return res;
}

function matFlattener(matrix){
  var array = new Array();
  for(var i=0; i<matrix.length; i++){
    for(var j=0; j<matrix[i].length; j++){
      array.push(matrix[i][j]);
    }
  }
  return array;
}

function my_union(matrix1, matrix2){
  //assert no duplicate values in matrix1 and matrix2
  var longuer;
  var shorter;
  var array1 = matFlattener(matrix1);
  var array2 = matFlattener(matrix2);
  var arrayRes = new Array();
  if(array1.length < array2.length){
    longuer = array2;
    shorter = array1;
  } else {
    longuer = array1;
    shorter = array2;
  }

  for(var i=0; i<longuer.length; i++){
    arrayRes.push([longuer[i]]);
    shorter = remove(longuer[i], shorter);
  }

  for(var i=0; i<shorter.length; i++){
    arrayRes.push([shorter[i]]);
  }

  return arrayRes;
}


推荐答案

当您调用自定义函数从电子表格中传递多范围参数,无论您传递单个行还是列,此参数都将始终为<矩阵>。

When you call the custom function from the spreadsheet and pass a multi-range parameter, this parameter will always be a matrix, regardless if you pass a single row or column.

为了测试电子表格这样的行为,您应该像这样更改 test 函数:



To test the behavior like the spreadsheet does, you should change your test function like this:

function test() {
  //note the "extra" brackets
  Browser.msgBox(my_union([[1,2,3]],[[1,2]]); //passing "single" rows
  Browser.msgBox(my_union([[1],[2],[3]],[[1],[2]]); //passing "single" columns
}

解决方案是调整您的 my_union 公式来解释它。

The solution is adjust your my_union formula to account for it.

这篇关于联合函数 - 在自定义函数中传递参数 - Google电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆